如何在 R Markdown 中将图像插入 Table
How To Insert Images Into Table in R Markdown
所以我有一个数据框
employee <- c('John Doe','Peter Gynn','Jolie Hope')
pic_url <- c('url_Johns Picture', 'url_Peters Picture', 'url_Jolies Picture')
df <- data.frame(employee, pic_url)
看起来像这样
employee pic_url
1 John Doe url_Johns Picture
2 Peter Gynn url_Peters Picture
3 Jolie Hope url_Jolies Picture
当我尝试基于此 table 编织一个 HTML 时,我首先在 Rmd 文件中获取 R 文件并使用 [=14= 创建一个 table ]
source(myrfile.R)
df %>% kable()
但这给了我错误,经过长时间的斗争,我认为这是因为图像 url 链接不应该在代码块中。但是我想不出如何在不使用代码块的情况下将图像包含在 table 中。我正在学习如何使用 knitr,所以如果你们能告诉我使用 knitr 或基本 R 的方法,我将不胜感激。提前致谢!
您可以使用 knitr 和 pander 包执行此操作。汽车图片取自这里:https://car-from-uk.com/sale.php?id=55162&country=us;在我的工作目录中重命名为 "rx4.jpg"。
rmarkdown 文档中的代码块:
library(knitr)
library(dplyr)
library(pander)
mtcars %>%
slice(1) %>%
mutate(
pic = "rx4.jpg" %>% pander::pandoc.image.return()
) %>%
pander()
产生这个输出:
所以我有一个数据框
employee <- c('John Doe','Peter Gynn','Jolie Hope')
pic_url <- c('url_Johns Picture', 'url_Peters Picture', 'url_Jolies Picture')
df <- data.frame(employee, pic_url)
看起来像这样
employee pic_url
1 John Doe url_Johns Picture
2 Peter Gynn url_Peters Picture
3 Jolie Hope url_Jolies Picture
当我尝试基于此 table 编织一个 HTML 时,我首先在 Rmd 文件中获取 R 文件并使用 [=14= 创建一个 table ]
source(myrfile.R)
df %>% kable()
但这给了我错误,经过长时间的斗争,我认为这是因为图像 url 链接不应该在代码块中。但是我想不出如何在不使用代码块的情况下将图像包含在 table 中。我正在学习如何使用 knitr,所以如果你们能告诉我使用 knitr 或基本 R 的方法,我将不胜感激。提前致谢!
您可以使用 knitr 和 pander 包执行此操作。汽车图片取自这里:https://car-from-uk.com/sale.php?id=55162&country=us;在我的工作目录中重命名为 "rx4.jpg"。
rmarkdown 文档中的代码块:
library(knitr)
library(dplyr)
library(pander)
mtcars %>%
slice(1) %>%
mutate(
pic = "rx4.jpg" %>% pander::pandoc.image.return()
) %>%
pander()
产生这个输出: