代码之家  ›  专栏  ›  技术社区  ›  Ashmin Kaul

使用shiny和plotly显示在R中创建的条形图的值

  •  0
  • Ashmin Kaul  · 技术社区  · 7 年前

    如果您运行下面的R shiny脚本,我们在仪表板中得到两个框,左侧框有一个条形图,右侧有一个DT表,当我使用event\u数据(“plotly\u click”)单击图表的任何一个条时,我希望相应的员工显示在表中,就像单击第一个条时一样,“r1”应该显示在表中。我试图执行“user\u cases$base1[d[3]]”,但它抛出了一个错误,即“错误:无效的下标类型‘list’”。我会附上快照作为参考,请帮我做同样的事情。

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    library(ggplot2)
    library(plotly)
    library(DT)
    ui <- dashboardPage(
    dashboardHeader(title = "Sankey Chart"),
    dashboardSidebar(
    width = 0
    ),
    dashboardBody(
    box(title = "Sankey Chart", status = "primary",height = "455" ,solidHeader = 
    T,
    plotlyOutput("sankey_plot")),
    
    box( title = "Case Summary", status = "primary", height = "455",solidHeader 
    = T, 
         dataTableOutput("sankey_table"))
    )
    )
    server <- function(input, output) 
    { 
    
    output$sankey_plot <- renderPlotly({
    height2 = c(56,45,23,19,8)
    base1 = c("r1","r4","r2","r5","r3")
    user_cases = data.frame(base1,height2)
    pp1 <<- ggplot(user_cases, aes(x = reorder(base1,-height2), y = height2)) + 
      geom_bar(stat = "identity", fill = "#3399ff" ) + scale_y_discrete(name 
    ="Cases") + scale_x_discrete(name = "Employee")
    ggplotly(pp1, tooltip="text",height = 392)
    })
    output$sankey_table <- renderDataTable({
    d <- event_data("plotly_click")
    user_cases$base1[d[3]]
    })
    }
    shinyApp(ui, server)
    

    DT table with bar chart

    要提取的数据集

    我正试图从bupaR图书馆的患者数据集中获取数据子集。执行此操作的代码如下:

    patients_final <- patients[patients$employee == as.data.frame( 
    user_time$employee[as.numeric(d[3])])]
    

    但我得到的错误是:“无法使用矩阵或数组进行列索引”附加快照以获得帮助。

    Snapshot for bar chart

    1 回复  |  直到 7 年前
        1
  •  1
  •   SBista    7 年前

    看一下修改后的代码,我改了 user_cases$base1[d[3]] 至as。数据帧(user\u cases$base1[as.numeric(d[3])])

     ## app.R ##
      library(shiny)
      library(shinydashboard)
      library(ggplot2)
      library(plotly)
      library(DT)
    
    
      height2 = c(56,45,23,19,8)
      base1 = c("r1","r4","r2","r5","r3")
      user_cases = data.frame(base1,height2)
    
    
      ui <- dashboardPage(
        dashboardHeader(title = "Sankey Chart"),
        dashboardSidebar(
          width = 0
        ),
        dashboardBody(
          box(title = "Sankey Chart", status = "primary",height = "455" ,solidHeader = 
                T,
              plotlyOutput("sankey_plot")),
    
          box( title = "Case Summary", status = "primary", height = "455",solidHeader 
               = T, 
               dataTableOutput("sankey_table"))
        )
      )
    
    
      server <- function(input, output) 
      { 
    
        output$sankey_plot <- renderPlotly({
    
          pp1 <<- ggplot(user_cases, aes(x = reorder(base1,-height2), y = height2)) + 
            geom_bar(stat = "identity", fill = "#3399ff" ) + scale_y_discrete(name 
                                                                              ="Cases") + scale_x_discrete(name = "Employee")
          ggplotly(pp1, tooltip="text",height = 392)
        })
        output$sankey_table <- renderDataTable({
          d <- event_data("plotly_click")
         as.data.frame( user_cases$base1[as.numeric(d[3])])
        })
      }
      shinyApp(ui, server)
    

    输出如下:

    enter image description here

    dataframe 根据您的要求输出。

    希望有帮助!