You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Add a mechanism for packages to register new chunk options #9037
Similar to #8702, it would be very helpful if a package could register additional chunk options for autocomplete in the IDE. For example, learnr uses a number of chunk options specific to learnr exercises.
These chunk option hooks are only registered while preparing the learnr tutorial. It would be very helpful for learnr to be able to register the additional chunk options and their expected type so that they autocomplete in the same way as eval, echo, results and the like.
Reacted by Alison Presmanes Hill and Alex Rossell Hayes
I have looked a bit in how this works for knitr and how we could try to offer a registering mechanism for other packages, because I thought is could be interesting to offer this. I believe this could be powered by a knitr evolution but would need some help from the IDE.
cc @yihui and @apreshill for the R Markdown scope of this feature.
How does it work currently:
knitr register its options for autocomplete in the IDE inside knitr:::opts_chunk_attr
RStudio IDE looks at this object to now what to name to suggest, and what value auto complete to do.
opts_chunk_attr is a list with element name being the option name, and element value being hints on what to do:
The class of the chunk option value is provided as character string
if "logical", IDE will offer TRUE or FALSE
if "function" - IDE do nothing specific - no value suggested
if "character" - IDE do nothing specific - no value suggested
if "numeric" - IDE do nothing specific - no value suggested
if "list" - IDE do nothing specific - no value suggested
if element value is NULL, IDE will not show the option
if element is a list, the value of this list will be the suggested values.
I have experimented with a registering mechanism in knitr that would allow another package to add elements to this object. I tried with learn to register new options using it in .onLoad() and it works - it adds correctly the expected elements in knitr:::opts_chunk_attr. However the IDE does not get the new options - I think it means the content of opts_chunk_attr is retrieved once per session.
This is where some insight for the IDE team would be great ! I experimented a bit with rstudioapi to see if I could access the internal function defined by .rs.addFunction but only the .rs.api.* functions seems to be the one officially exported for use with rstudioapi.
Idea was to trigger from knitr an update of the autocompletion values after a new registering happens.
Anyway, I think we can work something out to make this possible without too much changes. I did this research and experimentation to have better knowledge about what it would be to support this feature. So here are my notes and thoughts!
One drawback of this ☝️ approach is that the package which register the new options needs to be loaded for the new options to be known.
Another approach suggested by @jjallaire would be for the IDE to look into the package which the output is from to find a file that would contain the new option. This would allow the IDE to know about them when the Rmd is open by just looking at the YAML header output format. That way, even if package is not loaded, autocompletion would work.
I guess It would be inst/rstudio/ like with other extensions files (like connection.dcf for the viewer pane, or templates)
Not sure what the best format would be. Could be a file that decribe the expected option (within dfc or yaml), or an Rscript that would contain the register function mentioned above.
I am sure other will have ideas. I really think this would be a great feature. 😄
In the short term, I think just adding a specific set of learnr-specific chunk options that get activated for learnr R Markdown documents would be reasonable; those could either be hard-coded in the IDE or we could steal those from learnr if it is installed and loaded. I think the more general solution might require more work than is warranted, unless we see that this becomes more common practice among other R packages.
I'd be happy to have learnr-specific chunk options activated for learnr Rmds, but I have a few (personal) packages that provide additional knitr engines or that extend knitr chunk options. bookdown's markdown extensions for theorems are another example. In other words, I'm sure that there are at least a few other packages that would take advantage of this type of feature.
bookdown's markdown extensions for theorems are another example
This may not be a great example are they will slowly retire to be replaced by Pandoc fenced div syntax, which you can already do. (documentation soon to come)
But this apart, I think also that it would be a great extension mechanism for other package. However, the example you gave are for other than r engine. So I am not sure that is the same mechanism... learnr use R engine with specific option. That is different than custom engine (that is based on R but engine is not engine = 'r'). I am not sure my finding above cover this. 🤔
I still think this would be interested to have a way to support more chunk options for auto completion. With Quarto and the different non-R engine, this will be also useful.
At this point, do we have any strong opinions on which of these is the best way forward?
Provide an extension in the knitr package, which would allow packages to provide arguments to knitr:::opts_chunk_attr. This has the upside of being relatively simple, but requires packages registering these arguments to be loaded. (In practice, wouldn't we expect these packages to be loaded anyhow?)
Allow packages to define their chunks and options in a file like inst/rstudio/chunk-options.json. This is somewhat less discoverable by package authors outside of RStudio, but would work in the aforementioned case where the associated packages are installed but not loaded.
...but requires packages registering these arguments to be loaded. (In practice, wouldn't we expect these packages to be loaded anyhow?)
AFAIK, registering chunk options necessarily requires loading the package in some capacity. Most packages use an .onLoad() hook or an explicit user gesture like foobar::use_knitr_engine(), but some might register options via a custom output format.
Regardless, I think that registering chunk options via an in-session call to a knitr function is a good middle ground. It's additionally nice that it avoids the need to detect when a package is in use and makes the chunk options easier to use in other non-RStudio contexts.
That said, I do like the idea of organizing the options in a machine-readable file (rather than in R code). I wonder if the feature in knitr could register chunk options from a chunk-options.json in a well-known package location when the package is loaded?
I like this idea. I think it would be indeed possible to make this available through knitr.
I wonder if the feature in knitr could register chunk options from a chunk-options.json in a well-known package location when the package is loaded?
I don't think this is yet something we do in knitr (I mean reading from a package file). @gadenbuie How would that work ? Would knitr be require to look through all package loaded ? Or do you suggest a function to call by package author in their onLoad() statement (e.g. register_options()), that would by default look into a chunk-options.json in the package ?
@kevinushey am I correct that RStudio would still read knitr:::opts_chunk_attr. that would have the package specific option ? We could also provide a new specific function that does return something similar but improved (if needed ?) Positron would need to do the same right ?
I don't think this is yet something we do in knitr (I mean reading from a package file). @gadenbuie How would that work ? Would knitr be require to look through all package loaded ? Or do you suggest a function to call by package author in their onLoad() statement (e.g. register_options()), that would by default look into a chunk-options.json in the package ?
All of those are good options. I had in mind something like knitr adding package event hooks that would automatically register chunk options when a package with chunk-options.json in the right place is loaded or attached. That said, I like the idea of calling a register_options() that would default to using that JSON file in the .onLoad() package hook. That mechanism is more direct and less magical while still nudging people toward a useful convention.
@kevinushey am I correct that RStudio would still read knitr:::opts_chunk_attr. that would have the package specific option ? We could also provide a new specific function that does return something similar but improved (if needed ?) Positron would need to do the same right ?
Yeah, that's what I was thinking too.
I think it makes sense for packages to include their chunk options as a file -- this gives us a bit more flexibility in how the chunk options are read. knitr could use package event hooks (as @gadenbuie said), but it also leaves the door open for IDEs like RStudio / Positron to index those files even without requiring those packages to be loaded. In effect, I think it would allow for the best user experience across disparate environments where R might be invoked / embedded / used.
Reacted by Garrick Aden-Buie and Christophe Dervieux
Thanks for the package event hook. I always forgot this exists. Though I agree with you that more direct registration could be easier to understand. Explicit registration vs implicit. But magical feature are good too !
I think knitr could be the place for this update. I'll think about it and prototype something to discuss with yihui. Unless one of you wants to give it a shot as a PR in knitr.
Activity
cderv commentedon Mar 4, 2021
I have looked a bit in how this works for knitr and how we could try to offer a registering mechanism for other packages, because I thought is could be interesting to offer this. I believe this could be powered by a knitr evolution but would need some help from the IDE.
cc @yihui and @apreshill for the R Markdown scope of this feature.
How does it work currently:
knitr:::opts_chunk_attropts_chunk_attris a list with element name being the option name, and element value being hints on what to do:"logical", IDE will offer TRUE or FALSE"function"- IDE do nothing specific - no value suggested"character"- IDE do nothing specific - no value suggested"numeric"- IDE do nothing specific - no value suggested"list"- IDE do nothing specific - no value suggestedNULL, IDE will not show the optionI have experimented with a registering mechanism in knitr that would allow another package to add elements to this object. I tried with learn to register new options using it in
.onLoad()and it works - it adds correctly the expected elements inknitr:::opts_chunk_attr. However the IDE does not get the new options - I think it means the content ofopts_chunk_attris retrieved once per session.This is where some insight for the IDE team would be great ! I experimented a bit with rstudioapi to see if I could access the internal function defined by
.rs.addFunctionbut only the.rs.api.*functions seems to be the one officially exported for use with rstudioapi.Idea was to trigger from knitr an update of the autocompletion values after a new registering happens.
Anyway, I think we can work something out to make this possible without too much changes. I did this research and experimentation to have better knowledge about what it would be to support this feature. So here are my notes and thoughts!
Sources:
knitr
RStudio IDE
rstudio/src/cpp/session/modules/SessionAuthoring.R
Lines 19 to 21 in b8e2427
rstudio/src/cpp/session/modules/SessionRCompletions.R
Lines 3273 to 3302 in b8e2427
Experimentation
cderv commentedon Mar 12, 2021
One drawback of this ☝️ approach is that the package which register the new options needs to be loaded for the new options to be known.
Another approach suggested by @jjallaire would be for the IDE to look into the package which the output is from to find a file that would contain the new option. This would allow the IDE to know about them when the Rmd is open by just looking at the YAML header output format. That way, even if package is not loaded, autocompletion would work.
I guess It would be
inst/rstudio/like with other extensions files (likeconnection.dcffor the viewer pane, or templates)Not sure what the best format would be. Could be a file that decribe the expected option (within dfc or yaml), or an Rscript that would contain the register function mentioned above.
I am sure other will have ideas. I really think this would be a great feature. 😄
kevinushey commentedon Mar 19, 2021
In the short term, I think just adding a specific set of
learnr-specific chunk options that get activated forlearnrR Markdown documents would be reasonable; those could either be hard-coded in the IDE or we could steal those fromlearnrif it is installed and loaded. I think the more general solution might require more work than is warranted, unless we see that this becomes more common practice among other R packages.gadenbuie commentedon Mar 19, 2021
I'd be happy to have learnr-specific chunk options activated for learnr Rmds, but I have a few (personal) packages that provide additional knitr engines or that extend knitr chunk options. bookdown's markdown extensions for theorems are another example. In other words, I'm sure that there are at least a few other packages that would take advantage of this type of feature.
cderv commentedon Mar 19, 2021
This may not be a great example are they will slowly retire to be replaced by Pandoc fenced div syntax, which you can already do. (documentation soon to come)
But this apart, I think also that it would be a great extension mechanism for other package. However, the example you gave are for other than r engine. So I am not sure that is the same mechanism...
learnr use R engine with specific option. That is different than custom engine (that is based on R but engine is not
engine = 'r'). I am not sure my finding above cover this. 🤔I'll look further.
github-actions commentedon Sep 16, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.
cderv commentedon Sep 16, 2021
I still think this would be interested to have a way to support more chunk options for auto completion. With Quarto and the different non-R engine, this will be also useful.
github-actions commentedon Mar 16, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.
17 remaining items
kevinushey commentedon Sep 23, 2024
At this point, do we have any strong opinions on which of these is the best way forward?
Provide an extension in the
knitrpackage, which would allow packages to provide arguments toknitr:::opts_chunk_attr. This has the upside of being relatively simple, but requires packages registering these arguments to be loaded. (In practice, wouldn't we expect these packages to be loaded anyhow?)Allow packages to define their chunks and options in a file like
inst/rstudio/chunk-options.json. This is somewhat less discoverable by package authors outside of RStudio, but would work in the aforementioned case where the associated packages are installed but not loaded.gadenbuie commentedon Sep 23, 2024
AFAIK, registering chunk options necessarily requires loading the package in some capacity. Most packages use an
.onLoad()hook or an explicit user gesture likefoobar::use_knitr_engine(), but some might register options via a custom output format.Regardless, I think that registering chunk options via an in-session call to a knitr function is a good middle ground. It's additionally nice that it avoids the need to detect when a package is in use and makes the chunk options easier to use in other non-RStudio contexts.
That said, I do like the idea of organizing the options in a machine-readable file (rather than in R code). I wonder if the feature in knitr could register chunk options from a
chunk-options.jsonin a well-known package location when the package is loaded?cderv commentedon Sep 24, 2024
I like this idea. I think it would be indeed possible to make this available through knitr.
I don't think this is yet something we do in knitr (I mean reading from a package file). @gadenbuie How would that work ? Would knitr be require to look through all package loaded ? Or do you suggest a function to call by package author in their
onLoad()statement (e.g.register_options()), that would by default look into achunk-options.jsonin the package ?@kevinushey am I correct that RStudio would still read
knitr:::opts_chunk_attr.that would have the package specific option ? We could also provide a new specific function that does return something similar but improved (if needed ?) Positron would need to do the same right ?gadenbuie commentedon Sep 24, 2024
All of those are good options. I had in mind something like knitr adding package event hooks that would automatically register chunk options when a package with
chunk-options.jsonin the right place is loaded or attached. That said, I like the idea of calling aregister_options()that would default to using that JSON file in the.onLoad()package hook. That mechanism is more direct and less magical while still nudging people toward a useful convention.kevinushey commentedon Sep 24, 2024
Yeah, that's what I was thinking too.
I think it makes sense for packages to include their chunk options as a file -- this gives us a bit more flexibility in how the chunk options are read.
knitrcould use package event hooks (as @gadenbuie said), but it also leaves the door open for IDEs like RStudio / Positron to index those files even without requiring those packages to be loaded. In effect, I think it would allow for the best user experience across disparate environments where R might be invoked / embedded / used.cderv commentedon Sep 25, 2024
Thanks for the package event hook. I always forgot this exists. Though I agree with you that more direct registration could be easier to understand. Explicit registration vs implicit. But magical feature are good too !
I think knitr could be the place for this update. I'll think about it and prototype something to discuss with yihui. Unless one of you wants to give it a shot as a PR in knitr.
jthomasmock commentedon Jan 15, 2025
@cderv -- was there any progress made on this in
knitr?cderv commentedon Jan 16, 2025
Nope - not yet. Not something we priorized on knitr side yet. Happy to discuss this though to make this happen faster if needed.
Thanks for the reminder!