微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据 R Shiny 中的下拉值创建钻取?

如何解决如何根据 R Shiny 中的下拉值创建钻取?

我想创建一个钻取图表。基于 https://plotly-r.com/linking-views-with-shiny.html#drill-down 链接,我为特定变量创建了下钻,但现在我必须根据下拉值构建下钻。如何创建?

data:
col1   col2   sales 
A       E       20
B       G       10
B       H       15
A       H        5
ui.R:
fluidRow(                                                                                                                                   
   column(width=4,selectInput("colid","Select",choices = c("col1"))),),plotlyOutput('drilldown1',height = 500),plotlyOutput("drilldown2",height = 500)
                                                                    
server.R :
if(input$colid==as.character("col1")){
    drilldown1 <- reactiveVal()
    drilldown2 <- reactiveVal()
    observeEvent(event_data("plotly_click",source = "drilldown1"),{
        drilldown1(event_data("plotly_click",source = "drilldown1")$x)
        drilldown2(NULL)})
        
     observeEvent(event_data("plotly_click",source = "drilldown2"),{
        drilldown2(
            event_data("plotly_click",source = "drilldown2")$x
        )})
   output$drilldown1<- renderPlotly({
  
    df <- data %>%
       dplyr::group_by(col1) %>%
       dplyr::summarise('Mean'=round(mean(sales,na.rm=TRUE),digits =0))
                         )
    fig1 <-plot_ly(x = ~df$col1,y = ~df$Mean,type='bar',source = 
           "drilldown1")
    fig1

  
})

output$drilldown2<- renderPlotly({
  if (is.null(drilldown1())) return(NULL)
  
  df <- data %>%
    dplyr::filter(col1 %in% drilldown1()) %>%
    
    dplyr::group_by(col2) %>%
    dplyr::summarise('Mean'=round(mean(sales,digits = 0))
)
fig2 <-plot_ly(x = ~df$col2,source = 
               "drilldown2")
fig2


})

        
        
    

}
I am getting below error:

Error in : Can't access reactive value 'colid' outside of reactive consumer. 

请帮帮我。我已经搜索过这个我无法得到明确的答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。