-
Notifications
You must be signed in to change notification settings - Fork 383
only attach ojs_define etc if needed. #5012
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comment on the regex.
Otherwise, regarding the duplication, I think attach()
is used to put in search path what they call a database
. So when you call it twice with same name, it will add a new one and not assign the what
value to it.
You need to use assign()
for that.
So you could do
.tools_env <- attach(NULL, name = "tools:quarto")
to create the environment. This is mentioned in the help pageOne useful ‘trick’ is to use what = NULL (or equivalently a length-zero list) to create a new environment on the search path into which objects can be assigned by
assign()
- Then use
assign(..., envir = .tools_env)
when you need to add to that one
This is how I would do it.
As I remembered RStudio IDE is also using a tools:rstudio
, I checked and they are doing this kind of things, with more complex tricks for some reason (using a function to get the environment as an environment if it already exists, I don't think we need that).
Anyhow, you can see here
https://github.com/rstudio/rstudio/blob/68c31072ae07761aab3747ead8044b729cfe9cb4/src/cpp/r/R/Tools.R#L16-L22
Hope it helps
src/resources/rmd/execute.R
Outdated
# we need ojs only if markdown has ojs code cells | ||
# inspect code cells for spaces after line breaks | ||
|
||
needs_ojs <- grepl("\n[[:space:]]*```+\\{ojs\\}", markdown) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW this is the pattern we use in knitr to detect chunks
https://github.com/yihui/knitr/blob/1239f36c5dee8b0e029958a793ae71558902f52c/R/pattern.R#L32
^[\t >]*```+\\s*\\{([a-zA-Z0-9_]+( *[ ,].*)?)\\}\\s*$
A bit more complex regex but I think this is because it covers more case in knitr; like options in chunk header and we match on line. So seems good.
Only two comments on the pattern:
- I think
[[:space:]]
covers new line so you don't need\n
before. - Are we really strict on
{ojs}
exactly ? Nothing else can be in the{ }
? We support a bit more in knitr- for example I think a space works currently like
```{ojs }
works - Could there be any options ?
- for example I think a space works currently like
Just sharing as this could breaks some usage (that we could see as bad syntax, but it works today)
This could mean using \\{ojs( *)?\\}
or \\{ojs[^}]*\\}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could mean using
\\{ojs( *)?\\}
or\\{ojs[^}]*\\}
Either one of these is better than what I have, you're totally right.
Yes, I agree that this is how we should do that. My first goal was to hide them at all because Jenny needed a clean search() result for the book she's writing. The reason I didn't do it right away is that the two calls to attach happen in separate files, and I don't know the semantics of source() well enough to understand what happens with the variable assignment and the associated scope. But I'd appreciate the help in fixing that! |
2ab6766
to
0d0c35f
Compare
…nitr to find during knitting This avoids duplication of environment, as discussed in #5012 (review)
(cc @jennybc)
This PR makes it so that
search()
doesn't get polluted by the tools:quarto attach() call, which is really only needed on OJS settings.