Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run R interactively from Rscript

I'm trying to start a shiny app or an interactive .Rmd document from an Rscript. However, all I get is a message

Listening on http://127.0.0.1:...

I believe this is because R is running in interactive mode (another post about this). How can I write the proper Rscript so that either of the following would work?

My script

#!/usr/bin/Rscript

## This
library(shiny)
runApp(appDir = "../app")

## Or this
## rmarkdown::run("Main.Rmd")
like image 241
Rorschach Avatar asked Jul 20 '15 20:07

Rorschach


People also ask

How do I run an interactively in R?

You can run R interactively or in batch mode. e.g. type in R from the shell. The window that appears is called the R console. Any command you type into the prompt is interpreted by the R kernel.

Can you run Rscript from terminal?

If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon.

What is interactive mode in R?

An interactive R session is one in which it is assumed that there is a human operator to interact with, so for example R can prompt for corrections to incorrect input or ask what to do next or if it is OK to move to the next plot. GUI consoles will arrange to start R in an interactive session.

How do I run an Rscript from another Rscript?

You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.


1 Answers

If I understand your question correctly, I was able to achieve this with littler, which I use in lieu of Rscript for scripting tasks that revolve around R. I'm running CentOS 7, and based on the code in your question it looks like you are on a Unix-like machine, so installing littler should not be an issue. For minimal reproducibility, I used the default shiny application and shiny-based Rmarkdown templates provided by RStudio, saving them as testapp (the project / application directory name) and testRMD.rmd, respectively. Then, I have the following scripts:


testapp.r

#!/usr/bin/env r

shiny::runApp(
  "~/tmp/delete/testapp",
  port = 7088, 
  launch.browser = TRUE,
  host = "127.0.0.1")

testRMD.r

#!/usr/bin/env r

rmarkdown::run(
  file = "testRMD.rmd",
  dir = "~/tmp/delete",
  shiny_args = list(
    port = 7088,
    launch.browser = TRUE,
    host = "127.0.0.1"))

Set the permissions for these files so they can be executed -

[nathan@nrussell R]$ chmod +x testapp.r testRMD.r

(chmod +u ... should suffice, but regardless...), and you should be all set to run them from your terminal, etc...


[nathan@nrussell R]$ ./testapp.r
Loading required package: shiny

Listening on http://127.0.0.1:7088

enter image description here

[nathan@nrussell R]$ ./testRMD.r
Loading required package: shiny

Listening on http://127.0.0.1:7088

enter image description here


There is some additional command line output for the Rmd file that I omitted, but I'm sure this could be suppressed easily if desired. Anyhow, this seems to be working properly - both the shiny application and Rmarkdown application are interactive, just as when launched from RStudio - but if you had something else in mind please clarify.

like image 134
nrussell Avatar answered Oct 04 '22 19:10

nrussell