使用 selectinput 从数据框中提取列值

Extract column values from dataframe with selectinput

例如,我有一个数据框 mtcars,我只想根据 selectinput 中的变量选择提取列值。

示例代码:

shinyApp(
  ui = fluidPage(
    varSelectInput("variable", "Variable:", mtcars),
    verbatimTextOutput('data')
  ),
  server = function(input, output) {
   
    output$data <- renderText({
      mtcars$input$variable
    })
   
  }
)

例如:mtcars$mpg

我只想提取 mpg 向量值: 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 17.4 16.4 17.3 17.3 15.2 10.4 10.4 10.4 14.7 32.4 33.9 33.9 21.5 15.5 15.5 15.5 15.2 13.3 19.3 19.2 27.2 27.3 27.3 27.3 26.0 36.0 30.4 15.8 19.7 15.7 15.7 15.7 15.7 15.0 21.4 [= 12 = 12 = 12 = 12 = 12 =]

试试这个

output$data <- renderText({
      paste(mtcars[[as.name(input$variable)]])
})