Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop up the graphics window from Rscript?

Tags:

plot

r

I'm using Rscript to run a plot as follows:

x=1:10 
y=1:10 
plot(x,y) 

I expect this code to popup a window with a graphic showing the plot when I run the code like this:

Rscript plot.R

The program finishes to completion, and the graphic does not appear, not even momentarily. I know this code is right because it does produce a plot in the Rstudio GUI.

Does Rscript have a feature to popup that plot automatically on execution?

like image 231
Eric Leschinski Avatar asked Mar 02 '17 00:03

Eric Leschinski


People also ask

How do I open windows in R?

Getting started Start R by double-clicking on the R icon on the desktop, or by clicking on the R icon in the start menu. The R graphical user interface (GUI) will open, containing a single window called the command or console window.


1 Answers

Pop up the graphics window from Rscript, Example 1:

library(tcltk)
x=1:10 
y=1:10 
windows()     #Use X11() or quartz() if on linux or mac.
plot(x,y)
prompt  <- "hit spacebar to close plots"
extra   <- "some extra comment"
capture <- tk_messageBox(message = prompt, detail = extra)

The above code presents the plot in a popup window and waits for you to press ok on the tk_messageBox dialog.

Pop up the graphics window from Rscript, Example 2:

png("mygraphic.png") 
x = 1:10
print(x^2)
plot(x, x^2, 'o')
print("done")
dev.off()
browseURL("mygraphic.png") 

The above code saves the png to disk as a file and asks the operating system to open the file in the program designed for that filetype.

like image 54
Eric Leschinski Avatar answered Sep 19 '22 21:09

Eric Leschinski