Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running R Scripts with Plots

Tags:

bash

shell

r

I have a small shell script (bash) which runs a R script which produces a plot as output. Everything works fine but immedietly after the plot is rendered R quits. Is there a way to keep the R session alive until the plot window is closed.

The shell script.

#!/bin/bash R --slave --vanilla < myscript.r 

And the R script.

daq = read.table(file('mydata.dat')) X11() pairs(daq) //R Completes this and then exits immediately. 

Thanks in advance for any help!

like image 607
Stephen Diehl Avatar asked Jul 21 '10 16:07

Stephen Diehl


People also ask

How do I run an entire script in R?

To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).

How do I run an R code in line by line?

To execute one specific line: we simply click into the line we would like to execute and press the button with the green arrow ("execute current line or selection; or CTRL+ENTER ). RStudio will copy this line into the R console and execute the line (as if we would enter the command and press enter).

Is it possible to use scripts in R?

You can run the code in your R script easily. The Run button in the Script Editor panel toolbar will run either the current line of code or any block of selected code. You can use your First script. R code to gain familiarity with this functionality.

Can you run R scripts from command line?

The most convenient way to run R scripts from the command line is to use Rscript, an alternative front-end to run R code. Rscript is capable of executing R code from different command interpreters, such as a bash script on Linux or a Task Scheduler task on Windows.


1 Answers

If you use the Rscript command (which is better suited for this purpose), you run it like this:

#!/usr/bin/Rscript  daq = read.table(file('mydata.dat')) X11() pairs(daq)  message("Press Return To Continue") invisible(readLines("stdin", n=1)) 

Make sure to set the execute permission on myscript.r, then run like:

/path/to/myscript.r 

or without the shebang:

Rscript /path/to/myscript.r 
like image 157
Mark Avatar answered Oct 06 '22 13:10

Mark