error in eval(expr envir enclos) during knit in R-markdown

18,855

Solution 1

When you compile an R-markdown document the code is run inside a "clean" R Session. That means it will not have access to objects in the workspace. The R-markdown document chunks will only have access to objects created in another chunk of the document, or the same chunk.

One way around this would be to write out the workspace to a binary file

save.image("myWorkSpace.RData")

before knitting, and then in the first chunk of your R-markdown document do

load("myWorkSpace.RData")

but I don't recommend it. Much better to include the code that creates the objects in your R-Markdown document. That means the document is entirely selfcontained, increasing reproducibility.

Solution 2

I resolved the issue using this line on the top of the first chuck of the document.

knitr::opts_chunk$set(error = TRUE)

The side-effect is that the document has all log information. I am still looking for a better way to solve it!

Greetings!

Solution 3

I ran into this with knitr::opts_chunk$set(cache = TRUE) and tinkering too much with changing objects in the .Rmd.

Deleting the cache folders and knitting the document again seemed to work.

Share:
18,855
Rafael Sierra
Author by

Rafael Sierra

Just trying to understand.

Updated on June 04, 2022

Comments

  • Rafael Sierra
    Rafael Sierra almost 2 years

    I'm trying to create a document with R-markdown but the document doesn't seem to recognise the variables in my current workspace.

    Both the markdown document and the workspace are in the same working directory.

    How can I set it to use them and update the document?