Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r - how do I print ggplot objects from a nested tibble in a reactable with expandable rows?

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

like image 463
user1713174 Avatar asked Oct 24 '25 04:10

user1713174


1 Answers

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")
})
like image 64
user1713174 Avatar answered Oct 25 '25 19:10

user1713174



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!