26

I wonder whether I can use knitr markdown to just create a report on the fly with objects stemming from my current workspace. Reproducibility is not the issue here. I also read this very fine thread here.

But still I get an error message complaining that the particular object could not be found.

1) Suppose I open a fresh markdown document and save it.

2) write a chunk that refers to some lm object in my workspace. call summary(mylmobject)

3) knitr it.

Unfortunately the report is generated but the regression output cannot be shown because the object could not be found. Note, it works in general if i just save the object to .Rdata and then load it directly from the markdown file.

Is there a way to use objects in R markdown that are in the current workspace? This would be really nice to show non R people some output while still working.

Community
  • 1
  • 1
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207

4 Answers4

36

RStudio opens a new R session to knit() your R Markdown file, so the objects in your current working space will not be available to that session (they are two separate sessions). Two solutions:

  1. file a feature request to RStudio, asking them to support knitting in the current R session instead of forcibly starting a new session;
  2. knit manually by yourself: library(knitr); knit('your_file.Rmd') (or knit2html() if you want HTML output in one step, or rmarkdown::render() if you are using R Markdown v2)
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 5
    I feel like option 1 is counter to the paradigm of reproducible research. – Brandon Bertelsen Nov 01 '12 at 02:50
  • 5
    @BrandonBertelsen you are right, and it is exactly the reason why RStudio opens a new R session every time; by comparison, Emacs/ESS calls Sweave/knitr in the current R session. There are advantages of using the existing R session, though: 1. can be faster; 2. helps us debug the code (e.g. check the objects generated from the source document) – Yihui Xie Nov 01 '12 at 17:09
  • Curious to know if there has been any changes in Rstudio or Knitr since this was posted in 2012? – Thomas Speidel Apr 06 '15 at 16:53
  • 1
    @ThomasSpeidel Sorry, but not in this aspect. – Yihui Xie Apr 06 '15 at 22:28
  • 1
    Thanks for the answer. Actually in the newest versions of RStudio/Rmarkdown I could not run it as it is in your answer but from your hint I found the solution quite quickly: `library(markdown); render('your_file.Rmd')` (in case you want to update). Thanks. – lrnzcig Mar 07 '16 at 16:51
29

Might be easier to save you data from your other session using:

save.image("C:/Users/Desktop/example_candelete.RData")

and then load it into your MD file:

load("C:/Users/Desktop/example_candelete.RData")
mmann1123
  • 5,031
  • 7
  • 41
  • 49
  • actually that's what I do now. – Matt Bannert Nov 01 '12 at 09:57
  • 3
    The problem is I have a session that's so large it takes a couple of minutes to load. I get where Rstudio's coming from reproducibility-wise, but if we want to use markdown to make a presentable report with large data, we need to knit manually. – Wayne Jun 25 '14 at 19:21
  • 2
    You can choose what you save using save(mylmobject) instead of save.image() – Julien Colomb Oct 04 '17 at 15:38
1

The Markdownreports package is exactly designed for parsing a markdown document on the fly.

bud.dugong
  • 689
  • 7
  • 16
0

As Julien Colomb commented, I've found the best thing to do in this situation is to save the large objects and then load them explicitly while I'm tailoring the markdown. This is a must if your data is coming through an ODBC and you don't want to run the entire queries repeatedly as you tinker with fonts and themes.

CClarke
  • 503
  • 7
  • 18