Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev.args encoding issue with unicode symbol #2526

Closed
sebkopf opened this issue Oct 31, 2023 · 2 comments
Closed

dev.args encoding issue with unicode symbol #2526

sebkopf opened this issue Oct 31, 2023 · 2 comments
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@sebkopf
Copy link
Contributor

sebkopf commented Oct 31, 2023

I ran into an issue on Mac OS X where the encoding in dev.args chunk option seems to be getting lost. To explain I am including a series of reprexs to highlight the issue . Any help much appreciated. Note that this is NOT an issue on Windows (tested with the same package versions).

When including unicode symbol per mille (u2030 = ‰), saving to a pdf throws the following mbcsToSbcs error if the encoding is not included (unicode symbol dagger u2021 = ‡ for comparison does not throw an error on its own):

# unicode symbol causes an error
library(ggplot2)
p <- ggplot() + 
 annotate(geom = 'text', x = 0, y = 1, label = 'permil: \u2030 \n dagger: \u2021')
ggsave(plot = p, filename = "test.pdf")
#> Saving 7 x 5 in image
#> Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : conversion failure from UTF-8 to latin1 on 'permil: ‰ ' in 'mbcsToSbcs'

Created on 2023-10-31 with reprex v2.0.2

With the encoding included (e.g. WinAnsi, others work too), this issue is resolved:

# no error if proper encoding is included
library(ggplot2)
p <- ggplot() + 
 annotate(geom = 'text', x = 0, y = 1, label = ' dagger: \u2021 \n permil: \u2030')
ggsave(plot = p, filename = "test.pdf", encoding = "WinAnsi")
#> Saving 7 x 5 in image

Created on 2023-10-31 with reprex v2.0.2

However, when now including automatic chunk plot saving in an RMarkdown document, the error reappears for saving to pdf. First how it should work (no issue when saving to png alone, included a dev.args parameter to illustrate its effect):

# generate a simple rmarkdown file
"---
title: test
format: html
---
```{r 'plot', dev = 'png', fig.path = 'test-', dev.args = list(bg = 'yellow')}
library(ggplot2)
ggplot() +
   annotate(geom = 'text', x = 0, y = 1, label = 'dagger: \\u2021 \\n permil: \\u2030') +
   theme(plot.background = element_blank())
```" |> cat(file = "example.Rmd")

# render the rmarkdown file
rmarkdown::render("example.Rmd")
#> processing file: example.Rmd
#> output file: example.knit.md
#> /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc +RTS -K512m -RTS example.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output example.html --lua-filter /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --variable bs3=TRUE --section-divs --template /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable 'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in-header /var/folders/ns/x0fwvm6x22v8gf5tmj9r3yww0000gn/T//RtmptknJaz/rmarkdown-str16a411eae9e82.html
#> 
#> Output created: example.html

# check if png was created correctly
magick::image_read("test-plot-1.png")

Created on 2023-10-31 with reprex v2.0.2

Actual Issue

But as soon as I save to pdf I get the mbcsToSBcs error even when defining the encoding in the dev.args:

# saving to pdf in the chunk fails even with encoding = "WinAnsi"
"---
title: test
format: html
---
```{r 'plot', dev = 'pdf', fig.path = 'test-', dev.args = list(encoding = 'WinAnsi')}
library(ggplot2)
ggplot() +
   annotate(geom = 'text', x = 0, y = 1, label = 'dagger: \\u2021 \\n permil: \\u2030') +
   theme(plot.background = element_blank())
```" |> cat(file = "example.Rmd")

