Determine if user provided "results" chunk option in knitr chunk

I'm writing a knitr chunk hook and would like to check whether the user has explicitly set the results chunk option.

For "non-standard" chunk options, I can use knitr::opts_current$get("new_option") to determine whether or not the user explicitly provided the chunk option. If they did, opts_current$get() returns the value; if they did not, opts_current$get() returns NULL.

rmd_new_option <- paste(
    "```{r new_option = 'hello'}",
    "knitr::opts_current$get(\"new_option\")",
    "```",
    "",
    "```{r}",
    "knitr::opts_current$get(\"new_option\")",
    "```",
    "",
    sep = "\n"
)

writeLines(knitr::knit(text = rmd_new_option))
#> 
#> ```r
#> knitr::opts_current$get("new_option")
#> #> [1] "hello"
#> ```
#> 
#> 
#> ```r
#> knitr::opts_current$get("new_option")
#> #> NULL
#> ```

Unfortunately, the default value of the results chunk option is propagated to opts_current$get(), so this approach doesn't work. Instead it returns the default value:

rmd_results <- paste(
    "```{r}",
    "knitr::opts_chunk$get(\"results\")",
    "knitr::opts_current$get(\"results\")",
    "```",
    "",
    sep = "\n"
)

writeLines(knitr::knit(text = rmd_results))
#> 
#> ```r
#> knitr::opts_chunk$get("results")
#> #> [1] "markup"
#> knitr::opts_current$get("results")
#> #> [1] "markup"
#> ```

Is there another approach that directly returns the exact user-provided chunk options?

As you want this behavior for options that have default values, you can't to that I think because options are read and merged with default one. So I think this would require something new in knitr to get the user provided option value. Would you open a feature request in GH repo to discuss this according to your use case ?

Default values are :

str(knitr::opts_chunk$get())
#> List of 54
#>  $ eval         : logi TRUE
#>  $ echo         : logi TRUE
#>  $ results      : chr "markup"
#>  $ tidy         : logi FALSE
#>  $ tidy.opts    : NULL
#>  $ collapse     : logi TRUE
#>  $ prompt       : logi FALSE
#>  $ comment      : chr "#>"
#>  $ highlight    : logi TRUE
#>  $ strip.white  : logi TRUE
#>  $ size         : chr "normalsize"
#>  $ background   : chr "#F7F7F7"
#>  $ cache        : logi FALSE
#>  $ cache.path   : chr "reprex_reprex_cache/commonmark/"
#>  $ cache.vars   : NULL
#>  $ cache.lazy   : logi TRUE
#>  $ dependson    : NULL
#>  $ autodep      : logi FALSE
#>  $ cache.rebuild: logi FALSE
#>  $ fig.keep     : chr "high"
#>  $ fig.show     : chr "asis"
#>  $ fig.align    : chr "default"
#>  $ fig.path     : chr "reprex_reprex_files/figure-commonmark/"
#>  $ dev          : chr "png"
#>  $ dev.args     : NULL
#>  $ dpi          : num 96
#>  $ fig.ext      : NULL
#>  $ fig.width    : num 7
#>  $ fig.height   : num 5
#>  $ fig.env      : chr "figure"
#>  $ fig.cap      : NULL
#>  $ fig.scap     : NULL
#>  $ fig.lp       : chr "fig:"
#>  $ fig.subcap   : NULL
#>  $ fig.pos      : chr ""
#>  $ out.width    : NULL
#>  $ out.height   : NULL
#>  $ out.extra    : NULL
#>  $ fig.retina   : NULL
#>  $ external     : logi TRUE
#>  $ sanitize     : logi FALSE
#>  $ interval     : num 1
#>  $ aniopts      : chr "controls,loop"
#>  $ warning      : logi TRUE
#>  $ error        : logi TRUE
#>  $ message      : logi TRUE
#>  $ render       : NULL
#>  $ ref.label    : NULL
#>  $ child        : NULL
#>  $ engine       : chr "R"
#>  $ split        : logi FALSE
#>  $ include      : logi TRUE
#>  $ purl         : logi TRUE
#>  $ R.options    :List of 2
#>   ..$ tidyverse.quiet : logi TRUE
#>   ..$ tidymodels.quiet: logi TRUE

Created on 2020-09-15 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

I've since discovered that knitr::knit_code$get("<chunk-label>") returns the un-evaluated chunk options as in the chunk_opts attribute. Using this approach lets you directly access the chunk options that were specifically applied to a given chunk. The downside is you may have to run it through eval(paste(text = chunk_option), envir = knitr::knit_global()) first.

Using this method, you can tell the difference between globally-set options and specifically set options. Inside a knitr hook, for example, you might use options$label to get the current chunk label's code a chunk opts to compare with the values in options.

rmd_results <- paste(
  "```{r chunk-label, a_chunk_option = TRUE}",
  'knitr::opts_chunk$get("results")',
  'knitr::opts_current$get("results")',
  'attr(knitr::knit_code$get("chunk-label"), "chunk_opts")',
  "```",
  "",
  sep = "\n"
)

writeLines(knitr::knit(text = rmd_results))
#> 
#> ```r
#> knitr::opts_chunk$get("results")
#> #> [1] "markup"
#> knitr::opts_current$get("results")
#> #> [1] "markup"
#> attr(knitr::knit_code$get("chunk-label"), "chunk_opts")
#> #> $label
#> #> [1] "chunk-label"
#> #> 
#> #> $a_chunk_option
#> #> [1] TRUE
#> ```
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.