3

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!!!!!

user1713174
  • 307
  • 2
  • 7

2 Answers2

3

I actually solved this without requiring plotly -- you can use htmltools::plotTag. (Who knew?) Check it out:

reactable(df %>% select(Species), details = function(index) {
  htmltools::plotTag(df$plot[[index]], alt="plots")
})
user1713174
  • 307
  • 2
  • 7
1

If we wrap with ggplotly, then it would work

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

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662