我之前曾询问过在此处gt
的表格中插入图像并获得了很多帮助。但我现在遇到了一个新问题。困难的部分是我无法创建一个最小的例子。
从我原来的帖子的答案中考虑这段代码
library(gt)
library(magrittr)
library(purrr)
library(ggplot2)
# Let's make some pngs
mtcars %>%
split(.$cyl) %>%
map(~ ggplot(.x, aes(hp, mpg, color = factor(gear))) + geom_point()) %>%
set_names(c("CA", "UT", "OH")) %>%
iwalk(~ ggsave(paste0(.y, "_test.png"), .x))
column_one <- c("CA", "UT", "OH")
# Put the filenames in the column
column_two <- c("CA", "UT", "OH")
dashboard.data <- data.frame(column_one, column_two, stringsAsFactors = FALSE)
names(dashboard.data)[1] <- "State"
names(dashboard.data)[2] <- "IncidenceGauge"
dboard3 <- dashboard.data %>%
gt() %>%
tab_header(
title = md("**Big Title**"),
subtitle = md("*Subtitle*")
) %>%
text_transform(
locations = cells_body(vars(IncidenceGauge)),
fn = function(x) {
# loop over the elements of the column
map_chr(x, ~ local_image(
filename = paste0(.x, "_test.png"),
height = 100
))
}) %>%
cols_label(IncidenceGauge = "Risk Level")
print(dboard3)
它运行良好并生成此图像
现在,假设我想创建一个标题行,并且在该行的第二列中没有图像。在我的例子中,我创建了一个名为 NA_test.png 的图像,它只是一个空白的白色方块。对于下面的示例,您必须将图像显示为 NA_test.png,而不是 mtcars 的绘图。您会看到第二列现在以 NA 开头...
library(gt)
library(magrittr)
library(purrr)
library(ggplot2)
# Let's make some pngs
mtcars %>%
split(.$cyl) %>%
map(~ ggplot(.x, aes(hp, mpg, color = factor(gear))) + geom_point()) %>%
set_names(c("NA", "UT", "OH")) %>%
iwalk(~ ggsave(paste0(.y, "_test.png"), .x))
column_one <- c("Header", "UT", "OH")
# Put the filenames in the column
column_two <- c(NA, "UT", "OH")
dashboard.data <- data.frame(column_one, column_two, stringsAsFactors = FALSE)
names(dashboard.data)[1] <- "State"
names(dashboard.data)[2] <- "IncidenceGauge"
dboard3 <- dashboard.data %>%
gt() %>%
tab_header(
title = md("**Big Title**"),
subtitle = md("*Subtitle*")
) %>%
text_transform(
locations = cells_body(vars(IncidenceGauge)),
fn = function(x) {
# loop over the elements of the column
map_chr(x, ~ local_image(
filename = paste0(.x, "_test.png"),
height = 100
))
}) %>%
cols_label(IncidenceGauge = "Risk Level")
print(dboard3)
这也会运行并产生这个......再次,您只需想象 Header NA_test.png 旁边只是一个空白的白色图像。
我的问题是,在我的脚本中(更长,但实际上是从这个示例中复制的形式)当我遇到 NA 时,它似乎将它们视为空白而不是替换 NA。我收到 R 找不到 _test.png 的错误消息。不是我期望的 NA_test.png,而是 _test.png。
这是传递给的数据框gt()
Base 列没有调用text_transform()
,但 CommunityNewCases 列有调用,这就是发生错误的地方。
任何人都可以就为什么会发生这种情况提出任何理由吗?