我想生成小的 ggplot 图形并在reactable
. 我可以生成所需的图(每行 1 个),但是当将它们插入表格时,它们会显示为损坏的图像。我想它与文件路径有关?
注意:这最终将用于动态生成图形的闪亮应用程序中。我这样做是reactable
因为我计划将它与可扩展行功能结合起来。
这是一些可重现的代码
library(tidyverse)
library(palmerpenguins)
library(reactable)
#get list of species names
species <- penguins %>% select(species ) %>%
distinct() %>% pull()
# make a simple df
plot_data <- penguins %>%
group_by(species) %>%
summarize(mean = mean(bill_length_mm, na.rm=T))
# for loop to generate and save plots
for(i in species){
tmp_plot <-
plot_data %>%
filter(species == i) %>%
ggplot(aes(y=species, x=mean))+
geom_col()+
theme_void()
file_name <- i %>% janitor::make_clean_names()
ggsave(plot = tmp_plot,
filename = paste0("plots/",file_name,".png"),
width=4,
height=.5)
}
#make a table
species %>%
as.data.frame() %>%
rename("species" = 1) %>%
mutate(plot = paste0("<img src='plots/", janitor::make_clean_names(species),".png' />")) %>%
reactable(
columns = list(
plot = colDef(html = TRUE,
resizable = TRUE,
show=T)
))
就像检查一样,这样做会加载图形:magick::image_read("plots/adelie.png")
所以我不确定我错过了什么。