gpt4 book ai didi

r - 使用 gg_chunk() : Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable 时出现灵活错误

转载 作者:行者123 更新时间:2023-12-05 05:46:38 25 4
gpt4 key购买 nike

我正在尝试使用 flextable 使用 gg_chunk() 在单元格内生成一个带有 ggplots 的表格,但是我一直收到错误:点错误 [[1L]] [[1L]]:即使我尝试使用 officeodown 模板从 rmarkdown 运行代码,“closure”类型的对象也不可子集化。

这是我使用的代码:

  library(flextable)
library(dplyr)
library(purrr)

df <-
iris %>% select(Species, Petal.Width) %>% group_by(Species) %>% nest(data = c(Petal.Width))

gg <- function(x) {
d <- x %>% mutate(x = row_number()) %>% rename(y = 1)
p <- ggplot(d) +
geom_line(aes(x, y)) +
theme_void()
list(p)
}

df_gg <- df %>% mutate(gg = map(data, ~ gg(.x)))

ft <- flextable(data = df_gg) %>%
compose(j = "gg", value = as_paragraph(gg_chunk(
value = gg,
width = 1.5,
height = .4
))) %>%
autofit()
ft

感谢任何帮助!

最佳答案

这似乎是 purrr::compose 和 flextable::compose 的问题。

这应该有效:

library(flextable)
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)

df <-
iris %>% select(Species, Petal.Width) %>% group_by(Species) %>% nest(data = c(Petal.Width))

gg <- function(x) {
d <- x %>% mutate(x = row_number()) %>% rename(y = 1)
p <- ggplot(d) +
geom_line(aes(x, y)) +
theme_void()
list(p)
}

df_gg <- df %>% mutate(gg = map(data, ~ gg(.x)))

ft <- flextable(data = df_gg) %>%
compose(j = "gg", value = as_paragraph(gg_chunk(
value = gg,
width = 1.5,
height = .4
))) %>%
autofit()
ft

enter image description here

关于r - 使用 gg_chunk() : Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable 时出现灵活错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71134520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com