yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 876 forks source link

`dev.args` ignored unless wrapped in a list with device names as element names, even with only one device #2238

Closed petrbouchal closed 1 year ago

petrbouchal commented 1 year ago

When setting a device document-wide, the dev.args only has effect if passed in the form of dev.args = list(device_name = list(option_1 = "x")); when set as dev.args = list(option_1 = "x"), the argument is ignored.

This applies also when only one device is set in dev.

This is surprising given that the documenation states the former usage as necessary only when multiple devices are used (and can be interpreted that when multiple devices are set, the dev.args list would apply to both devices unless set as list(dev_1 = list(...), dev_2 = list(...))).

The reprex below explores the resulting files when

  1. dev.args is set without the list structure (dev.args is ignored)
  2. dev.args is a list with one element, named after the single device (this works)
  3. the same arguments are passed to the device directly, for confirmation

Reprex

---
title: "dev.args passed to svglite"
output: 
  html_document:
    self_contained: false
---

```{r setup, include=FALSE}
library(ggplot2)
library(svglite)

Set dev.args as named list for the single device

knitr::opts_chunk$set(echo = TRUE, dev = "svglite",
                      comment = "",
                      dev.args = list(svglite = list(web_fonts = list("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap",
                                  "https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Condensed:ital,wght@0,400;0,700;1,400;1,700&display=swap"))))

Make plot

plt <- ggplot(mtcars) +
  annotate("text", label = "FFf gjfsgd fdshfdsk", x = .5, y = .5, family = "IBM Plex Sans", size = 12)

Render plot

plt

Inspect resulting file

Note the font-related lines that have appeared on lines 5-6 because the argument has been passed through dev.args

readLines("svgtest_files/figure-html/list-1.svg", n = 15)

Now set dev.args as a list of options

knitr::opts_chunk$set(echo = TRUE, dev = "svglite",
                      comment = "",
                      dev.args = list(web_fonts = list("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap",
                                  "https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Condensed:ital,wght@0,400;0,700;1,400;1,700&display=swap")))

Render file

plt

Inspect resulting file - note no font-related lines here

readLines("svgtest_files/figure-html/no-list-1.svg", n = 15)

Pass the argument directly to the device

svglite("svgtest.svg",
        web_fonts = list("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,700;1,400;1,700&display=swap",
                                  "https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Condensed:ital,wght@0,400;0,700;1,400;1,700&display=swap"))
plt
dev.off()

Inspect the file

Again, the arguments have an effect

readLines("svgtest.svg", n = 15)

Same without the argument

svglite("svgtest-noarg.svg")
plt
dev.off()

Inspect the file

Here, the font lines do not appear, as expected

readLines("svgtest-noarg.svg", n = 15)

## Session info

```r
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3, RStudio 2023.3.0.323

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  askpass_1.1       base64enc_0.1.3   brew_1.0.7        brio_1.1.3        bslib_0.4.0       cachem_1.0.6      callr_3.7.0       cli_3.6.0         clipr_0.8.0      
  clisymbols_1.2.0  commonmark_1.8.0  compiler_4.2.1    cpp11_0.4.3       crayon_1.5.2      credentials_1.3.2 curl_5.0.0        desc_1.4.2        devtools_2.4.3   
  diffobj_0.3.5     digest_0.6.31     ellipsis_0.3.2    evaluate_0.20     fansi_1.0.3       fastmap_1.1.0     fs_1.6.1          gert_1.9.0        gh_1.3.1         
  gitcreds_0.1.2    glue_1.6.2        graphics_4.2.1    grDevices_4.2.1   highr_0.10        htmltools_0.5.4   httr_1.4.4        ini_0.3.1         jquerylib_0.1.4  
  jsonlite_1.8.0    knitr_1.42.3      lifecycle_1.0.3   magrittr_2.0.3    memoise_2.0.1     methods_4.2.1     mime_0.12         openssl_2.0.3     pillar_1.8.1     
  pkgbuild_1.3.1    pkgconfig_2.0.3   pkgload_1.2.4     praise_1.0.0      prettyunits_1.1.1 processx_3.6.1    prompt_1.0.1      ps_1.7.1          purrr_1.0.1      
  R6_2.5.1          rappdirs_0.3.3    rcmdcheck_1.4.0   rematch2_2.1.2    remotes_2.4.2     reprex_2.0.2      rlang_1.0.6       rmarkdown_2.16    roxygen2_7.2.1   
  rprojroot_2.0.3   rstudioapi_0.14   rversions_2.1.1   sass_0.4.2        sessioninfo_1.2.2 stats_4.2.1       stringi_1.7.8     stringr_1.5.0     sys_3.4          
  testthat_3.1.6    tibble_3.1.8      tinytex_0.42      tools_4.2.1       usethis_2.1.6     utf8_1.2.2        utils_4.2.1       vctrs_0.5.2       waldo_0.4.0      
  whisker_0.4       withr_2.5.0       xfun_0.37         xml2_1.3.3        xopen_1.0.0       yaml_2.3.7        zip_2.2.2        

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 1 year ago

Should be fixed now. Thanks for the report!

github-actions[bot] commented 10 months ago

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.