Search code examples
rggplot2plotlyshinyggplotly

Displaying the value of bar created in R using shiny and plotly


If you run the R shiny script below, we get two boxes in a dashboard, the left box has a bar chart and right has a DT table, when I click on any bar of the chart using event_data("plotly_click"), I want the corresponding Employee to be displayed in the table besides, like when clicked on first bar, "r1" should be displayed in the table besides. I tried doing "user_cases$base1[d[3]]" but it throws an error as "Error: invalid subscript type 'list'". I will attach the snapshot for the reference, please help me with the same.

## 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

Dataset to be fetched

I am trying to fetch subset of the data from the patients dataset from bupaR library. The code for doing it is as follows:

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

but the error I get is: "Can't use matrix or array for column indexing" attaching the snapshot for the help.

Snapshot for bar chart


Solution

  • Have a look at the modified code, I have changed user_cases$base1[d[3]] to as.data.frame(user_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)
    

    The output is as below:

    enter image description here

    You can modify the dataframe output as per your requirement.

    Hope it helps!