Rscript draws in PDF

I have a simple R script. When it is launched through Rscript.exe, by default it is output to a PDF file. I want the script to open a chart window.

I use the command:

Rscript.exe tmp_plot.R 

r tmp_plot.R file contains:

 x <- 1:10 y <- sin(x) plot(x,y) 
+7
source share
3 answers

You run R in non-interactive mode - Rscript is for scripts, so the default build device is pdf() , not x11() , or something else by default for your OS ( windows() in appearance), however, it’s trivial to open an alternative device; use x11() or windows() . The problem that you are trying to write a script that will display a graph on the screen is that in the example code you showed, the script ends immediately after drawing the graph displayed on the screen or on the pdf() device. In the best case, you can pause it with using Sys.sleep() , for example:

 x <- 1:10 y <- sin(x) x11() ## or windows() plot(x,y) Sys.sleep(10) 

I think you are going wrong on this. If you want interactivity when you run R "script", by which I mean a set of R statements that do some analysis, you'd better get an editor / IDE on your OS that allows you to go through a script line or piece of code at a time, and also interact with the current session R. For this, I use Emacs and the ESS extension. You can consider Tinn-R or RStudio as alternatives.

Rscript designed to run scripts or batch jobs that do not need human interaction or intervention.

+18
source
 library(tcltk) # for message box and thus hold-open functionality x11() # for Linux, see documentation for other operating systems # first plot # second plot # hold-open functionality prevents script from exiting user acts prompt <- "hit spacebar to close plots" extra <- "some extra comment" capture <- tk_messageBox(message = prompt, detail = extra) 

If you do not like the idea of ​​a given timer, the above script will only exit the keystroke (space or enter) or mouse click (OK button) from the user.

+5
source

Using windows, I worked on this problem by writing a batch script that calls Rscript in a script, and then immediately opens the generated PDF file.

 @echo off rscript myscript.r Rplots.pdf 

Although this solution is ridiculous, this is exactly what I need, which just views the data on the go.

0
source

Source: https://habr.com/ru/post/894810/


All Articles