首页 > 解决方案 > 通过单击 Plot Data Point 在 Shiny 中过滤数据

问题描述

我想通过单击 Shiny 中的绘图数据点来过滤数据,就像我们在 powerbi 中一样。我已经在powerbi中开发了一个仪表板我希望在闪亮时具有相同的效果,例如如果我单击闪亮仪表板中的绘图数据点,其他绘图应该向下钻取以响应该点,我已经在闪亮中构建了一个完整的仪表板但是我需要添加这些功能。如果我想知道约翰(数据点)的 2 月(数据点)月销售额,也可以有多个数据点向下钻取。

标签: javascriptrshinydrilldown

解决方案


在 UI 中,您应该添加、单击、双击或悬停:

 plotOutput("plot1", click = "plot_click")

并且在Server中会输入$plot_click,X和Y坐标

这里有一个闪亮的解释: https ://shiny.rstudio.com/articles/plot-interaction.html

我为你写了一个简单的例子:

library(shiny)
library(ggplot2)
library(MASS)

ui<- shinyUI(
       fluidPage(
                plotOutput("grafica", hover="clickGrafica"),    
                tableOutput("miverbatini")                      
        )
)
server<- shinyServer(function(input,output) {

        output$grafica <- renderPlot({

                ggplot(mpg,aes(x=cty, y=hwy)) +   
                        geom_point()
        })

        output$miverbatini <- renderTable({  
                nearPoints(mpg,input$clickGrafica, threshold = 10)  # near points 20  
        })
})
shinyApp(ui, server)

推荐阅读