I can't seem to get R markdown/knitr to see/use objects in my global environment in R.
From what I've read knitr should use the global environment as standard but every one of my objects I include in a code chunk returns the error
## Error: object 'XXX' not found
Am I missing something really simple here?
Do I need to manually load objects from the global environment first?
Thanks in advance
Marty
knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.
When a user starts a new session in R, the R system creates a new environment for objects created during that session. This environment is called the global environment.
RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.
If you've already saved the object(s) to a file, then one clean approach for markdown purposes is as follows:
if(file.exists("rfModel.Rda")){
load("rfModel.Rda")} else {
modFit <- train(class~.,method="rf",data=train)
}
This effectively bypasses the lengthy model build time by only building it if it does not exist as an object yet, so that it preserves reproducibility. This is similar to the cache idea, but is more generalizable IMHO.
It sounds like you want the same code to work with both knitr and your global environment. This can be useful when building complicated Rmd files that require testing during construction.
The issue lies in that knitr uses the local folder when you press knit, and does not look for the project home folder (i.e. your Rproj - I am assuming you use relative paths). So when you go to run code it only works for one or the other. The approach around this is to write code in your Rmd using relative paths to the project folder (as you would in a normal R script), and redirect knitr to use the project home folder. To do this insert the following code at the top of your rmd script.
```{r setup, include=FALSE}
library(knitr)
dd <- getwd()
knitr::opts_knit$set(root.dir = paste0(dd,'/../../'))
knitr::opts_chunk$set(cache.path = paste0(dd,'/cache/'))
knitr::opts_chunk$set(fig.path = paste0(dd,'/figures/'))
```
This code does the following:
Happy coding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With