Scoping/Environment struggle in R markdown

Hey,

I never observed that before. Interesting !
I knew the envir was something that one should not really touch often, but I never thought of an issue like that.
Here is simpler reprex IMO.

---
title: test
output: html_document
---

```{r}
library(xfun)
```

The extension of "test.Rmd" is `r file_ext("test.Rmd")`

```{r}
file_ext("test.Rmd")
```

Put this in test.Rmd and try render using

rmarkdown::render("test.Rmd", envir = new.env(parent = baseenv()))

You'll get the error that file_ext is not found.

To complete @atusy explanation on GH, you can consult the chapter about environment is advance R
https://adv-r.hadley.nz/environments.html#search-path

new package is attach as a parent of global env. This means having the global environment has parents seems important to R behavior if you want to use packages without explicit namespacing (with ::). I don't think you can work around that.

I would like the building of such vignettes happens in an environment that does not have the global one as parent.

Can you explain a little more why you want that ? There maybe other solution if we go back to the original need.

1 Like