Home > OS >  In R and using Plotly how can I extract the dataframe related to the zoom-in tool?
In R and using Plotly how can I extract the dataframe related to the zoom-in tool?

Time:06-11

This my code:

library(gapminder)

ggplotly(ggplot(gapminder %>% filter(country == "Chile"))   aes(x = year, y = pop)   geom_line())

Then I use the zoom in tool:

enter image description here

Then I have this output:

enter image description here

My question is: How can I access/extract the dataframe related to this output?

Any help ?

CodePudding user response:

I guess you need this: We can do it with crosstalk:

enter image description here

library(plotly)
library(crosstalk)
library(DT)

options(persistent = FALSE)

my_data <- SharedData$new(gapminder %>% filter(country=="Chile"))

my_plot <- plot_ly(my_data, x = ~year, y = ~pop, type = 'scatter', mode = 'lines') %>% 
  add_markers(alpha = 0.5) %>%
  highlight("plotly_selected", dynamic = TRUE)

my_table <- datatable(my_data)
bscols(widths = c(6, 4), my_plot, my_table)
  • Related