Flextable error when using gg_chunk(): Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable

Viewed 81

I am trying to use flextable to produce a table with ggplots inside cells using gg_chunk() however I keep getting an error: Error in dots[[1L]][[1L]] : object of type 'closure' is not subsettable even if I try to run the code from rmarkdown using an officeodown template.

This is the code I am using:

  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

Appreciate any help!

1 Answers

It seems it's an issue with purrr::compose and flextable::compose.

This should work:

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

Related