# render the rmarkdown file
rmarkdown::render("example.Rmd")
#> processing file: example.Rmd
#> 
#> Quitting from lines 6-11 [plot] (example.Rmd)
#> Error in `grid.Call.graphics()`:
#> ! conversion failure from UTF-8 to latin1 on ' permil: ‰' in 'mbcsToSbcs'
#> Backtrace:
#>      ▆
#>   1. ├─rmarkdown::render("example.Rmd")
#>   2. │ └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>   3. │   └─knitr:::process_file(text, output)
#>   4. │     ├─knitr:::handle_error(...)
#>   5. │     │ └─base::withCallingHandlers(...)
#>   6. │     ├─base::withCallingHandlers(...)
#>   7. │     ├─knitr:::process_group(group)
#>   8. │     └─knitr:::process_group.block(group)
#>   9. │       └─knitr:::call_block(x)
#>  10. │         └─knitr:::block_exec(params)
#>  11. │           └─knitr:::eng_r(options)
#>  12. │             ├─knitr:::in_input_dir(...)
#>  13. │             │ └─knitr:::in_dir(input_dir(), expr)
#>  14. │             └─knitr (local) evaluate(...)
#>  15. │               └─evaluate::evaluate(...)
#>  16. │                 └─evaluate:::evaluate_call(...)
#>  17. │                   ├─base (local) handle(...)
#>  18. │                   ├─base::withCallingHandlers(...)
#>  19. │                   ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  20. │                   └─knitr (local) value_fun(ev$value, ev$visible)
#>  21. │                     └─knitr (local) fun(x, options = options)
#>  22. │                       ├─base::withVisible(knit_print(x, ...))
#>  23. │                       ├─knitr::knit_print(x, ...)
#>  24. │                       └─knitr:::knit_print.default(x, ...)
#>  25. │                         └─evaluate (local) normal_print(x)
#>  26. │                           ├─base::print(x)
#>  27. │                           └─ggplot2:::print.ggplot(x)
#>  28. │                             ├─grid::grid.draw(gtable)
#>  29. │                             └─grid:::grid.draw.gTree(gtable)
#>  30. │                               └─grDevices::recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
#>  31. ├─grid:::drawGTree(x)
#>  32. │ ├─grid::grid.draw(x$children[[i]], recording = FALSE)
#>  33. │ └─grid:::grid.draw.gTree(x$children[[i]], recording = FALSE)
#>  34. │   └─grDevices::recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
#>  35. ├─grid:::drawGTree(x)
#>  36. │ ├─grid::grid.draw(x$children[[i]], recording = FALSE)
#>  37. │ └─grid:::grid.draw.gTree(x$children[[i]], recording = FALSE)
#>  38. │   └─grDevices::recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
#>  39. ├─grid:::drawGTree(x)
#>  40. │ ├─grid::grid.draw(x$children[[i]], recording = FALSE)
#>  41. │ └─grid:::grid.draw.grob(x$children[[i]], recording = FALSE)
#>  42. │   └─grDevices::recordGraphics(drawGrob(x), list(x = x), getNamespace("grid"))
#>  43. └─grid:::drawGrob(x)
#>  44.   ├─grid::drawDetails(x, recording = FALSE)
#>  45.   └─grid:::drawDetails.text(x, recording = FALSE)
#>  46.     └─grid:::grid.Call.graphics(...)

Created on 2023-10-31 with reprex v2.0.2

I'm totally stumped, especially because this used to work in the past. Any help why encoding does not fix this issue would be very much appreciated. It's possible that a recent OS X upgrade is the cause for this but that seems unfixable so I hope it's not the issue.

Here's my xfun::session_info('rmarkdown')

R version 4.3.1 (2023-06-16)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.0, RStudio 2023.6.2.561


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

time zone: America/Denver
tzcode source: internal

Package version:
  base64enc_0.1.3   bslib_0.5.1       cachem_1.0.8      cli_3.6.1         digest_0.6.33     ellipsis_0.3.2    evaluate_0.22     fastmap_1.1.1     fontawesome_0.5.2
  fs_1.6.3          glue_1.6.2        graphics_4.3.1    grDevices_4.3.1   highr_0.10        htmltools_0.5.6.1 jquerylib_0.1.4   jsonlite_1.8.7    knitr_1.45.2     
  lifecycle_1.0.3   magrittr_2.0.3    memoise_2.0.1     methods_4.3.1     mime_0.12         R6_2.5.1          rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.25.1 
  sass_0.4.7        stats_4.3.1       stringi_1.7.12    stringr_1.5.0     tinytex_0.48      tools_4.3.1       utils_4.3.1       vctrs_0.6.4       xfun_0.40        
  yaml_2.3.7       

Pandoc version: 3.1.1

Checklist

When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:

  • [ x ] formatted your issue so it is easier for us to read?

  • [ x ] included a minimal, self-contained, and reproducible example?

  • [ x ] pasted the output from xfun::session_info('rmarkdown') in your issue?

  • [ x ] upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?

  • [ x ] installed and tested your bug with the development version of the rmarkdown package using remotes::install_github("rstudio/rmarkdown")?

@yihui yihui added the bug an unexpected problem or unintended behavior label Oct 31, 2023
@yihui yihui self-assigned this Oct 31, 2023
@yihui
Copy link
Member

yihui commented Oct 31, 2023

Should be fixed in knitr now. Thanks for the report! You can install the development version via

remotes::install_github('yihui/knitr')

