2

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?

Rafael Sierra
  • 107
  • 1
  • 2
  • 11
  • 4
    Welcome to SO. First of all you should read [here](http://stackoverflow.com/help/how-to-ask) about how to ask a good question; a good question has better changes to be solved and you to receive help. On the other hand a read of [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is also good. It explains how to create a reproducible example in R. Help users to help you by providing a piece of your data a desired output and things you have tried so far. – SabDeM Sep 27 '15 at 18:43
  • 1
    Rmarkdown is essentially the same as closing R, and reopening it clean. It will not read anything into it from your current session. If you want to load things in, you'll need to explicitly load them inside the Rmarkdown file. – jeremycg Sep 27 '15 at 19:09

4 Answers4

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.

atiretoo
  • 1,812
  • 19
  • 33
  • Thank you for your reply, @atiretoo. As a matter of fact, I actually tried that before, but I keep getting an error. However, it reads this time: "Error in readChar". I'm looking into it right now to see if I can get this to work. About creating the object in the chunks, in this particular case I'm trying to plot in a document the result of a very long processing. Either way, I'd need to load data.frames into R-markdown, which, as stated before, isn't working either. Thanks for your suggestions. I'll try to work in that direction from now on. – Rafael Sierra Sep 27 '15 at 21:35
1

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!

Joni Hoppen
  • 658
  • 5
  • 23
  • Solved my issue. Thanks! – Steven Lockton Jun 26 '17 at 18:10
  • This solution only shows the errors and continues building the document, it doesn't fix the errors themselves, so your document is likely to be incomplete (unless errors are derived from relict code). – Matt Sep 14 '17 at 16:49
1

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.

guyabel
  • 8,014
  • 6
  • 57
  • 86
0

This error can occur if you are including multiple <> within the same code block in your .Rmd file.

Matt
  • 490
  • 8
  • 19