Using "source" in a shiny app for deployment at shinyapps.io

Just sharing a little hard-won knowledge.
I am using "source()" quite a bit in a Rmarkdown+shiny, here:
https://trials.shinyapps.io/Bias-variance-smoothing-shrinking/

When running from RStudio as you develop:

  • Using source() in an R chunk in non-reactive code, the file path is relative to the main Rmarkdown document.
  • Using source() in an R chunk in reactive code, the file path is relative to the package root.

When deployed at shinyapps.io (& probably any shiny server),
source() in the non-reactive setting works without change.
But for source()'ing from a reactive setting, you need to take care. This works.

if(interactive()) {  ### Running out of RStudio locally; relative to package root, file is at
  analysisReactiveSetup = paste0('inst/T15lumpsplit/', 'analysisReactiveSetup.R')
} else  {            ### Running at shinyapps.io; location is same as the root document.
  analysisReactiveSetup =  'analysisReactiveSetup.R'
}

Then you can put this in your reactive contexts. interactive() returns TRUE when running from RStudio, FALSE when running from the shiny server.

source(analysisReactiveSetup)

Surely everyone knew this but me, ... and maybe someone else puzzled by why source() isn't working.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.