8

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")
Community
  • 1
  • 1
Rorschach
  • 31,301
  • 5
  • 78
  • 129

2 Answers2

5

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.

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • thanks a lot! For some reason I was just assuming the launch.browser was always true (facepalm). I haven't tried littler yet, but it looks really good. I need this to be shared with windows as well for now though. While trying this, I found that whilst rendering an rmarkdown I needed to specify the file name with `default_file` and not `file` for some reason. – Rorschach Jul 21 '15 at 15:52
  • @nongkrong That's great, HTH. I didn't test this with Rscript, but it should work with that interface as well; I think the `launch.browser` argument is the critical takeaway here anyways. – nrussell Jul 21 '15 at 17:58
0

Thanks @nrussell, your example helped me a lot!

Here is my solution for launching an interactive markdown doc on Windows 10.

REM Change to correct directory
cd "C:\Users\me\Documents\project_folder\"

REM Print files available (not required, but helpful)
dir

REM Point system to R's Pandoc with Rscript then launch
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" -e ^
"Sys.setenv(RSTUDIO_PANDOC='C:/Users/me/Documents/RStudio/bin/pandoc'); rmarkdown::run(file = 'myInteractiveMarkdown.Rmd', shiny_args = list(launch.browser = TRUE, host = '127.0.0.1'))"

I found I got two errors initially:

  1. When I didn't point the system env to R's pandoc it gave the error message error pandoc version 1.12.3 or higher is required which I solved using the instructions here
  2. When I set the port in the shiny_args, subsequent executions of the bat file would get an error that the port was already busy
lifedroid
  • 164
  • 2
  • 7