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

problem with svg device, expression(), and unicode characters #1831

Closed
3 tasks done
richarddmorey opened this issue Apr 14, 2020 · 4 comments
Closed
3 tasks done

problem with svg device, expression(), and unicode characters #1831

richarddmorey opened this issue Apr 14, 2020 · 4 comments

Comments

@richarddmorey
Copy link
Sponsor Contributor

richarddmorey commented Apr 14, 2020

I am having difficulty using the svg device with math expressions and unicode (in this case, left- and right-quotes). This only occurs on my Windows 10 computer; my osx computer seems unaffected. Consider the following Rmardown document:

---
output: 
  html_document:
      dev: svg
---

```{r echo=FALSE}
plot(1, axes=FALSE, ty='n')

## Line A: 
 mtext(side = 2, text = expression(paste("A")))
## Line B:
 mtext(side = 3, text = expression(paste("“B")))
## Line C: 
 mtext(side = 4, text = expression(paste("C")))

axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
```

(Note the left-quote in Line B). Compilation fails with the error:

Quitting from lines 8-18 (testing.Rmd) 
Error in mtext(side = 4, text = expression(paste("C"))) : 
  metric information not available for this device
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> mtext
Execution halted
  • If I change the device to png I get the output I expect.
  • If I comment out lines B and C, I get the output I expect (eg, the output from lines A and the axis)
  • If I comment out only line C, I do not get the output I expect. Neither line B works, nor the axis. There appears to be something wrong with line B that causes everything later in the chunk to fail too.
  • If I remove the expression() from line B, everything works as expected (including the unicode left-quote)
  • I tried installing the development version of knitr as the Issue Guide suggests, but that did not help.

This appears, then, to be an interaction between svg, expression, and unicode.

Windows session info (fails on this computer)

> xfun::session_info('knitr')
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363), RStudio 1.2.1335

Locale:
  LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
  LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
  LC_TIME=English_United Kingdom.1252    

Package version:
  evaluate_0.14   glue_1.4.0      graphics_3.6.2  grDevices_3.6.2 highr_0.8       knitr_1.28.3    magrittr_1.5   
  markdown_1.1    methods_3.6.2   mime_0.9        stats_3.6.2     stringi_1.4.6   stringr_1.4.0   tools_3.6.2    
  utils_3.6.2     xfun_0.13       yaml_2.2.1  

OSX session info (no problem on this computer)

> xfun::session_info('knitr')
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.2.5031

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

Package version:
  evaluate_0.14   glue_1.4.0      graphics_3.6.1  grDevices_3.6.1
  highr_0.8       knitr_1.28.3    magrittr_1.5    markdown_1.1   
  methods_3.6.1   mime_0.9        stats_3.6.1     stringi_1.4.6  
  stringr_1.4.0   tools_3.6.1     utils_3.6.1     xfun_0.12      
  yaml_2.2.1 

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

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

@richarddmorey
Copy link
Sponsor Contributor Author

Adding to the mystery, the code works if I change fonts using showtext (apologies for the hard-coded path name to the font):

---
output: 
  html_document:
      dev: svg
---

```{r echo=FALSE, include=FALSE}
  library(showtext)

  ## set chunk options to use showtext
  knitr::opts_chunk$set(fig.showtext=TRUE, dpi=300)

  sysfonts::font_paths( list.dirs("D:/OneDrive - Cardiff University/talks/significance_testing_two_parter/assets/css", recursive = TRUE) )
  
  font_add("roboto_condensed",
           regular = "RobotoCondensed-Regular.ttf", 
           bold = "RobotoCondensed-Bold.ttf",
           italic = "RobotoCondensed-Italic.ttf",
           bolditalic = "RobotoCondensed-BoldItalic.ttf"
  )

  ## Add hooks so that I can choose fonts in the chunk options         
  knitr::knit_hooks$set(
    roboto_condensed = function(before, options, envir)
      if (before) par(family = "roboto_condensed")
  )
  
  knitr::opts_chunk$set(roboto_condensed = TRUE)
  
```

```{r echo=FALSE}
plot(1, axes=FALSE, ty='n')

## Line A: 
 mtext(side = 2, text = expression(paste("A")))
## Line B:
 mtext(side = 3, text = expression(paste("“B")))
## Line C: 
 mtext(side = 4, text = expression(paste("C")))

axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
```

This Rmd file compiles just fine and gives me what I expect. So it seems to be an interaction between the default font, the svg device, expression(), and unicode.

@cderv
Copy link
Collaborator

cderv commented Apr 19, 2020

Let's not the getting out of Rmarkdown and knitr framework still get us the error

Using R svg device in R script

svg("test.svg")
plot(1, axes=FALSE, type ='n')
## Line A: 
mtext(side = 2, text = expression(paste("A")))
## Line B:
mtext(side = 3, text = expression(paste("“B")))
## Line C: 
mtext(side = 4, text = expression(paste("C")))
axis(1, at = par()$usr[1:2], labels = c("More", "Less"), tick = FALSE)
dev.off()

I get the error when executing Line C part

> mtext(side = 4, text = expression(paste("C")))
Error in mtext(side = 4, text = expression(paste("C"))) : 
  metric information not available for this device

When specifying a device for knitr, it will use the corresponding R code for the device. If there is an issue with the device and the code for the plot already, it will also error in knitr.

I think this needs to be investigate at the R code level outside of knitr or rmarkdown for a better understanding.

Removing the special character in line B make the error disappear. I am not sure that such a character can be used in expression or maybe it is an encoding issue as you said this is OS related 🤷

Hope it helps understand better this

@richarddmorey
Copy link
Sponsor Contributor Author

Yes, confirmed that happens with just the svg device outside of knitr. Thanks. I'm going to close this up and report it elsewhere.

@github-actions
Copy link

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 Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants