Skip to content

Commit

Permalink
Convert dashes in chunk option names to dots (#2282)
Browse files Browse the repository at this point in the history
Co-authored-by: Christophe Dervieux <christophe.dervieux@gmail.com>
  • Loading branch information
yihui and cderv authored Aug 28, 2023
1 parent 251c4e5 commit 22b2090
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.43.6
Version: 1.43.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

- Use the correct type of progress bar when rendering Quarto documents in RStudio, which takes place in RStudio's background jobs pane or build pane (thanks, @hadley, #2271).

## MAJOR CHANGES

- Dashes (`-`) in the names of all chunk options are normalized to dots (`.`) now, e.g., `fig-height` will be converted to `fig.height`. This is to make **knitr** more compatible with Quarto since Quarto always use dashes in chunk option names (#2282).

## MINOR CHANGES

- In-body chunk options (`#|`) are now preserved when extracting code from a document via `purl()` (thanks, @LuisLauM, #2268).
Expand Down
2 changes: 0 additions & 2 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ partition_chunk = function(engine, code) {
# normalize field name 'id' to 'label' if provided
meta$label = unlist(meta[c('label', 'id')])[[1]]
meta$id = NULL
# convert any option with fig- into fig. and out- to out.
names(meta) = sub('^(fig|out)-', '\\1.', names(meta))

# extract code
if (length(code) > 0 && is_blank(code[[1]])) {
Expand Down
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ tikz_dict = function(path) {
# but now also place to tweak default options
fix_options = function(options) {
options = as.strict_list(options)
# convert dashes in option names with dots
dashes = grep('-', names(options), value = TRUE)
options[gsub('-', '.', dashes)] = options[dashes]
options[dashes] = NULL

# normalize aliases
aliases = c(fig.format = 'dev', fig.dpi = 'dpi')
for (j in intersect(names(options), names(aliases))) {
options[[aliases[j]]] = options[[j]]
options[[j]] = NULL
}

# if you want to use subfloats, fig.show must be 'hold'
if (length(options$fig.subcap)) options$fig.show = 'hold'
Expand Down
18 changes: 18 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,21 @@ assert('remove_urls() removes the link', {
(remove_urls('a [b](c) d [e f+g](h) i.') %==% 'a b d e f+g i.')
(remove_urls('a [b](c) `[d](e)` f.') %==% 'a b `[d](e)` f.')
})

assert('options using `-` are converted to `.` and default value replaced', {
opts = opts_chunk$merge(list('fig-cap' = 'caption', 'out-width' = 300))
(is.null(fix_options(opts)[['fig-cap']]))
(is.null(fix_options(opts)[['out-width']]))
(fix_options(opts)[['fig.cap']] == 'caption')
(fix_options(opts)[['out.width']] == 300)
rm(opts)
})

assert('fig.format and fig.dpi', {
opts = opts_chunk$merge(list('fig-format' = 'svg', 'fig-dpi' = 750))
(is.null(fix_options(opts)[['fig-format']]))
(is.null(fix_options(opts)[['fig-dpi']]))
(fix_options(opts)[['dev']] == 'svg')
(fix_options(opts)[['dpi']] == 750)
rm(opts)
})

0 comments on commit 22b2090

Please sign in to comment.