I have the following Rmd
file I called test.Rmd
:
---
title: \"test\"
output: html_document
---
```{r}
print(y)
```
```{r}
x <- \"d
Your two first examples fail for different reasons. To understand both failures, it's first important to know a bit about how code chunks are evaluated by knitr and rmarkdown.
When you call rmarkdown::render()
on your file, each code chunk is ultimately evaluated by a call to evaluate::evaluate()
. In terms of its evaluation behavior and scoping rules, evaluate()
behaves almost exactly like the base R function eval()
.
(Where evaluate::evaluate()
differs most from eval()
is in how it handles the output of each evaluated expression. As explained in ?evaluate
, in addition to evaluating the expression passed as its first argument, it "captures all of the information necessary to recreate the output as if you had copied and pasted the code into an R terminal". That info includes plots and warning and error messages, which is why it's so handy in a package like knitr!)
In any case, the eventual call to evaluate()
, from within the function knitr:::block_exec()
, looks something like this
evaluate::evaluate(code, envir = env, ...)
in which:
code
is a vector of character strings giving the (possibly multiple) expressions making up the current chunk.
env
is value that you supplied the envir
formal argument in your original call to rmarkdown::render()
.
In your first example, envir
is a list, not an environment. When that is the case, evaluation is carried out in a local environment created by the function call. Unresolved symbols (as documented in both ?eval
and ?evaluate
) are looked for first in the list passed a envir
and then in the chain of environments beginning with that given by the enclos
argument. Assignments, crucially, are local to the temporary evaluation environment, which goes out of existence once the function call is complete.
Because evaluate()
operates, one at a time, on a character vector of expressions, when envir
is a list, variables created in one of those expression won't be available for use in the subsequent expressions.
When the envir
argument to rmarkdown::render()
is a list, your code block ultimately gets evaluated by a call like this:
library(evaluate)
code <- c('x <- "don\'t you ignore me!"',
'print(x)')
env <- list(y = 1:10)
evaluate(code, envir = env)
## Or, for prettier printing:
replay(evaluate(code, envir = env))
## > x <- "don't you ignore me!"
## > print(x)
## Error in print(x): object 'x' not found
The effect is exactly the same as if you did this with eval()
:
env <- list(y =1 :10)
eval(quote(x <- "don't you ignore me"), envir = env)
eval(quote(x), envir = env)
## Error in eval(quote(x), envir = env) : object 'x' not found
When envir=
is an environment returned by as.environment(list())
, you get errors for a different reason. In that case, your code block ultimately gets evaluated by a call like this:
library(evaluate)
code <- c('x <- "don\'t you ignore me!"',
'print(x)')
env <- as.environment(list(y = 1:10))
evaluate(code, envir = env)
## Or, for prettier printing:
replay(evaluate(code, envir = env))
## > x <- "don't you ignore me!"
## Error in x <- "don't you ignore me!": could not find function "<-"
## > print(x)
## Error in print(x): could not find function "print"
As you've noted, this fails because as.environment()
returns an environment whose enclosing environment is the empty environment (i.e. the environment returned by emptyenv()
). evaluate()
(like eval()
would) looks for the symbol <-
in env
and, when it doesn't find it there, starts up the chain of enclosing environments which, here, don't contain any match. (Recall also that when envir
is an environment, rather than a list, the enclos
argument is not used.)
To do what you want, you'll need to create an environment that: (1) contains all of the objects in your list and that; (2) has as its enclosing environment the parent environment of your call to render()
(i.e. the environment in which a call to render()
is normally evaluated). The most succinct way to do that is to use the nifty list2env()
function, like so:
env <- list2env(list(y="hello"), parent.frame())
render('test.Rmd', output_format = "html_document",
output_file = 'test.html',
envir = env)
Doing so will result in your code chunks being evaluated by code like the following, which is what you want:
library(evaluate)
code <- c('x <- "don\'t you ignore me!"',
'print(x)')
env <- list2env(list(y = 1:10), envir = parent.frame())
evaluate(code, envir = env)
replay(evaluate(code, envir = env))
## > x <- "don't you ignore me!"
## > print(x)
## [1] "don't you ignore me!"