I am trying to filter a plot in shiny based on the selected frame in an animated plotly plot (for this app: http://www.seabbs.co.uk/shiny/ExploreGlobalTB/#). Is this possible using the current event_data implementation (for reference: https://plotly-book.cpsievert.me/linking-views-with-shiny.html)? From reading through the ropensci GitHub I don't think this has been implemented. If not any other suggestions for getting this functionality?
A working example of reactivity between plots (on clicking) is as follows (warning this code uses getTBinR and will pull WHO TB data from here (http://www.who.int/tb/country/data/download/en/).
#install.packages(shiny)
library(shiny)
# install.packages(devtools)
# devtools::install_github("seabbs/getTBinR", ref = "improved_interactive_plots")
library(getTBinR)
ui <- fluidPage(
plotlyOutput("map_tb_burden"),
verbatimTextOutput("country")
)
server <- function(input, output, session) {
# Make map of metric
output$map_tb_burden <- renderPlotly({
map <- map_tb_burden(interactive = TRUE, year = c(2000:2016))
})
#Get country clicked on map
country <- reactive({
country <- event_data(event = "plotly_click", source = "WorldMap")
country <- country$key[[1]]
})
output$country <- renderText({
if (is.null(country())) {
"Select a country for more information"
} else {
paste0("Showing data for ", country())
}
})
}
shinyApp(ui, server)
The frame for this data is Year in the ggplot object which is then converted using plotly (mapping function code: https://github.com/seabbs/getTBinR/blob/improved_interactive_plots/R/map_tb_burden.R). Ideally I would be able to use the event_data framework, but failing this would it be possible to select the current frame from the plotly object?
A second example modified from MLavoie's answer. Just to clarify the aim is to extract both country on clicking and year as this is animated over. Both examples extract country on clicking, the following makes it clear that year is not changing dynamically as it is animated.
library(shiny)
library(plotly)
library(getTBinR)
tb_burden <- get_tb_burden()
ui <- fluidPage(
plotlyOutput("map_tb_burden"),
dataTableOutput("country")
)
server <- function(input, output, session) {
# Make map of metric
output$map_tb_burden <- renderPlotly({
key <- tb_burden$iso3
g <- list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type = 'Mercator')
)
plot_ly(data = tb_burden, frame = ~year, type = 'choropleth', z = ~e_inc_100k, text = ~country, color = ~e_inc_100k, colors = "Reds", locations = ~iso3, key = key) %>%
layout(geo = g)
})
#Get country clicked on map
countryB <- reactive({
d <- event_data("plotly_click")
ff <- data.frame(d[3])
ff
})
output$country <- renderDataTable({
d <- event_data("plotly_click")
# if (is.null(d))
# return(NULL)
withProgress(message = 'Click on a country', {
validate(
need(d, 'Click on a country!')
)
testing <- tb_burden %>% filter(iso3 == countryB()$key)
testing
})
})
}
shinyApp(ui, server)