6
votes

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) enter image description here

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.

1
I had a similar issue. After applying your solution it still broke down in the first chunk (my source file is also rather long). Turns out the best solution for me was to change the chunk options from r,include=FALSE to r global,include=FALSE. Apparently R didnt store the values from the first chunk otherwise.5th

1 Answers

6
votes

The following seems to solve the problem: change the source() function to

source("testExternalisation.R", local=environment())