7

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.

Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
BetaScoo8
  • 429
  • 4
  • 9
  • Maybe output to a graphics file (e.g. PNG) and pop that up on the screen with an appropriate viewer? – Ben Bolker Nov 17 '11 at 13:21
  • 1
    Combining this with Latex (maybe using Sweave, also possible without I think) would allow you to generate a pdf with a report. – Paul Hiemstra Nov 17 '11 at 13:26
  • I'm unfortunatly unfamiliar with latex (will have a look though), and image viewers doesn't seam like a very robust way of getting this to work (i.e. someone installs some camera software and everything breaks) I wonder if a batch file could help, just attempted on but the rscript still quit on exit and closed the graph windows. Thanks for the ideas though. – BetaScoo8 Nov 17 '11 at 13:43
  • 2
    Or open the graphics file in a web browser (you may be interested in the `brew` package as well -- easier than LaTeX) ? – Ben Bolker Nov 17 '11 at 14:15

4 Answers4

9

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.

Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
  • The script actually calls windows() 4 times, but they are all closed when the script exits unfortunately. I tried using locator() but either I'm not fast enough with my clicks, or it doesn't work. – BetaScoo8 Nov 17 '11 at 14:07
  • It's supposed to keep the plot window up _until_ the user clicks. Sorry it doesn't work for you. – Michael Hoffman Nov 17 '11 at 14:16
  • Ah ok, could be the way I've implemented it also. Thanks – BetaScoo8 Nov 17 '11 at 14:20
  • I've realised that one of the functions was not returning any data from the database and was throwing an error, this was preventing this locator(1) method from working, It now does, many thanks. – BetaScoo8 Nov 17 '11 at 16:10
2

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

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

Spacedman
  • 92,590
  • 12
  • 140
  • 224
1

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!
Jimi WIlls
  • 348
  • 1
  • 10