I know RScript is non-interactive, but I would like to display figures without running into the R interactive console first (the same way as in python and java).
In other words, I am hoping to be able to run a single build command from command prompt or terminal that runs an R script and displays its figures.
Thanks.
If you are familiar with shiny you can build simple shinyApp to display file.png file in browser:
Rscript -e 'png("file.png"); plot(1:10); dev.off(); runApp("display_png_app")'
Good comment by #Ista: you don't need to use shiny, you can simply use browseURL("file.png") command instead runApp.
Related
I'm using R for some processes, and I was wondering if it was possible to save a copy of an Rscript with the parameters in the script after it has ran - instead of args[1]. This would let the user be able to open the saved Rscript in Rstudio after the script has ran, with their argument instead of args[x] for any alterations they might want to make to plots.
For example:
Rscript plot_script.r path/to/file
read.table(args[1])
Should be:
read.table("path/to/file")
In the saved output Rscript.
Is this possible?
I have written an interactive R Script which gives the user certain options using which the rest of the execution takes place. These inputs have to be optional as required by the user. One other requirement from the user was that he did not want to manually select the entire script in RStudio just to execute it. I tried to create a BAT file for this execution. However, the program does not execute. Below is an example user interactive script.
FYI_Splitter <- winDialog(type = "yesno","Do you want to remove FYI from base file?
Yes = Remove FYI
No = Do Not Remove FYI")
Below is what I am using to execute the BAT file.
"C:\Program Files\R\R-3.4.2\bin\Rscript.exe" "<Path_of_file>\Test_Program.R"
I came to know that batch file execution does not support interactive sessions. Is there any way to make interactive sessions in R execute without selecting the entire code in RStudio? Any help on this would be appreciated.
You could try
echo source("<Path_of_file>\Test_Program.R") | R.exe --ess --vanilla
The interactive() command in R then returns TRUE for me.
I'd like to run R as an inferior process. I'd like R to display help pages as html (in the browser) and to display plots as if it were run interactively. I know I can run R with the --interactive argument but then R seems to assume it's running on a terminal end emit control sequences.
How should I run R to get html help pages & plot (as with --interactive) and simple textual output with no control sequences?
If I use R, it assumes to be run non-interactively (html help pages or plots won't work but the output contains no control sequences).
If I use R --interactive --no-readline, it runs interactively (help pages and plots work as expected) but the output is garbled and difficult to parse since it assumes to be running on a terminal.
Is there a way to control the assumptions R makes about the terminal it's running in?
It seems that argument order matters. (Part of) the problem gets resolved when calling R as R --no-readline --interactive.
In Python, running the interpreter with the -i flag first executes the script, then drops back into the interpreter
python -i hello.py
Hello world
>>> print("Python ftw")
Python ftw
>>>
which allows me to type commands and reach the variables after execution.
With R, this seems to be of great difficulty. I have been searching online for some time, and am surprised to see there is not so many results with the keywords "R run file shell interpreter".
With R, you can use
$ R -f myfile.R which executes and then exits the interpreter
$ Rscript myfile.R which still does the same thing.
Even worse, it does not plot when run like this and just exits without showing any signs that something has been plotted.
So, to repeat my question:
How do I make R to drop into the R shell after running commands from a file, a.k.a. a script?
Concurrently, how can I make R really plot the plots and not close them off immediately?
I can do these with Python, MATLAB, Octave, Ruby and many others, and should be able to do with R too.
I will answer your two questions separately:
How do I drop into a shell after my script has executed?
The function "browser" called with no arguments will allow to to drop into a shell on the line that it's called. Appending this to your script should do the trick.
How do I save graphics when not run in interactive mode?
First, check that there isn't a pdf file being created in your working directory. Depending on how you're running R, I believe it may be named "Rplots.pdf". Personally, however, I prefer to explicitly save graphics to a particular file, as such:
pdf("temp.pdf")
plot(rnorm(100))
dev.off()
which will save the plot in a new file called temp.pdf (and will overwrite any existing file by that name, so watch out).
Functions analagous to "pdf" exist for other image formats if you would prefer that.
I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.