Sourcing external R scripts that rely on a variable set in the master/main Shiny document
knitr, r, r-markdown, rstudio, shiny
Solution
The following seems to solve the problem: change the source() function to
source("testExternalisation.R", local=environment())
Problem
I have just installed the preview release of RStudio, Version 0.98.864 (May 24th, 2014). Also, I have installed the development versions of knitr and shiny, via ``` devtools::install_github(c("yihui/knitr", "rstudio/shiny")) ``` I am trying to create a Shiny Document (using Rstudio dialog with Shiny Document template) to: 1) Set the value of a variable, e.g. x. The following code is the contents of my Rmd file: (I have to place this as an image as the formatting is playing up) 2) Source an R script (testExternalisation.R) in the same directory that uses the variable, x, set in the .Rmd file; code as follows: ``` y <- x + 3 ``` However, on running the .Rmd document I get the following message: "Error: object 'x' not found. Now, if I remove the first 3 lines of my .Rmd file, i.e. the front matter for a Shiny html_document, I am perfectly able to knit the resulting .Rmd document. Is there a solution for sourcing external scripts in Shiny Documents that rely on variables in the calling Shiny Doc? Edit: When knitting the document, environment() returns `<environment: R_GlobalEnv>` for both the .Rmd and .R files. However, when running the Shiny document, the .Rmd environment is `<environment: 0x05828968>` and source environment is `<environment: R_GlobalEnv>`, so I need to ensure the two are using the same environment ... Thanks.