Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether shiny runs the R code

Tags:

r

rstudio

shiny

rnw

I would like to run R code in two locations, in an Rnw file and as an interactive shiny R markdown document.

Thus, what I need, since interactive shiny components do not work in Rnw files, is a code snippet in R that detects whether to load the interactive code or not.

This seems to work, but it feels like a quick hack:

if (exists("input")) { # input is provided by shiny
    # interactive components like renderPlot for shiny
} else {
    # non-interactive code for Rnw file
}

Is there a stable solution or something like a global variable that I can access that says whether shiny is running at the moment? Or should I check whether the shiny package is loaded?

What's safest?

like image 993
Xiphias Avatar asked Sep 27 '15 10:09

Xiphias


1 Answers

There is now a function shiny::isRunning().

like image 188
dk. Avatar answered Sep 28 '22 11:09

dk.