Flexdashboard:根据下拉输入制作反应式数据框

Flexdashboard: Make a reactive dataframe based on drop down inputs

我正在尝试创建一个数据框,其内容会根据仪表板用户的输入而变化。我已经像这样设置了空数据框和输入:

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")

我想对 beta 数据框进行编码,这样如果用户从 ai_issue 对象的下拉列表中选择 'Environment',beta 数据框内的相应单元格将更改为 1。有人知道怎么做吗?

此示例使用 multiple 代替 inputSelect,因此您可以指定一个或多个选项。

reactive 函数将根据 input 选择设置您的数据框变量。

我希望这可以帮助您入门。如果这不是您想要的,请告诉我。

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)