Make R (statistics package) wait for keyboard prompt when run within a bash script

前端 未结 5 911
别跟我提以往
别跟我提以往 2021-02-15 13:14

I am using R to generate a series of plots within a loop, with the user hitting the enter key to indicate they have seen the plot and it is time to move on. These are interacti

相关标签:
5条回答
  • 2021-02-15 13:43

    It's a late answer but my goal was similar: Rscript execution should bring up an rgl window with a plot and nothing else, and it should remain there till the window is closed, i.e. the rgl window should not terminate.

    To achieve this, I simply placed this at the end of the R script, and the rgl plot will remain there for manipulation until you quit the window, consuming little CPU time:

    play3d(function(time) {Sys.sleep(0.01); list()} )
    

    For regular 2D R plots, locator() works similarly, or locator(1) if one click should close the plot window.

    0 讨论(0)
  • 2021-02-15 13:44

    I'm not sure if there is an easy way to wait a keyboard input, but at least you can wait mouse click.
    Not elegant but try this script:

    quartz() # or maybe windows() in windows
    for (i in 1:5) {plot(i, i); locator(1)}
    
    0 讨论(0)
  • 2021-02-15 13:45

    plot.lm uses devAskNewPage(TRUE); perhaps that would also work here.

    0 讨论(0)
  • 2021-02-15 13:46

    Use this:

    readLines("stdin", n = 1)
    

    That will get the real stdin instead of what stdin() uses.

    I'd invoke it with:

    Rscript myfile.r
    
    0 讨论(0)
  • 2021-02-15 14:00

    Here is an example script that works for me (tested your first calling method on windows). It uses the tcltk package and creates an extra, small window with a single button, the script will pause (but still allow you to interact with the rgl window) until you either click on the 'continue' button on press a key while that window is active, then continue with the script.

    library(tcltk)
    library(rgl)
    
    mywait <- function() {
        tt <- tktoplevel()
        tkpack( tkbutton(tt, text='Continue', command=function()tkdestroy(tt)),
            side='bottom')
        tkbind(tt,'<Key>', function()tkdestroy(tt) )
    
        tkwait.window(tt)
    }
    
    
    x <- rnorm(10)
    y <- rnorm(10)
    z <- rnorm(10)
    
    plot3d(x,y,z)
    
    mywait()
    
    x <- rnorm(100)
    y <- rnorm(100)
    z <- rnorm(100)
    
    plot3d(x,y,z)
    
    mywait()
    
    cor(x,y)
    
    0 讨论(0)
提交回复
热议问题