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

wz1wpwve  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(132)

我正在尝试在我的R Markdown文档的脚注中包含小图标(保存为.png文件),该文档将被转换为PDF文件。我希望这些图标在脚注中提供额外的视觉信息,但我在实现这一点时遇到了困难。
我尝试使用HTML标签和LaTeX的\includegraphics包,但图标在PDF输出中无法正确显示。有没有人可以提供指导,如何成功地将这些图标包含在从R Markdown生成的PDF的脚注中?
提前感谢您的帮助!

  1. # Load the required libraries
  2. library(kableExtra)
  3. library(dplyr)
  4. # Create example data
  5. data <- data.frame(
  6. Category = c("Toyota", "Honda", "Ford", "Nissan"),
  7. Count = c(50, 60, 45, 55),
  8. Minimum = c(100, 110, 95, 105),
  9. Maximum = c(200, 210, 195, 205)
  10. )
  11. # Define a vector with image links and descriptions in a footnote
  12. footnote_text <- c(
  13. "Decreasing < -5% ![Arrow](Images/arrow_down.png)",
  14. "Slightly Decreasing -5 to -1% ![Arrow](Images/arrow_right_down.png)",
  15. "Stagnant -1 to +1% ![Arrow](Images/arrow_right.png)",
  16. "Slightly Increasing +1 to +5% ![Arrow](Images/arrow_right_up.png)",
  17. "Increasing > +5% ![Arrow](Images/arrow_up.png)"
  18. )
  19. # Create the table with adjusted column names
  20. table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  21. kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  22. add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  23. add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  24. add_footnote(footnote_text, notation="none")
  25. # Display the table
  26. table
k3fezbri

k3fezbri1#

这个应该可以了提供存储在Images文件夹中的图像。
基本上你必须传递png图像作为<img>属性。然后将字符串显示为html,即add_footnote(..., escape = F)

  1. library(kableExtra)
  2. library(dplyr)
  3. library(glue)
  4. # Create example data
  5. data <- data.frame(
  6. Category = c("Toyota", "Honda", "Ford", "Nissan"),
  7. Count = c(50, 60, 45, 55),
  8. Minimum = c(100, 110, 95, 105),
  9. Maximum = c(200, 210, 195, 205)
  10. )
  11. footnote_text <- c(
  12. "Decreasing < -5%)",
  13. "Slightly Decreasing -5 to -1%",
  14. "Stagnant -1 to +1%",
  15. "Slightly Increasing +1 to +5%",
  16. "Increasing > +5%"
  17. )
  18. footnote_png_paths <- c(
  19. "Images/arrow_down.png",
  20. "Images/arrow_right_down.png",
  21. "Images/arrow_right.png",
  22. "Images/arrow_right_up.png",
  23. "Images/arrow_up.png"
  24. )
  25. display_annotation <- function(width = "20px", height = "20px") {
  26. 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>')
  27. }
  28. # Create the table with adjusted column names
  29. table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  30. kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  31. add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  32. add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  33. add_footnote(display_annotation(), notation = "symbol", escape = F)
  34. # Display the table
  35. table
展开查看全部

相关问题