如何在 R Markdown PDF 输出的脚注中包含 PNG 图标?

问题描述 投票:0回答:1

我试图在 R Markdown 文档的脚注中包含小图标(另存为 .png 文件),该文档将转换为 PDF 文件。我希望这些图标在脚注中提供额外的视觉信息,但我在实现这一点时遇到了困难。

我尝试使用 HTML 标签和 LaTeX 的 \includegraphics 包,但图标在 PDF 输出中无法正确显示。任何人都可以提供有关如何成功地将这些图标包含在 R Markdown 生成的 PDF 脚注中的指导吗?

预先感谢您的协助!

# 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
r png kable footnotes
1个回答
0
投票

这应该可以完成工作。提供您存储在

Images
文件夹中的图像。

基本上你必须将 png 图像作为

<img>
属性传递。然后将字符串显示为html,意思是
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
© www.soinside.com 2019 - 2024. All rights reserved.