I am trying to build an app which displays a dataframe as a datatable. I wanted to provide users with ability to subset the datatable with selectinput and daterangeinput filters on the sidebar panel.
Heres when I ran into issues,
- it only subsets the datatable when all the inputs are selected
- it displays an empty datatable on loading the app
- when one of the inputs are changed back it doesnot update the datatable.
Here is my sample code for shiny app
Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
Company <- c("A","B","C","A","B","C","A")
Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
date <- as.Date(date,"%Y-%m-%d")
df <- data.frame(Region, Company, Status, date)
The ui file is
ui.R
shinyUI(
fluidPage(
titlePanel("Summary"),
sidebarLayout(
sidebarPanel(
selectInput("region",
label = "Choose a Region",
choices = c("", unique(df$Region))),
selectInput("company",
label = "Choose a company",
choices = c("", unique(df$Company))),
selectInput("status",
label = "Choose Proj status",
choices = c("", unique(df$Status))),
dateRangeInput("projdate",
label = "Date of interest:",
start = Sys.Date() - 60,
end = Sys.Date())
),
mainPanel(
dataTableOutput("table")
)
)
))
The Server file is
server.R
shinyServer(function(input,output){
output$table <- renderTable({
datadf %>% as.data.frame() %>% filter(Region == input$region & Company == input$company & Status == input$status)
})
})
When the app launches it does not display the table without any filter applied. I am not sure if I am doing everything in the output$table function to get the output I desire.
If someone could guide me as to how I can display the datatable without any user input and then the datatable updates as userinputs are changed.
I tried placing the input factors in a reactive environment but then operation not allowed without an reactive context pops up.
See Question&Answers more detail:os