0

I am not new to R but I am new to R-Shiny and am trying to build an app that queries data (I do not know if there is a better term to describe what I am trying to do.)

Essentially, the app has a series of questions using inputs in the UI that is then supposed to narrow down the data to match the user's specific preferences. I have been referring to it as a "buzzfeed quiz" because that the most non-technical thing I can think of that does the same thing. Regardless, I may have bitten off more than I can chew because I am having trouble getting the syntax correct in my code to get it to query even one question. I think once I get one to work I should be able to do the other questions.

UI <-fluidpage(
    tabPanel(
      "Find My AVF",
      headerPanel("What Your AVF Match?"),
  radioButtons(inputId = "q1", label = h3("How many people are in your household?"),
               choices = list("1 to 2" = 1, "3 to 5" = 2, "6 to 7" = 3, "8" = 4, "9 or more" = 5), 
               selected = 1),
  actionButton("execute", label = "Find my Perfect Car!"),
  tableOutput("resulttable")
    )

server <- function(input,output){
  q1a <- reactive({
    if (input$q1 == "1 to 2"){
      q1dat <- Seats < 6
    } else if (input$q1 == "3 to 5") {
      q1dat <- Seats > 2 & Seats < 6
    } else if (input$q1 == " 6 to 7") {
      q1dat <- Seats > 5 
    } else if (input$q1 == "8"){
      q1dat <- Seats > 7 
    } else if (input$q1 == "9 or more") {
      q1dat <- Seats = 9
    }
  })
  result <- eventReactive(input$execute, {
    output$resulttable <- renderTable(q1a
    )
  })
  }

I was hoping to simply get R to reduce my dataset down to based on what the user clicks on in the UI and what it is attached to in the code behind it. If the person selects 3 to 5 people, I want it to get the dataset down to only vehicles that have between two and six seats. Currently, the app does nothing. The UI works fine, but the action button does not perform any actions.

LJCulp
  • 1
  • You'll have to be a little more clear with what you're attempting. with `q1dat <- Seats = 9` within a reactive function that stores into q1a, its really not clear what you want. I recommend using the `mtcars` built-in object to create a better example. Hint: use `carb` for the number of seats, or append your own column. – hedgedandlevered May 01 '19 at 00:17

1 Answers1

1

Since a reproducible example was not provided, I will use the mtcars dataset to demonstrate a working example:

library(shiny)
library(dplyr)

ui <-fluidPage(
  tabPanel(
    "Find My AVF",
    headerPanel("What Your AVF Match?"),
    radioButtons(inputId = "q1",
                 label = h3("How many people are in your household?"),
                 choices = list(
                   "1 to 2" = 1,
                   "3 to 5" = 2,
                   "6 to 7" = 3,
                   "8" = 4,
                   "9 or more" = 5
                   ), 
                 selected = 1),
    actionButton("execute", label = "Find my Perfect Car!"),
    tableOutput("resulttable")
  ))

  server <- function(input,output){
    q1a <- reactive({
      if (input$q1 == 1){
        q1dat <- mtcars %>%
          filter(cyl < 6)
      } else if (input$q1 == 2) {
        q1dat <- mtcars %>%
          filter(cyl > 2 & cyl < 6)
      } else if (input$q1 == 3) {
        q1dat <- mtcars %>%
          filter(cyl > 5 )
      } else if (input$q1 == 4){
        q1dat <- mtcars %>%
          filter(cyl > 7 )
      } else if (input$q1 == 5) {
        q1dat <- mtcars %>%
          filter(cyl == 9)
      }
      return(q1dat)
    })
    observeEvent(input$execute, {
      output$resulttable <- renderTable(q1a())
    })
  }

shinyApp(ui, server)

Several issues are preventing your app from running as expected. Your example is treating Seats as a dataset, where I assume it is a variable within another dataset, which is replaced by mtcars and the variable cyl from it in the example. You need to subset your dataset using the respective conditional. Your reactive function q1a is also not returning a value when called, resolved with return(q1dat) after your control statements.

Your input with id q1 can be inspected with:

radioButtons(inputId = "q1",
                label = h3("How many people are in your household?"),
                choices = list(
                  "1 to 2" = 1,
                  "3 to 5" = 2,
                  "6 to 7" = 3,
                  "8" = 4,
                  "9 or more" = 5
                  ), 
                selected = 1)

You will see that the value of each button is a numeric from 1 to 5. This the value you need to test against in your control statements.

Finally, per Shiny: what is the difference between observeEvent and eventReactive?, you need to observe the event of the actionButton() with observeEvent() instead of eventReactive().

Raoul Duke
  • 435
  • 3
  • 13