Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr global environment

Tags:

r

knitr

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

like image 966
marty_c Avatar asked Mar 18 '14 19:03

marty_c


People also ask

What is the purpose of knitr?

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.

What is global environment in RStudio?

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.

What is knitr in rmarkdown?

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.


2 Answers

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.

like image 161
ddunn801 Avatar answered Nov 03 '22 17:11

ddunn801


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:

  • first, finds the current directory for your rmd.
  • second, sets the project root directory. I keep my files two folders in, hence the '/../../' , this will need to be adjusted for your folder structure.
  • third, you need to set the cache folder path manually, as the default setting no longer works, hence cache wont work.
  • finally, do the same for the figures folder, as again you need to overwrite the default.

Happy coding.

like image 33
pdbentley Avatar answered Nov 03 '22 18:11

pdbentley