【发布时间】:2018-07-10 20:22:44
【问题描述】:
我正在尝试根据动画情节图中的选定帧过滤闪亮的情节(对于这个应用程序:http://www.seabbs.co.uk/shiny/ExploreGlobalTB/#)。这是否可以使用当前的 event_data 实现(供参考:https://plotly-book.cpsievert.me/linking-views-with-shiny.html)?通过阅读ropensci GitHub,我认为这还没有实现。如果没有其他获得此功能的建议?
图之间反应性的工作示例(点击时)如下(警告此代码使用 getTBinR 并将从此处提取 WHO TB 数据 (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)
此数据的框架是 ggplot 对象中的 Year,然后使用 plotly 进行转换(映射函数代码:https://github.com/seabbs/getTBinR/blob/improved_interactive_plots/R/map_tb_burden.R)。理想情况下,我可以使用 event_data 框架,但如果不这样做,是否可以从 plotly 对象中选择当前帧?
根据 MLavoie 的回答修改的第二个示例。只是为了澄清目的是在动画结束时提取点击国家和年份。这两个示例都在点击时提取国家/地区,以下内容清楚地表明年份并没有随着动画而动态变化。
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)
【问题讨论】:
-
不清楚你想要什么。您需要使用 map_tb_burden 吗?还是 ploy_geo 没问题?当您单击一个国家/地区时,您是否尝试从地图中提取一些信息?
-
道歉。最后一个用例是使用这个函数,但是任何其他方法的例子都很好。目前,上述代码在点击时提取国家级数据。我也在寻找一种提取当前动画帧的方法。这可能是使用相同的事件数据调用、第二个事件数据调用或其他一些机制。