Search code examples
rshinydplyrflexdashboard

Flexdashboard: Make a reactive dataframe based on drop down inputs


I am trying to create a data frame whose content changes based on the inputs of the user of the dashboard. I have set up the empty data frame and the input like so:

selectInput("ai_issue", 
            label = "Select Issue Area:",
            choices = c("Environment",
                        "Human rights",
                        "Refugee relief"))

beta <- as.data.frame(matrix(rep(0), nrow = 3))
beta$levels = c("Environment", "Human Rights", "Refugee Relief")

I am wanting to code the beta data frame so that if the user selects 'Environment' from the drop down of the ai_issue object the appropriate cell within the beta data frame will change to 1. Does anyone know how to do this?


Solution

  • This example uses multiple for inputSelect so you can indicate one or more choices.

    A reactive function will set your data frame variable depending on the input selection.

    This is something I hope can help in getting you started. If this is not what you had in mind please let me know.

    library(shiny)
    
    beta <- data.frame(
      value = c(0,0,0),
      levels = c("Environment", "Human Rights", "Refugee Relief")
    )
    
    ui <- fluidPage(
      fluidRow(
        column(6,
          selectInput("ai_issue", 
                      label = "Select Issue Area:",
                      choices = beta$levels,
                      multiple = TRUE)
        ),
        column(6,
          tableOutput("data")
        )
      )
    )
    
    server = function(input, output) {
    
      df <- reactive({
        beta$value[beta$levels %in% input$ai_issue] <- 1
        beta
      })
    
      output$data <- renderTable({
        df()
      })
    }
    
    shinyApp(ui, server)