Search code examples
rstudior-markdownfont-awesometikzwaffle-chart

Compile RMarkdown PDF with Waffle Chart and Glyphs FAILURE. MWE included


I am unable to compile PDF using RMarkdown, the waffle package, and glyphs

It doesn't matter if I use the TIKZ device or PDF rendering. The document compiles without a problem to HTML.

The only work around I can think of right now is to create SVG graphic with the HTML compiler, then to reference those files in my intermediate .TEX file.

Notice that if you simply run the code below the line that says, " ## ---- waffle_figure ---- ". It should create the chart given that you've previously installed the fontawesome font on your system, installed the extrafont package in R, and run the font_install() command.

---
title: "Waffle"
output:
  pdf_document:
    latex_engine: xelatex
  html_document: default
header-includes:
- \usepackage{fontspec}
- \defaultfontfeatures{Extension = .otf}
- \usepackage{fontawesome}
- \usepackage{tikz}
---

```{r setup, include=FALSE}
library(knitr)
library(tikzDevice)
knitr::opts_chunk$set(warning = FALSE,  error = FALSE, message = FALSE,  results='hide', echo = FALSE, dev = "tikz", external = TRUE)
```

\faTwitter

## Waffle Plot

You can also embed plots, for example:  \newline

```{r pressure, echo=FALSE, dev="tikz"}
## ---- waffle_figure ----
loadpackages <- function(package.list = c("ggplot2", "Rcpp")) {
    new.packages <- package.list[!(package.list %in% installed.packages()[,"Package"])]
    if (length(new.packages)){install.packages(new.packages, repos = 'http://cran.us.r-project.org')}
    lapply(eval(package.list), require, character.only = TRUE)}

loadpackages(c("waffle", "extrafont", "grid", "gridExtra", "tikzDevice"))
parts <- c(40, 30, 20, 10)
waffle(parts,
       rows=10,
       use_glyph = "user",
       glyph_size = 5)
```

Solution

  • The Waffle package is now updated! Now, both the quartz and cairo devices work with knitr/rmarkdown. Huge thank you's are due to Bob Rudis and Dave Gandy.

    The revised code includes latin modern (LaTeX) font in order to demonstrate that it is possible to implement latex fonts alongside fontawesome glyphs in waffle charts.

    In order for the code to work, you must first install lmroman10-regular-webfont.ttf and fontawesome-webfont.ttf from their respective websites: Click on the webfont kit link and Fontawesome download.


    title: "Waffle"
    output:
      pdf_document:
        latex_engine: xelatex
      html_document: default
    header-includes:
    - \usepackage{fontspec}
    - \defaultfontfeatures{Extension = .otf}
    - \usepackage{fontawesome}
    - \usepackage{tikz}
    ---
    
    ```{r setup, include=FALSE}
    library(knitr)
    knitr::opts_chunk$set(warning = FALSE,  error = FALSE, message = FALSE,  results='hide', echo = FALSE, dev = "tikz", external = TRUE)
    ```
    
    ## Font Awesome Gyphy
    
    Font awesome glyphs are easy to integrate into the main text. For example: \faTwitter.
    
    ## Waffle Plot
    
    You can also embed plots with glyphs and custom fonts, for example:  \newline
    
    ```{r pressure, echo=FALSE, dev="quartz_pdf", dev.args=list(family = "Helvetica")}
    ## ---- waffle_figure ----
    loadpackages <- function(package.list = c("ggplot2", "Rcpp")) {
        new.packages <- package.list[!(package.list %in% installed.packages()[,"Package"])]
        if (length(new.packages)){install.packages(new.packages, repos = 'http://cran.us.r-project.org')}
        lapply(eval(package.list), require, character.only = TRUE)}
    
    loadpackages(c("waffle", "extrafont", "grid", "gridExtra"))
    # font_import()  # Run this command, type "y", and press enter after installing new fonts.
    
    parts <- c(40, 30, 20, 10)
    waffle(parts,
           rows=10,
           use_glyph = "user",
           glyph_size = 7)+ggtitle("Some Sample Text Here")+
      theme(plot.title = element_text(family="LM Roman 10"))
    ```