-
-
Notifications
You must be signed in to change notification settings - Fork 872
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
opts_current$set should error #1798
Comments
…t we can disable the $set() method (#1798)
|
@yihui what's the best way around this if I have a legitimate use of |
@yihui I figured out a way around this that will work. But I do want to point out that the documentation for
Relative to this advice, locking the binding is quite an aggressive change to make in a minor version release. |
@gadenbuie Please feel free to unlock it by yourself with I'll update the documentation accordingly. Thanks for the reminder! BTW, I'm curious about your use of |
Thanks @yihui! I appreciate your open door philosophy 😄 I read the comments in the "different method" commit and was worried that My use of
Which renders to the following since
I found a clever way around this by using a I'm now using a package-scoped global variable to hold the flag, which is fine. But I preferred |
Thanks for explaining the use case! That sounds reasonable to me. Now I feel this change is indeed too aggressive. Let me think more about it and decide whether this should be a warning (with a hint in the message on how to unlock it) instead of an error. Sorry about the hassle! |
I would recommend using It allows getting from a specific label or listing in the evaluated order. ``` {r x, echo=TRUE}
print(1)
```
## get chunk options of the "x" chunk
```{r, echo=TRUE}
attr(knitr::knit_code$get("x"), "chunk_opts")
```
## get latest chunk (itself)
```{r}
tail(knitr::knit_code$get(), 1L)
``` |
Thanks @atusy but that's not my use case. To clarify, in my example above, the chunk option |
I believe the underlying issue is also important to fix (adapt) as I understand
|
I agree. Also, inline chunk may possibly gain chunk options in the future if we implement new syntax like |
FWIW @yihui it seems one of the usage is to be able to tweak some chunk options from within another hook Is there another design pattern to be able to do such modification of |
@cderv Normally hooks shouldn't need the knitr::opts_hooks$set(fig.env = function(options) {
if (using_some_flextable_feature()) {
# I wonder if they meant `results` instead of `output` here
options$output = 'asis'; options$fig.env = NULL
}
options
})
The PR #2292 will do it. |
I think they meant output because it is a It seems they are trying to fix something related to Quarto rendering and output.
I think what this all means is that: If you want to tweak options, then you need to modify options hooks. |
Is the I've tried the following:
and don't see the alt. |
@howardbaek setting option using I believe your usecase is jhudsl/ottrpal#119 - maybe we can help with that and understand if there. Example of how to modify options with options hook: ---
title: test
output: html_document
---
```{r}
knitr::opts_hooks$set(engine = function(options) {
# do nothing for non R chunk
if (tolower(options$engine) != "r") return(options)
# if not fig.alt, then use a default one
if(is.null(options$fig.alt)) {
options$fig.alt = "individual slide notes"
}
options
})
```
```{r}
knitr::opts_current$get("fig.alt")
``` @yihui regarding jhudsl/ottrpal#119 use case, maybe you have insights and advices on how to modify options from within a function run in a chunk ? Question has also been asked in Communityso we could maybe follow up there too. (https://community.rstudio.com/t/setting-fig-alt-using-knitr-knit-hooks/174159) |
There is no way to modify chunk options from within the current code chunk. Currently, the only way to modify chunk options is via option hooks (like @cderv's example above) which occurs before a chunk is evaluated. When a chunk is being evaluated or after it is evaluated, it's no longer possible to modify its chunk options. Calling |
@cderv Thanks for the quick response. Your code sheds some light, but brings up followup questions. I did try options hooks, but this didn't work:
I don't fully understand how to use Also, regarding jhudsl/ottrpal#119, I inserted your code inside
and experimented inside a Rmd:
But the last line prints NULL, so don't think its working inside a function? BTW, how did you insert Rmd code chunks into this issue without having to put a |
You set an option hook on
So you can't set a default
This is why I gave you the example with setting the option hook on the You could set the option hook to anything that has meaning for your issue. For example you could want to set a default
You need to run the hook function once before it can apply on a chunk. As you put it in your function, called the function and in the same chunk test that the Setting up a option hook will apply to the whole documents, and all chunks. Usually best way to set them up are:
Hope it helps
Just follow the issue guide: https://yihui.org/issue/#please-format-your-issue-correctly |
Yes, this helps a lot. What I'm trying to implement is not possible in knitr. I'll have to figure out a workaround fix. |
Christophe Dervieux (4): escape `fig.alt` for HTML output (#2291) close #2295: use a warning instead of an error for `opts_current` locking (#2296) Improve convert_chunk_header() for YAML options (#2300) Probably need to use the higher dependency gridSVG that requires XML not available on R 3.6.0 Pedro Faria (1): close #2226: add an error handler to improve YAML error message (#2294) Yihui Xie (12): start the next version give users the key to unlock opts_current in the error message (yihui/knitr#1798) simplify this clumsy decade-old JS code the new version of papaja is on CRAN now: crsh/papaja#566 fix #2288: preserve the minus sign when formatting negative numbers -10^n revise c1c9a5766a632fcb535fc432c7be401cba965c22 and ca7172752b6a3b4a5d2d9492e21844adad20198d: just don't use simple progress bars in RStudio, no matter where they are lingglosses v0.0.7 is on CRAN fix rstudio/rmarkdown#2518: when fig.num is a multiple of fig.ncol, add \newline to the last subfigure restore `opts_current` after each code chunk, and also after `knit()` exits (#2292) fix #2302: wrap figure output in raw latex blocks for Markdown output (#2303) revert 4307aedad27ec04defa35bf21d578feecd10665e and fix #2302 by escaping % instead CRAN release v1.45
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. |
As per #841 (wontfix), you're not going to support changing the current chunk options.
However,
opts_current$set
allows modifying the options and doesn't complain about it either.It even succeeds in changing an option, but at least for
results='asis'
this leaves opts_current in an inconsistent state because the updated option is not honored.Note how knitting the MWE, the first section reports
results = markup
and thenresults = asis
despite thecat(...)
being captured and commented instead.Since programmatically updating the current chunk options will not be supported, I suggest to at least
stop()
when code is trying to callopts_current$set()
.MWE
By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: