I want to create a simple shiny app.In the app I upload a csv file. Here the csv file that I use is mtcars.csv. Then there are two selectInputs. I populate the first one with the columns of the uploaded csv file. And the second one is populated by all values for the field that is already selected in the first selectInput. Until here everything is fine. The problem starts when I click the actionButton: I want to renderTable all the records of the uploaded csv where the first selectInput is equal to the second one. For example, if the csv file is mtcars and first selectInput is cylinder and the sencond one is 6, I want to renderTable all the cars that have 6 cylinders.
However, the result is always empty. Here is the code for my UI:
ui <- fluidPage(
# Application title
titlePanel("This is a test!"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Browse",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
selectInput("first","Variable Name", choices = NULL),
selectInput("second","Ranges", choices = NULL),
actionButton("btn","Proceed")
),
mainPanel(
tableOutput("out")
)
)
)
And here is my server:
server <- function(input, output, session) {
#storing the csv in df
df = reactive({
req(input$file)
return(read_csv(input$file$datapath))
})
#populating first inputSelect
observe({
choices1 = colnames(df())
updateSelectInput(session,"first", choices = choices1)
})
#populating second inputSelect
observeEvent(input$first,{
choices1 = df() %>%
select(input$first) %>%
unique()
updateSelectInput(session,"second", choices = choices1)
})
#The goal is to return all rows in df() where the values
#of column in first selectInput is equal to second selectInput
observeEvent(input$btn,{
x = df() %>%
filter(input$first == input$second) #problem: this is always empty
output$out = renderTable(x)
})
}
I'm new to shiny. And I'm looking for the right way to do this. Here's the snapshot of the empty output: