gadenbuie / xaringanthemer

😎 Give your xaringan slides some style
https://pkg.garrickadenbuie.com/xaringanthemer/
Other
445 stars 28 forks source link

Inconsistent behaviour when plotting with ggplot2 and emojis and xaringanthemer is being used #66

Closed mine-cetinkaya-rundel closed 3 years ago

mine-cetinkaya-rundel commented 3 years ago

I have been observing that when I have emojis in my plots that I make with ggplot2 and include in a xaringan presentation, sometimes when I knit the emojis show up and other times they don't. I believe the culprit might be xaringanthemer as adding/removing the chunk where a theme is defined with xaringanthemer seems to affect the outcome.

Here is my minimal xaringan document


---
title: "xaringan document with emoji ggplot2"
output:
  xaringan::moon_reader:
    css: xaringan-themer.css
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

```{r xaringan-themer, include = FALSE}
library(xaringanthemer)
style_duo_accent(
  primary_color = "blue",
  secondary_color = "white",
)
suppressPackageStartupMessages(library(ggplot2))

ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_text(label = "🚗")


Here are the steps to reproduce the issue:

- Restart session
- Run `xaringan::inf_mr()` -> Emojis don't work in the viewer, or when popped out to the browser
- Run `xaringan::inf_mr()` again -> Emojis work in the viewer and when popped out to the browser
- Restart session
- Click Knit -> Emojis don't work in the viewer, or when popped out to the browser
- Click Knit again -> Emojis don't work in the viewer, or when popped out to the browser
- Run `xaringan::inf_mr()` -> Emojis don't work in the viewer, or when popped out to the browser
- Run `xaringan::inf_mr()` again -> Emojis work in the viewer and when popped out to the browser
- Remove the chunk labeled `xaringan-themer`, butleave the css parameter in the YAML (so it uses the previously generated CSS file)
- Restart session
- Run `xaringan::inf_mr()` -> Emojis work in the viewer and when popped out to the browser
- Restart session
- Click Knit again -> Emojis work in the viewer and when popped out to the browser
- Put the chunk labeled `xaringan-themer` back in, and we're back to the beginning

cc @thomasp85
gadenbuie commented 3 years ago

Hi @mine-cetinkaya-rundel thanks for the detailed investigation! The issue is almost certainly the result of xaringanthemer using showtext for the theme_xaringan() fonts. Because showtext requires the knitr option fig.showtext = TRUE and that particular option is basically only documented in an issue comment on GitHub (or was when I wrote xaringanthemer), xaringanthemer automatically sets this option on load.

The good news is that there's a work-around: set fig.showtext = FALSE in the chunk options of the chunk where xaringanthemer is attached to opt out of showtext features.

```{r xaringan-themer, include = FALSE, fig.showtext = FALSE}
library(xaringanthemer)
style_duo_accent(
  primary_color = "blue",
  secondary_color = "white",
)
suppressPackageStartupMessages(library(ggplot2))

ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_text(label = "🚗")
mine-cetinkaya-rundel commented 3 years ago

Ah, thank you!

Are there any potential side effects of fig.showtext = FALSE, e.g. if I'm using xaringanthemer to set a font as well?

gadenbuie commented 3 years ago

The side effects only appear when you try to use theme_xaringan() with ggplot2 when you want theme_xaringan() to find the fonts used in your xaringanthemer theme. If you have those fonts locally, you can call theme_xaringan(use_showtext = FALSE), possibly also using the text_font and title_font arguments to choose the correct local font names.

If you're using xaringanthemer for just the CSS file, then there aren't any side effects! (Or you can set eval=FALSE on the xaringan-themer chunk after running it once to make the xaringan-themer.css file.)

mine-cetinkaya-rundel commented 3 years ago

That all sounds good, thank you so much!

Maybe worth documenting the option somewhere in addition to this issue in case others run into it? But I'll close the issue here since we have a resolution either way.