如何从Shiny中的情节事件获取构面数据

时间:2019-01-18 20:32:39

标签: r shiny plotly r-plotly

我正在尝试shiny包括一些plotly的情节。我想使用反应性事件,因此当我单击图时,另一个图将根据过滤后的数据进行更新。事实是,我希望具有click事件的情节具有构面,并且我想根据所单击的构面过滤数据。但据我所知,事件信息未发送回方面信息。

我简化了shiny plotly events的示例以包含构面(请参见下面的reprex),但是没有任何事件接收到构面信息。特别是,我感兴趣的event_data("plotly_click")在任何地方都无法获得点击状态。

library(plotly)
library(shiny)

ui <- fluidPage(
    plotlyOutput("plot"),
    verbatimTextOutput("hover"),
    verbatimTextOutput("click"),
    verbatimTextOutput("brush"),
    verbatimTextOutput("zoom")
)

server <- function(input, output, session) {

    output$plot <- renderPlotly({
        # use the key aesthetic/argument to help uniquely identify selected observations
        key <- row.names(mtcars)
        p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
            geom_point() +
            facet_grid(rows = "gear")
        ggplotly(p) %>% layout(dragmode = "select")
    })


    output$hover <- renderPrint({
        d <- event_data("plotly_hover")
        if (is.null(d)) "Hover events appear here (unhover to clear)" else d
    })

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        if (is.null(d)) "Click events appear here (double-click to clear)" else d
    })

    output$brush <- renderPrint({
        d <- event_data("plotly_selected")
        if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
    })

    output$zoom <- renderPrint({
        d <- event_data("plotly_relayout")
        if (is.null(d)) "Relayout (i.e., zoom) events appear here" else d
    })


}

shinyApp(ui, server)

我一直在搜索它,找不到解决方案。例如,有什么我可以传递给render函数的东西,以便它获取构面并可以在事件数据中将其发送给bac吗?

1 个答案:

答案 0 :(得分:0)

好吧,您可以在修改的示例代码中看到它们“使用关键的美学/参数来帮助唯一地识别选定的观察结果”。为什么不使用它来识别构面?

您只需要调整代码中的一行,即可在key = gear中更改aes()