Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to define the scope of inline chunks #1853

Closed
courtiol opened this issue Jun 30, 2020 · 5 comments
Closed

Question: how to define the scope of inline chunks #1853

courtiol opened this issue Jun 30, 2020 · 5 comments

Comments

@courtiol
Copy link

Dear @yihui and colleagues, this is a rather technical question (see link to RStudio community at the end)

Within a package I am working on, I am creating optional vignettes that the user can build on demand.

I would like the building of such vignettes happens in an environment that does not have the global one as parent.
So, my custom vignette building function includes something like:

vignette_env <- new.env(parent = baseenv())
rmarkdown::render(path.to.file, envir = vignette_env)

The problem, when I do that, is that the package loaded and the objects created inside knitr chunks are not visible (in scope) to inline calls that follow, although they are visible to other chunks.

I don't understand why and how to cope with this scoping issue.

I have posted a question on RStudio community with a full reprex (https://community.rstudio.com/t/scoping-environment-struggle-in-r-markdown/70709), but failed to attract any attention in more than a week.

@atusy
Copy link
Collaborator

atusy commented Jul 18, 2020

As far as I tried, I faced error with a normal chunk.

I'd recommend rendering within callr::r.
An easy way is to use reprex::reprex_render, but it is not yet available on CRAN.

I guess this is the cause.
Even if you use new.env(parent = baseenv()), search() does not change.
Also, library attaches the package to the 2nd position of search().
As a result, imported package is located between .GlobalEnv and stats.
In the example below, library(MASS) adds "package:MASS" between them.
This location is not accessible from the environment you defined.

# rmarkdown::render('~/R/example.Rmd', envir = new.env(baseenv()))
library(MASS)
environment()
## <environment: R_GlobalEnv>
parent.env(environment())
## <environment: package:MASS>
## attr(,"name")
## [1] "package:MASS"
## attr(,"path")
## [1] "/usr/local/lib/R/library/MASS"
search()
##  [1] ".GlobalEnv"        "package:MASS"      "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"

@courtiol
Copy link
Author

Thanks @atusy but I don't get you: I am fine with packages attaching envir at the second position and I don't see how this explains why chunks and inline work using different scopes. Would not that be simply a bug?

@cderv
Copy link
Collaborator

cderv commented Jul 20, 2020

@atusy FWIW we are discussing this in the community website if you want to join and share ideas
https://community.rstudio.com/t/scoping-environment-struggle-in-r-markdown/70709

@courtiol
Copy link
Author

The scoping issue does not seem to be reproduced by others, and manipulating environment can easily be avoided by using callr::r(). See previous link to community rstudio for more info.

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants