How to Include PNG Icons in Footnotes in R Markdown PDF Output?

54 Views Asked by At

I am trying to include small icons (saved as .png files) in the footnotes of my R Markdown document, which will be converted to a PDF file. I want these icons to provide additional visual information within the footnotes, but I am encountering difficulties achieving this.

I have attempted to use the HTML tag and LaTeX's \includegraphics package, but the icons are not displaying correctly in the PDF output. Can anyone provide guidance on how to successfully include these icons in the footnotes of a PDF generated from R Markdown?

Thank you in advance for your assistance!

# Load the required libraries
library(kableExtra)
library(dplyr)

# Create example data 
data <- data.frame(
  Category = c("Toyota", "Honda", "Ford", "Nissan"),
  Count = c(50, 60, 45, 55),
  Minimum = c(100, 110, 95, 105),
  Maximum = c(200, 210, 195, 205)
)

# Define a vector with image links and descriptions in a footnote
footnote_text <- c(
  "Decreasing < -5% ![Arrow](Images/arrow_down.png)",
  "Slightly Decreasing -5 to -1% ![Arrow](Images/arrow_right_down.png)",
  "Stagnant -1 to +1% ![Arrow](Images/arrow_right.png)",
  "Slightly Increasing +1 to +5% ![Arrow](Images/arrow_right_up.png)",
  "Increasing > +5% ![Arrow](Images/arrow_up.png)"
)

# Create the table with adjusted column names
table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  add_footnote(footnote_text, notation="none")

# Display the table
table
1

There are 1 best solutions below

1
Aleksandr On

This should do the job. Provided your images stored in Images folder.

Basically you have to pass png images as <img> attribute. Then display the strings as html, meaning add_footnote(..., escape = F).

library(kableExtra)
library(dplyr)
library(glue)

# Create example data 
data <- data.frame(
  Category = c("Toyota", "Honda", "Ford", "Nissan"),
  Count = c(50, 60, 45, 55),
  Minimum = c(100, 110, 95, 105),
  Maximum = c(200, 210, 195, 205)
)

footnote_text <- c(
  "Decreasing < -5%)",
  "Slightly Decreasing -5 to -1%",
  "Stagnant -1 to +1%",
  "Slightly Increasing +1 to +5%",
  "Increasing > +5%"
)
footnote_png_paths <- c(
  "Images/arrow_down.png",
  "Images/arrow_right_down.png",
  "Images/arrow_right.png",
  "Images/arrow_right_up.png",
  "Images/arrow_up.png"
)

display_annotation <- function(width = "20px", height = "20px") {
  glue_collapse(sapply(1:length(footnote_text), function(x) glue('{footnote_text[x]} <img src="{footnote_png_paths[x]}" alt="" width="{width}" height="{height}">')), '<br>')
  
}

# Create the table with adjusted column names
table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  add_footnote(display_annotation(), notation = "symbol", escape = F)

# Display the table
table