clrpackages pushed a commit to clearlinux-pkgs/R-knitr that referenced this issue Apr 9, 2024
Kyle F Butts (2):
      Add support for `# %%` for chunk demarcation in `knitr::spin` (#2307)
      make spin() recognize `#|` comments as code chunks (#2320)

Lee Mendelowitz (1):
      Fix the error when kable()'s caption value is of length > 1 (#2312)

Max Schmit (1):
      add ALTER as sql statment, that is not returning anything (#2330)

Yihui Xie (53):
      start the next version
      fix #2304: shouldn't have used `return(meta)` since `meta` is not the `citation()` but `packageDescription()`
      fix rstudio/rmarkdown#2526 by reverting bebf117 since the `cairo_pdf` device has been explicitly supported in chunk_device(); now all possible `pdf()` arguments are supported, including `family`
      Disallow unbalanced chunk delimiters (#2306)
      close #1679: obtain the caption from the chunk option `tab.cap` for `kable()`
      also try the chunk option tbl.cap when tab.cap is NULL (quarto-dev/quarto-cli#7555)
      tweak R Markdown v1 vignettes
      move mailing list link and use https in ref card
      move css and js to jsdelivr for html_vignette
      use jsdelivr for the Rhtml template
      rewrite the docco classic style using JS instead of R
      standardize the docco linear format
      new version of funmediation is on CRAN now
      support the ... argument in chunk hooks
      optimize PNG images in package vignettes via optipng and pngquant if they are installed
      use R Markdown v1 for the knitr-intro vignette, too
      delete some files that are no longer needed in inst/examples and also ignore some files
      fix #2308: don't trim trailing spaces escaped by `\`
      support in-body chunk options for ojs and mermaid (quarto-dev/quarto-cli#7799)
      amend 3dcfac4780accc3791855b298a556297f62dc73d to fix quarto-dev/quarto-cli#5994: add YAML options to code for dot/mermaid/ojs chunks so that the full chunk content can be returned to Quarto
      add trailing slashes to URLs
      use the `assert(fact, {})` syntax for all tests
      revert c9473ad: map the chunk option `tbl-cap` to `tab.cap`
      leave tbl.cap alone; don't convert it to tab.cap, since Quarto has its own special handling of `tbl-cap`
      Create FUNDING.yml
      use xfun::decimal_dot() to ensure dot as the decimal separator
      fix rstudio/rmarkdown#2525: ensure the dot is used as the decimal separator even when options(OutDec) is not `.` or `LC_NUMERIC` is inappropriate
      factor out code to xfun::csv_options() and xfun::divide_chunk()
      get_option_comment() has been moved to xfun
      roxygenize
      bump xfun version
      xfun 0.42 is on CRAN now
      revert 87d094a6c593c31a3667821865e91b4a530018ec per request from CRAN maintainers, since _R_CHECK_VIGNETTES_SKIP_RUN_MAYBE_=true has been made the default
      fix rstudio/tinytex#435: adjust PATH temporarily for pdf_crop() to be able to find the command `pdfcrop` from TinyTeX
      RStudio IDE still calls parse_params(): https://github.com/rstudio/rstudio/blob/9a6196233d8eae5aa365b73eaafba04f7714190c/src/cpp/session/modules/SessionRmdNotebook.R#L666
      a follow-up on #2331: use xfun::fenced_block() from yihui/xfun@22a97ce
      xfun 0.43 is on CRAN now
      not sure what's wrong with pak
      `knitr::imgur_upload()` is now simply a wrapper function of `xfun::upload_imgur()` (#2325)
      too many ways to do the same thing... drop support for `#-` in spin()
      make spin() work again with input that can't be parsed as R code
      use xfun::check_old_package()
      markdown no longer sets this option internally, so no need to empty it now
      better detection of Rmd v2 input in knit2html(), so it no longer warns against documents that use markdown:: or litedown:: output formats
      make regex a little stricter so it won't match rmarkdown::
      the loo issue has been fixed (#2306)
      roxygen2 requires _PACKAGE now
      rename class names 'block' to 'knitr_block', and 'inline' to 'knitr_inline' since I need to export the print() methods and the names 'block' and 'inline' are too general
      preserve trailing \n in source code by doubling it, because later we will remove trailing \n in fenced_block()
      a temporary workaround for davidgohel/flextable#621
      StructFDR has been updated on CRAN but unfortunately the maintainer didn't seem to have seen my email about #2306
      I can't use knitr_block/knitr_inline as class names since it would break the lightparser package; let me just get rid of these S3 methods, which are unnecessarily advanced---just use normal functions instead (i.e., use inherits() to do the dispatch by myself)
      CRAN release v1.46

knokknok (1):
      fix #2318: faster processing of dependencies in dep_auto() (#2321)
Copy link

github-actions bot commented May 1, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
Archived in project
Development

No branches or pull requests

2 participants