Using Highcharter in an Interactive and Static Quarto Report

Displaying interactive and static charts in different quarto report formats: .html, .pdf, and docx (word document)
data visualisation
data analysis
r
quarto
code
Author

Eli Nimy

Published

February 17, 2024

Generating reports with Highcharter charts in R can pose challenges when the output format needs to be in .pdf or .docx. However, there’s a straightforward workaround available. You can download the chart and then paste it into either a PowerPoint or Word document. If needed, you can convert the document to a .pdf format afterward.

To enable downloading options, you can use the hc_exporting(...) function:

highcharter_chart_object |> 
  hc_exporting(enabled = TRUE)

The highcharter_chart_object represents a highcharter chart.

Download options provided by enabling exporting:

In this post, we will explore another workaround using the webshot2 and htmlwidgets packages. This method enables you to work exclusively within R without the need to download or take screenshots of charts for pasting into other documents.

Note

To learn more about the YAML Quarto code chunk options used in this post, you can refer to the Quarto Chuck Options section in the R for Data Science (2e) online book.

0.1 Package Use-case and Installation

Tidyverse will be used for data manipulation

install.packages("tidyverse")

Highcharter will be used to create an interactive chart

install.packages("highcharter")

Htmlwidgets will used to save the interactive chart to an .html file

install.packages("htmlwidgets")

Webshot2 will be used to take a screenshot of the saved interactive chart (.html file). The screenshot can be saved as an .png, .jpeg, or .pdf.

install.packages("webshot2")

0.2 Dataset

The line chart generated using Highcharter, showcased in the reports within the subsequent sections, provides insights derived from the vaccines dataset.

vaccines is a highcharter dataset that contains the number of infected people by Measles, measured over 70-some years and across all 50 states. From the WSJ analysis: http://graphics.wsj.com/infectious-diseases-and-vaccines/

Format - A data frame with 3,876 observations and 3 variables.

  • year- year
  • state - name of the state
  • count - number of cases per 100,000 people. If the value is NA the count was 0

View vaccines dataset

library(highcharter)
vaccines
# A tibble: 3,876 × 3
    year state                count
   <int> <chr>                <dbl>
 1  1928 Alabama              335. 
 2  1928 Alaska                NA  
 3  1928 Arizona              201. 
 4  1928 Arkansas             482. 
 5  1928 California            69.2
 6  1928 Colorado             207. 
 7  1928 Connecticut          635. 
 8  1928 Delaware             256. 
 9  1928 District Of Columbia 536. 
10  1928 Florida              120. 
# ℹ 3,866 more rows

0.3 .pdf Quarto Report

The following code produces a .pdf report, accomplished by setting the output format to PDF using format: pdf. The delay argument specifies the time to wait before taking a screenshot, in seconds. It is advisable to set a longer delay to ensure all assets display properly.

Note

To generate a PDF format report, a LaTeX installation is necessary. To ensure smooth output of a .pdf report, please install TinyTeX by executing the following command in your Terminal:

Terminal
quarto install tinytex
---
title: "Measles Infected Cases"
author: "E-tech Blog"
date: "2024-02-17"
format: pdf
execute:
  echo: false
  warning: false
---
library(tidyverse) 
library(highcharter)
library(webshot2)
library(htmlwidgets)

measles_infected_cases <- vaccines |> 
  filter(state %in% c("Florida", "California")) |> 
  mutate(count = ifelse(is.na(count), 0, count)) |> 
  hchart("line", hcaes(x = year, y = count, group = state)) |> 
  hc_xAxis(title = list(text = "Year")) |>  
  hc_yAxis(title = list(text = "Number of cases per 100k people")) |>  
  hc_title(text = list("Measles Infected Cases per 100k People 
                       in Florida & California")) |> 
  hc_colors(c("#dc3545", "#5c6f7e")) |> 
  hc_exporting(enabled = FALSE) |> 
  hc_add_theme(hc_theme(chart = list(backgroundColor = "white")))

saveWidget(widget = measles_infected_cases,
           file = "measles_infected_cases_chart.html")
webshot(url = "measles_infected_cases_chart.html",
        file = "measles_infector_cases_chart.jpeg",
        delay = 2)

The above code produces the following report:

0.4 .docx Quarto Report

The .docx Quarto report can be produced by replacing format: pdf with format: docx in the code provided in Section 0.3.

Note

Viewing Word documents necessitates the installation of MS Word (or Libre/Open Office on Linux).

The .docx format will produce the following report:

0.5 .html Quarto Report

The following code produces an .html report. This is achieved by setting the output format to HTML using format: html.

---
title: "Measles Infected Cases"
author: "E-tech Blog"
date: "2024-02-17"
format: html
execute:
  echo: false
  warning: false
---
library(tidyverse) 
library(highcharter)

measles_infected_cases <- vaccines |> 
  filter(state %in% c("Florida", "California")) |> 
  mutate(count = ifelse(is.na(count), 0, count)) |> 
  hchart("line", hcaes(x = year, y = count, group = state)) |> 
  hc_xAxis(title = list(text = "Year")) |>  
  hc_yAxis(title = list(text = "Number of cases per 100k people")) |>  
  hc_title(text = list("Measles Infected Cases per 100k People 
                       in Florida & California")) |> 
  hc_colors(c("#dc3545", "#5c6f7e")) |> 
  hc_exporting(enabled = TRUE) |> 
  hc_add_theme(hc_theme(chart = list(backgroundColor = "white")))

measles_infected_cases

The above code produces the following report:

0.6 Sejda .html to PDF

Alternatively, the Sejda HTML to PDF converter can be used to quickly convert a .html format report to a .pdf report.

Thank you for following along

Your friendly neighborhood data scientist