1
votes

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?

1
Would you describe a bit more about what you wish to build? Are you sure the drop down selectInput meets your needs (rather than, for example, an "add" button)? If someone selects "Human rights" or "Refugee relief" will the data change as well? And if you select a choice repeatedly going back and forth between choices should the data continue to change? Likely, you could use something like a reactiveVal to contain a manipulated data frame. - Ben
@Ben yes, I am trying to build a conjoint model. What we are wanting to do is to have a data frame that we 'turn on' different features. We have the beta means for each factor that we are going to then use matrix multiplication to find the utility and market share given the selections from the drop down. - Jeff Gallini

1 Answers

2
votes

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)