Question

I have have a R script which queries a database, runs some analysis, plots a few charts based on the current system date.

I want to get this script to run daily at boot, I thought I could do this fairly simply using a shortcut to rscript.exe with necessary parameters.

This works fine, however the script quits after it is run, not very useful for viewing the charts.

I'm using XP and win7.

Is there an easy way to keep the output from the script on screen? I've tried incorporating scan into the script, but it doesn't pause.

I know I could just open the rgui, and run a single line of code, but the plan is to deploy this to a colleague's computer who is totally unfamiliar with R.

Was it helpful?

Solution

This works for me on Linux:

#!/usr/bin/env Rscript

X11()
with(mtcars, plot(mpg, hp))
locator(1)

The user has to click the plot window before it disappears. I presume it would work on Windows with a call to windows() instead.

OTHER TIPS

Michael's solution may already work, but here is the something showing a plot inside a tkrplot frame. The tkrplot package (on CRAN) uses the tcltk extensions to R and is available everywhere.

# From http://stackoverflow.com/questions/3063165/
#     r-building-a-simple-command-line-plotting-tool-
#     capturing-window-close-events

require(tcltk)
library(tkrplot)

## function to display plot, called by tkrplot and embedded in a window
plotIt <- function(){ plot(x=1:10, y=1:10) }
tt <- tktoplevel()       ## create top level window event handler
done <- tclVar(0)        ## variable to wait on    
## bind to the window destroy event, set done variable when destroyed
tkbind(tt,"<Destroy>",function() tclvalue(done) <- 1)
## Have tkrplot embed the plot window, then realize it with tkgrid
tkgrid(tkrplot(tt,plotIt))
tkwait.variable(done)    ## wait until done is true
## script continues, or exits, ... once plot is closed

If you look through the tcltk documentation for R, you find other examples with 'Ok' buttons to close etc.

How about Sys.sleep(1e30)? That should wait for long enough.

OK, I was gonna totally bitch about all the answers I've seen to this kind of question because none of them worked on windows. readline, tkwait.window, Sys.sleep(1e30), while(TRUE), none of it worked.

But I just updated R to v3.1.0 and now tkwait.window(yourmainwindow) works, while(TRUE){} works, though Sys.sleep(1e30) still doesn't not work.

Never mind... I'm using tkwait.window, cos it's tk, and is waits for my window (which is what I want).

Getting the e.g. from http://www.sciviews.org/_rgui/tcltk/OKtoplevel.html to work... (comments removed for brevity)

require(tcltk)
tt <- tktoplevel()
OK.but <- tkbutton(tt, text = "OK", command = function() tkdestroy(tt))
tkgrid(OK.but)
tkfocus(tt)
tkwait.window(tt) # <-- added this to make the window stay!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top