我正在尝试复制此过程并获取 gt 包来呈现 ggplot 对象,其中表中的每一行都有不同的图形(如火花线)。
链接页面上似乎有解决方案,但是当我尝试复制它时,出现以下错误
Error in body[[col]][loc$rows] <- fn(body[[col]][loc$rows]) : replacement has length zero
有人可以帮我调试吗?我在这里用头撞墙。
示例代码:
library(tidyverse)
#> Warning: package 'tibble' was built under R version 3.5.2
library(gt)
library(purrr)
# make a function for creating a plot
# of a group
plot_group <- function(name, df) {
plot_object <-
ggplot(data = df,
aes(x = hp, y = trq,
size = msrp)) +
geom_point(color = "blue") +
theme(legend.position = "none")
return(plot_object)
}
# make a plot of each mfr
gtcars %>%
group_by(mfr) %>%
nest() %>%
mutate(plot = map2(mfr, data, plot_group)) %>%
select(-data) %>%
# Create empty column (a placeholder for the images)
mutate(ggplot = NA) ->
tibble_plot
# Minor changes to this code
tibble_plot %>%
gt() %>%
text_transform(
locations = cells_body(vars(ggplot)), # use empty cell as location
fn = function(x) {
# Insert each image into each empty cell in `ggplot`
map(.$plot, ggplot_image, height = px(200))
}
)