From 32881d054cafb2540b346106840231f0d49bd903 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 24 Aug 2023 16:17:45 +0200 Subject: [PATCH 1/5] Avoid depending on a knitr internal function prevent any future change in knitr that would remove this function and break quarto --- src/resources/rmd/hooks.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index 5e83e01999..e54b82dc5b 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -1,6 +1,12 @@ # hooks.R # Copyright (C) 2020-2022 Posit Software, PBC +# inline knitr:::merge_list() +merge_list <- function(x, y) { + x[names(y)] <- y + x +} + knitr_hooks <- function(format, resourceDir, handledLanguages) { knit_hooks <- list() @@ -639,7 +645,7 @@ knitr_options_hook <- function(options) { options[["message"]] = results$yaml[["warning"]] } # merge with other options - options <- knitr:::merge_list(options, results$yaml) + options <- merge_list(options, results$yaml) # set code options$code <- results$code } From 5df4b569c6498d10fe489552ee49a5f5c06e3815 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 24 Aug 2023 16:19:38 +0200 Subject: [PATCH 2/5] Correctly replace any existing dotted-knitr-option when normalizing dashed-knitr-options --- src/resources/rmd/hooks.R | 158 +++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index e54b82dc5b..da2a2b124d 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -696,85 +696,87 @@ knitr_options_hook <- function(options) { # as support all documented knitr chunk options without the user # needing to replace . with - normalize_options <- function(options) { - names(options) <- sapply(names(options), function(name) { - if (name %in% c( - # Text output - "strip-white", - "class-output", - "class-message", - "class-warning", - "class-error", - "attr-output", - "attr-message", - "attr-warning", - "attr-error", - # Paged tables - "max-print", - "sql-max-print", - "paged-print", - "rows-print", - "cols-print", - "cols-min-print", - "pages-print", - "paged-print", - "rownames-print", - # Code decoration - "tidy-opts", - "class-source", - "attr-source", - # Cache - "cache-path", - "cache-vars", - "cache-globals", - "cache-lazy", - "cache-comments", - "cache-rebuild", - # Plots - "fig-path", - "fig-keep", - "fig-show", - "dev-args", - "fig-ext", - "fig-width", - "fig-height", - "fig-asp", - "fig-dim", - "out-width", - "out-height", - "out-extra", - "fig-retina", - "resize-width", - "resize-height", - "fig-align", - "fig-link", - "fig-env", - "fig-cap", - "fig-alt", - "fig-scap", - "fig-lp", - "fig-pos", - "fig-subcap", - "fig-ncol", - "fig-sep", - "fig-process", - "fig-showtext", - # Animation - "animation-hook", - "ffmpeg-bitrate", - "ffmpeg-format", - # Code chunk - "ref-label", - # Language engines - "engine-path", - "engine-opts", - "opts-label", - # Other chunk options - "R-options")) { - sub("-", ".", name) - } else { - name + knitr_options_dashed <- c( + # Text output + "strip-white", + "class-output", + "class-message", + "class-warning", + "class-error", + "attr-output", + "attr-message", + "attr-warning", + "attr-error", + # Paged tables + "max-print", + "sql-max-print", + "paged-print", + "rows-print", + "cols-print", + "cols-min-print", + "pages-print", + "paged-print", + "rownames-print", + # Code decoration + "tidy-opts", + "class-source", + "attr-source", + # Cache + "cache-path", + "cache-vars", + "cache-globals", + "cache-lazy", + "cache-comments", + "cache-rebuild", + # Plots + "fig-path", + "fig-keep", + "fig-show", + "dev-args", + "fig-ext", + "fig-width", + "fig-height", + "fig-asp", + "fig-dim", + "out-width", + "out-height", + "out-extra", + "fig-retina", + "resize-width", + "resize-height", + "fig-align", + "fig-link", + "fig-env", + "fig-cap", + "fig-alt", + "fig-scap", + "fig-lp", + "fig-pos", + "fig-subcap", + "fig-ncol", + "fig-sep", + "fig-process", + "fig-showtext", + # Animation + "animation-hook", + "ffmpeg-bitrate", + "ffmpeg-format", + # Code chunk + "ref-label", + # Language engines + "engine-path", + "engine-opts", + "opts-label", + # Other chunk options + "R-options" + ) + # Un-normalized knitr options, and replace any existing options + for (name in knitr_options_dashed) { + if (name %in% names(options)) { + options[[sub("-", ".", name)]] <- options[[name]] + options[[name]] <- NULL } - }, USE.NAMES = FALSE) + } options } From e44956cc1be4ad3227ae57492d849d1796c4d74a Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 24 Aug 2023 16:20:29 +0200 Subject: [PATCH 3/5] Correctly alias fig-format and fig-dpi to knitr's option dev and dpi --- src/resources/rmd/hooks.R | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index da2a2b124d..e2b584021e 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -651,17 +651,18 @@ knitr_options_hook <- function(options) { } options[["yaml.code"]] <- results$yamlSource - # some aliases - if (!is.null(options[["fig.format"]])) { - options[["dev"]] <- options[["fig.format"]] - } - if (!is.null(options[["fig.dpi"]])) { - options[["dpi"]] <- options[["fig.dpi"]] - } } else { # convert any option with fig- into fig. and out- to out. options <- normalize_options(options) } + + # some aliases not normalized + if (!is.null(options[["fig-format"]])) { + options[["dev"]] <- options[["fig-format"]] + } + if (!is.null(options[["fig-dpi"]])) { + options[["dpi"]] <- options[["fig-dpi"]] + } # if there are line annotations in the code then we need to # force disable messages/warnings From d4bb531150ee0ee401c9081484a8903f2afa076b Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 29 Aug 2023 18:21:41 +0200 Subject: [PATCH 4/5] gsub is required as some options have more than one dash --- src/resources/rmd/hooks.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index e2b584021e..dae13d2f98 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -774,7 +774,7 @@ normalize_options <- function(options) { # Un-normalized knitr options, and replace any existing options for (name in knitr_options_dashed) { if (name %in% names(options)) { - options[[sub("-", ".", name)]] <- options[[name]] + options[[gsub("-", ".", name)]] <- options[[name]] options[[name]] <- NULL } } From 409137de870936d748ff1ceafb096ff43a10d648 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Thu, 31 Aug 2023 15:37:49 +0200 Subject: [PATCH 5/5] Add about knitr 1.44 normalization in comments for future us --- src/resources/rmd/hooks.R | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/resources/rmd/hooks.R b/src/resources/rmd/hooks.R index dae13d2f98..9cdd7d2e21 100644 --- a/src/resources/rmd/hooks.R +++ b/src/resources/rmd/hooks.R @@ -652,11 +652,18 @@ knitr_options_hook <- function(options) { options[["yaml.code"]] <- results$yamlSource } else { - # convert any option with fig- into fig. and out- to out. + # from knitr 1.44 onwards the normalization is done at parsing time by knitr + # for all known options c(names(opts_chunk_attr), names(opts_chunk$get())) + # so normalization here should not be necessary anymore at some point + # TODO: remove normalization here in a few years + # when 1.44 is widely used version options <- normalize_options(options) } # some aliases not normalized + # from knitr 1.44, `fig.format` and `fig.dpi` are now alias + # to `dev` and `dpi` + # TODO: remove below in a few years when 1.44 is widely used version if (!is.null(options[["fig-format"]])) { options[["dev"]] <- options[["fig-format"]] } @@ -696,7 +703,14 @@ knitr_options_hook <- function(options) { # however we want to support all existing knitr code as well # as support all documented knitr chunk options without the user # needing to replace . with - +# from knitr 1.44 onwards the normalization is done at parsing time by knitr +# for all known options c(names(opts_chunk_attr), names(opts_chunk$get())) +# so normalization here should not be necessary anymore at some point +# TODO: remove normalization here in a few years +# when 1.44 is widely used version normalize_options <- function(options) { + # TODO: knitr 1.44 store all known options as it does the normalization + # they are in `c(names(opts_chunk_attr), names(opts_chunk$get()))` knitr_options_dashed <- c( # Text output "strip-white", @@ -771,7 +785,7 @@ normalize_options <- function(options) { # Other chunk options "R-options" ) - # Un-normalized knitr options, and replace any existing options + # Un-normalize knitr options, and replace any existing options (e.g default one) for (name in knitr_options_dashed) { if (name %in% names(options)) { options[[gsub("-", ".", name)]] <- options[[name]]