I am trying to run the code below to filter a dataframe based on the selection made in the plot.
When I replace filter(category()==1 with English==1 or Math==1, it works fine, but i would like to use it dynamically based on the selection made in the Bar plot.
Also, how can i make the bar selected a different color? Like when English is selected, I would like to show it as Blue and other bars faded.
library(shinydashboard)
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(plotly)
subject <- c('Math','Science','English')
TotStudents <- c(13, 15, 20)
sub_freq <- data.frame(subject, TotStudents)
sub_freq
name <- c('Sam', 'John', 'David', 'Bill', 'Jack', 'Peter', 'Zach')
Math <- c(1,0, 1, 1, 0, 1, 1)
Science <-c(0,0, 1, 1, 1, 0, 1)
English <-c(1,1, 0, 1, 0, 1, 0)
name_flag <- data.frame(name, Math, Science, English)
name_flag
ui <- dashboardPage(
dashboardHeader(title = "Test Dashboard",titleWidth = 200),
dashboardSidebar(
),
dashboardBody(
plotlyOutput("cond", height = "500px", width="600px"),
br(),
uiOutput("imageLink"),
br(),
DTOutput("datatable")
)
)
server <- function(input, output) {
category <- reactiveVal()
observeEvent(event_data("plotly_click", source = "imgLink"), {
category(event_data("plotly_click", source = "imgLink")$x)
})
Create text paragraph of info on a selected point
output$imageLink <- renderText({
event.data <- event_data(event = "plotly_click", source = "imgLink")
if (is.null(event.data)) {
print("You can click on a bar to select a subject.")
} else {
category()
}
})
output$cond <- renderPlotly({
event.data <- event_data(event = "plotly_selected", source = "imgLink")
sub_freq %>%
plot_ly(x = ~subject, y = ~TotStudents, source = "imgLink") %>%
layout(title = "Student Count by Subject")
})
output$datatable<- renderDataTable({
name_flag
mbrs <- name_flag %>%
filter(category()==1)
})
}
shinyApp(ui, server)