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

使用Shining和plotly显示在R中创建的条形图的值,r,ggplot2,plotly,shiny,ggplotly,R,Ggplot2,Plotly,Shiny,Ggplotly,如果您运行下面的R Shining脚本,我们会在仪表板中看到两个框,左侧框有一个条形图,右侧框有一个DT表,当我使用事件数据单击图表的任何一个条形图(“plotly_click”)时,我希望相应的员工显示在表中,就像单击第一个条形图时一样,“r1”除此之外,还应在表中显示。我尝试执行“user_cases$base1[d[3]]”,但它抛出一个错误“error:invalid subscript type‘list’”。我会附上快照作为参考,请帮我做同样的事情 ## app.R ## libra

如果您运行下面的R Shining脚本,我们会在仪表板中看到两个框,左侧框有一个条形图,右侧框有一个DT表,当我使用事件数据单击图表的任何一个条形图(“plotly_click”)时,我希望相应的员工显示在表中,就像单击第一个条形图时一样,“r1”除此之外,还应在表中显示。我尝试执行“user_cases$base1[d[3]]”,但它抛出一个错误“error:invalid subscript type‘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)
##app.R##
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(GG2)
图书馆(绘本)
图书馆(DT)

ui看看修改后的代码,我已经将
user\u cases$base1[d[3]]
更改为as.data.frame(user\u cases$base1[as.numeric(d[3]))

##app.R##
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(GG2)
图书馆(绘本)
图书馆(DT)
高度2=c(56,45,23,19,8)
base1=c(“r1”、“r4”、“r2”、“r5”、“r3”)
user_cases=data.frame(base1,height2)

ui你能提供一个示例数据吗?@SBista,谢谢你的回复,代码里有。请运行脚本并进行检查。非常感谢您的帮助,我有一个更进一步的加载项要求,我使用您的“as.data.frame(user_cases$base1[as.numeric(d[3])”)从使用bupaR包获得的“patients”数据集中获取行。但是,我得到了一个错误,就像在新快照中一样。我在这个问题上增加了一部分。请在这里帮助我。由于您已将其包装在data.frame周围,可能这就是出现此错误的原因,也许可以进行一些调整。什么是
patients3
。我添加了
as.data.frame
,因为
renderDataTable
需要
数据框。如果您的输出将是一个
数据帧
大道歉,即患者数据集,那么您可以删除该部分。数据中的“employee”列具有与我们从“as.data.frame(user_cases$base1[as.numeric(d[3])”公式中获得的值相同的值,我需要每个员工对应的相应患者行,然后您可以将
as.data.frame(user\u cases$base1[as.numeric(d[3]))
替换为
user\u cases[as.numeric(d[3]),]
patients_final <- patients[patients$employee == as.data.frame( 
user_time$employee[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)