R pop-up boxes not working when run in terminal - r

I am using the svDialogs (an R wrapper library for zenity) to create GUI pop-up boxes, and this works fine when I run the code through either R studio, or from an R terminal session (running Ubuntu 16.04).
A minimal example is:
library(svDialogs)
dlgMessage("Hello Stackoverflow!")
However, when I run the code directly through the terminal it does not work:
Rscript --vanilla -e 'source("path/to/file.R")'
The terminal shows that the library loaded, and does not display an error message: but the pop-up does not appear! If I add an additional line after the call to dlgMessage, that line runs. i.e. if I run the modified code
library(svDialogs)
dlgMessage("Hello Stackoverflow!")
print("Goodbye Stackoverflow!")
then the second line does show in the terminal window (i.e. the code is not crashing at dlgMessage).
Happy for solutions not relying on dlgMessage if there is a workarond: I'd previously tried using Zenity natively through R using system() but couldn't get this to work.

R can be run in either interactive or non-interactive modes, with the default depending on whether or not it is assumed that there is a human operator, see documentation for interactive.
When run in non-interactive mode, R will not display any pop-up boxes. The default is that when running code in the terminal, R runs in non-interactive mode. Following the documentation above, this can be overwritten by using the command in linux
R --vanilla --interactive < "path/to/file.R"
Similarly in Windows using --ess with Rterm.exe

Related

ProfileView plot not showing when running Julia Code

I wanted to profile my code in Julia and I installed the package "ProfileView.jl". However, when I run the code the windows is popping for a second and closing immediately.
I have run the script from VS Code and also from the terminal. The code I am profiling is:
using ProfileView
#profview Mesh,MatPointData, AnalysisConstants = setupfunc(T)
In general, you should run ProfileView interactively in a REPL, not as part of a script. If you're using VSCode, you want to start the Julia REPL (Ctrl-Shift-P and then Julia: Start REPL); from the terminal, you want to run an interactive session, i.e., $ julia and not $ julia somescript.jl.
If you simply must use a script (again, not recommended because you'll lose access to some nice interactive functionality in ProfileView), see https://juliagraphics.github.io/Gtk.jl/latest/manual/nonreplusage/.

Writing Console Virtual Terminal Sequences From R to Powershell

I'm trying to write colored output back to the terminal from an R script running in that terminal. I'm working in the Windows Powershell in Rstudio on Windows 10. It seems like the powershell supports colors, but I cannot get my outputs from R to activate the virtual terminal sequences needed to display them. If I use echo in the terminal, I can display the colors the way I want to:
I've tried multiple ways of writing them to the terminal from within an R script, including using the popular crayon package, but none seem to work:
# test.R
library(crayon)
cat("$([char]27)[36mCyan!$([char]27)[37m\n")
cat("$([char]27)[36m", "Cyan!", "$([char]27)[37m", "\n")
cat(green("Green!"),"\
Note that this post addresses the use of color in the RStudio console while I'm trying to get it to work in the terminal, which is separate. Crayon correctly outputs the colors in the console, but you can't execute a script there.
Is there a way to write lines to the terminal so that the virtual terminal sequences will work properly?

new version of R with rstudio

I installed a new version of R but rstudio still uses the old version. The command "which R" is just a shell script and I'm not sure how to get rstudio and the new version of R integrated. R base installed in /usr/share/doc.
Any tips?
Thanks,
Bob
See the RStudio support pages. In particular, for Linux, you have to set the RSTUDIO_WHICH_R environment variable.
As found out in the comments, you're on a Linux system, specifically Linux Mint 17. I can see three basic scenarios here:
You want to ensure RStudio uses a specific version of R when you are launching RStudio from the terminal as a one-time event.
You want to ensure RStudio uses a specific version of R every time you launch RStudio from the terminal.
You want RStudio to use a specific version of R when you launch RStudio from the applications menu (or, equivalently, via something like Synapse).
They are dealt with in turn below. I assume throughout that the path to the R binary you want RStudio to use is /opt/R/3.1.0/bin/R, which you should change as appropriate.
One-time Terminal Launch
After opening a terminal via Ctrl-Alt-T, run
export RSTUDIO_WHICH_R=/opt/R/3.1.0/bin/R
Then, anytime you launch RStudio from the terminal via the rstudio command in that terminal session, it will use the specified R version. However, after you exit, the next time you open the terminal, it will no longer respect that choice.
Every Terminal Launch
Use your favorite text editor to edit the file ~/.bashrc. At the end, on a new line, add
export RSTUDIO_WHICH_R=/opt/R/3.1.0/bin/R
Then, either launch the terminal, or if you already have it open run the command source .bashrc. Then, you can launch RStudio via the command rstudio and it will use the version of R you want.
Launching from the Applications Menu
Use your favorite text editor to edit the file ~/.profile. At the end, on a new line, add
export RSTUDIO_WHICH_R=/opt/R/3.1.0/bin/R
Then you need to log out of your system and log back in. After that, anytime you launch RStudio from the application menu, it will use the specified R version.

Can I start an Rcmdr session from a unix shell?

I want to launch Rcmdr as a command from bash (or any unix shell), perhaps as an alias. R accepts the CMD argument and I could also pipe a script in with <. I would like the R console to stay open, and an interactive RCommander session to be started (Rcmdr is a popular GUI for R, for any newbies reading along, and it seems that you start up R, type library(Rcmdr) and then Commander() to start it up).
I am aware of how to add Rcmdr to my profile, and it appears to always start up if I include library(Rcmdr) in my .Rprofile, on my Linux workstation.
If I pipe my input in with < then this script works up to the point where it says that Commander GUI is launched only in interactive sessions:
library(Rcmdr);
Commander();
However if I run R CMD BATCH ./rcommander.r it just starts up and shuts down immediately, probably giving me some warning about interactive sessions that I didn't see, because CMD BATCH puts R into non-interactive mode and is thus useless for the purpose of "injecting" Rcmdr into an interactive R session.
It appears impossible to "source a file on the command line but run interactively" in R. It also appears that there are command line options to ignore the global and the user profile, but not to specify a custom profile like R --profile-custom ./.Rprofile2
Either I would like to specify a profile that means "Right now I want to start up and use RCmdr" and still be able to run R without it sometimes.
Working on an Ubuntu machine here, I was able to use the advice provided by Dirk in this mailing list post:
nathan#nathan-laptop:~/tmp$ cat rcommander.r
#!/bin/bash
r -lRcmdr -e'while(TRUE) Commander();'
nathan#nathan-laptop:~/tmp$ cat rcommander2.r
#!/bin/bash
Rscript --default-packages=Rcmdr -e 'while(TRUE) Commander();'
The first script uses Dirk's littler package, available on CRAN, and the second uses the standard Rscript executable. As noted, you can kill the process with ctrl + c from your terminal.

how to display r console in matlab

i am running a R script in MATLAB environment using the "system" command as described in:
How to Run a R script from Matlab
my R script takes too long to proceed. when i use the Rstudio itself, the progress is displayed in the r console. but i am not able to see the progress status in matlab screen when i call the R script from Matlab.
how can i see the progress of running the R script.
is there any command for that?
You need to specify the '-echo' option when using the system command
[status,cmdout] = system(command,'-echo')
http://nl.mathworks.com/help/matlab/ref/system.html
I've used the system function with the -echo option to run ffmpeg from the matlab command window, and the resulting command window output is the identical to what I see in the cmd.exe console.
If this doesn't resolve the issue, can you post some example code of what your doing?

Resources