正如@marc p所建议的,您可以只关注
names(mtcars)
通过提供给
selected
参数。这也有助于排除在
input$select
是
null
.
library(shiny)
ui = basicPage(
selectInput("select", "Select columns to display",
names(mtcars),
selected = names(mtcars)[c(1, 3)], # display 1st and 3rd variables
multiple = TRUE),
h2('The mtcars data'),
dataTableOutput('mytable')
)
server = function(input, output) {
output$mytable = renderDataTable({
mtcars[, input$select, drop=FALSE]
})
}
shinyApp(ui, server)