2

I have a tibble in R with nested rows and I created a ggplot object per row. I would like to print these graphics using reactable's expandable rows feature.

I think an example will explain this best. I'll use iris for simplicity. First, we take iris, nest it by "Species", and then make a column called "plot" that contains ggplot objects:

library(dplyr)
library(reactable)
library(ggplot2)

df = iris %>%
  as_tibble() %>%
  nest_by(Species) %>%
  mutate(plot = list(
    ggplot(data, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + ggtitle(Species)
  ))

It is possible to make a reactable object on our dataframe that spits out a nested reactable when you click on any row:

reactable(df %>% select(Species), details = function(index) {
    reactable(df$data[[index]])
  }
)

BUT...I can't figure out how to do the equivalent thing for printing the "plot" column. For example, this doesn't work:

reactable(df %>% select(Species), details = function(index) {
    print(df$plot[[index]])
  }
)

I've poked around with some HTML things like htmltools::img(df$plot[[index]]) but it's all not working.

Any thoughts?? Thanks!!!!!

4

2 回答 2

1

如果我们用 包裹ggplotly,那么它会起作用

library(plotly)
library(reactable)
library(ggplot2)
library(dplyr)
reactable(df %>% 
    select(Species), details = function(index) {
 ggplotly(df$plot[[index]])
})

-输出

在此处输入图像描述

于 2021-07-22T22:17:40.643 回答
1

我实际上在不需要的情况下解决了这个问题plotly——你可以使用htmltools::plotTag. (谁知道?)检查一下:

reactable(df %>% select(Species), details = function(index) {
  htmltools::plotTag(df$plot[[index]], alt="plots")
})
于 2021-07-28T18:48:07.610 回答