Skip to content

Feature Request: Add a mechanism for packages to register new chunk options #9037

Open

Description

@gadenbuie

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.

image

Activity

  1. added
    autocompletion
    on Mar 3, 2021
  2. cderv commented on Mar 4, 2021

    @cderv
    Collaborator

    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!

    Sources:

    knitr

    RStudio IDE

    # starting with v0.4.2 this is avavilable directly from knitr
    if (.rs.getPackageVersion("knitr") >= "0.4.2")
    return(knitr:::opts_chunk_attr)

    opts <- knitr:::opts_chunk_attr
    if (activeArg %in% names(opts))
    {
    # Options to fill in the various if statements
    foundCompletions <- FALSE
    results <- character()
    quote <- TRUE
    if (is.list(opts[[activeArg]]))
    {
    foundCompletions <- TRUE
    potentials <- unlist(opts[[activeArg]])
    results <- .rs.selectFuzzyMatches(potentials, token)
    quote <- TRUE
    }
    if (identical(opts[[activeArg]], "logical"))
    {
    foundCompletions <- TRUE
    potentials <- c(TRUE, FALSE)
    results <- .rs.selectFuzzyMatches(potentials, token)
    quote <- FALSE
    }
    if (foundCompletions)
    return(.rs.makeCompletions(token = token,
    type = .rs.acCompletionTypes$ARGUMENT,
    quote = quote,
    results = results))
    }

    Experimentation

  3. cderv commented on Mar 12, 2021

    @cderv
    Collaborator

    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. 😄

  4. kevinushey commented on Mar 19, 2021

    @kevinushey
    Collaborator

    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.

  5. gadenbuie commented on Mar 19, 2021

    @gadenbuie
    MemberAuthor

    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.

  6. cderv commented on Mar 19, 2021

    @cderv
    Collaborator

    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'll look further.

  7. github-actions commented on Sep 16, 2021

    @github-actions

    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.

  8. added
    stale
    on Sep 16, 2021
  9. cderv commented on Sep 16, 2021

    @cderv
    Collaborator

    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.

  10. removed
    stale
    on Sep 16, 2021
  11. moved this from Backlog to Need External Work in R Markdown Team Projectson Nov 2, 2021
  12. github-actions commented on Mar 16, 2022

    @github-actions

    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.

  13. 17 remaining items

  14. added this to the Autumn Crocus milestone on Jun 3, 2024
  15. self-assigned this
    on Sep 23, 2024
  16. kevinushey commented on Sep 23, 2024

    @kevinushey
    Collaborator

    At this point, do we have any strong opinions on which of these is the best way forward?

    1. 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?)

    2. 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.

  17. gadenbuie commented on Sep 23, 2024

    @gadenbuie
    MemberAuthor

    ...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?

  18. cderv commented on Sep 24, 2024

    @cderv
    Collaborator

    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 ?

  19. gadenbuie commented on Sep 24, 2024

    @gadenbuie
    MemberAuthor

    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.

  20. kevinushey commented on Sep 24, 2024

    @kevinushey
    Collaborator

    @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.

  21. cderv commented on Sep 25, 2024

    @cderv
    Collaborator

    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.

  22. removed their assignment
    on Dec 5, 2024
  23. jthomasmock commented on Jan 15, 2025

    @jthomasmock
    Contributor

    @cderv -- was there any progress made on this in knitr?

  24. cderv commented on Jan 16, 2025

    @cderv
    Collaborator

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autocompletionenhancement

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions