I have just started learning ShinyApp and am trying to create a table whose values can dynamically filtered.
The result I want
<filter>GlassSupplier Supplier1
WindowType AverageBreakageRate
Aluminum 3.63
Wood 7.22
The result I get.
<filter>GlassSupplier Supplier1
WindowType AverageBreakageRate
Aluminum 2.815
Vinyl 6.165
Wood 7.22
My code creates a table but does not filter based on the select input selection. Also is there a way to add an action button, so the table reflects changes due to a new select input parameter only when the action button is hit?
Any help will be greatly appreciated!
library(shiny)
library(dplyr)
library(readxl)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "left",
sidebarPanel("sidebar panel",
selectInput(inputId = "table",
label = "Choose a Supplier",
"Names"),
),
mainPanel("main panel",
tableOutput("myTable")
)))
server <- function(input, output,session)
{
GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
BreakageRate <- c(7.22,6.33,3.63,2,6)
df<- data.frame(GlassSupplier,WindowType,BreakageRate)
data <- reactive({
req(input$table)
dframe <- df %>% group_by(WindowType) %>% summarise(BrkRate = mean(BreakageRate))
})
#Update SelectInput Dynamically
observe({
updateSelectInput(session, "table", choices = df$GlassSupplier)
})
output$myTable = renderTable({
data()
})
}
shinyApp(ui,server)
CodePudding user response:
You just need to filter
. For the actionButton
to work, just change the reactive()
to eventReactive()
object. Try this
library(shiny)
library(dplyr)
library(readxl)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "left",
sidebarPanel("sidebar panel",
selectInput(inputId = "table",
label = "Choose a Supplier",
"Names"),
actionButton(inputId = "btn",label="Update")
),
mainPanel("main panel",
tableOutput("myTable")
)))
server <- function(input, output,session)
{
GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
BreakageRate <- c(7.22,6.33,3.63,2,6)
df<- data.frame(GlassSupplier,WindowType,BreakageRate)
data <- eventReactive(input$btn, {
req(input$table)
df %>% dplyr::filter(GlassSupplier %in% input$table) %>%
group_by(WindowType) %>%
dplyr::summarise(BrkRate = mean(BreakageRate))
})
#Update SelectInput Dynamically
observe({
updateSelectInput(session, "table", choices = df$GlassSupplier)
})
output$myTable = renderTable({
data()
})
}
shinyApp(ui,server)