1

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:

enter image description here

1 Answer 1

3

I believe this should work

observeEvent(input$btn,{
      x = df()[df()[,input$first] == input$second,]
      output$out = renderTable(x)
   })

The problem is you are doing this

filter("cyl" == 6) 

you need to do this

filter(cyl == 6)

This is happening because input$first = "cyl" is a string. So your code is comparing a string with a number, which is giving zero rows on filter.

Sign up to request clarification or add additional context in comments.

4 Comments

If you're set to using dplyr::filter, you could also do something like: filter(!! rlang::sym(input$first) == input$second)
Thank you both. @Aurèle , now I'm facing another similar problem in ggplot2. I can pass parameters as you specified in aes. However, I receive error when I want to do something similar in facet_grid. for example facet_grid(~(!!rlang::sym(var2))) or facet_grid(~!!rlang::sym(var2)) throws error. I also don't get it why we need to use !! with sym?. Any help is appreciated.
@wastepaper You should make it a new question (tagged with rlang). You can look up "quasiquotation", it's now fully baked in dplyrbut still not completely into ggplot2, that might be the cause of your problem... Oh, wait, actually: have you tried the simpler: facet_grid(var2) (without the tilde ~)?
@Aurèle I found a way: facet_wrap(as.formula(paste("~", var2 )))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.