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)