我无法使用Shiny中的radioButtons和selectInput函数来选择/取消选择mtcars数据集的不同列。 自从过去两天以来我一直坚持下去,有人可以帮我吗。 我将非常感谢。
致谢
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
column(width = 10,
radioButtons(inputId="variables", label="Select variables:",
choices = c("All","mpg","cyl","disp"),
selected = "All", inline = TRUE )),
column(width = 10,
selectInput(inputId = "level", label = "Choose Variables to
display", multiple = TRUE, choices = names(mtcars)[4:11]))),
mainPanel (
h2("mtcars Dashboard"),
DT::dataTableOutput("table"))))
#server
server<-function(input, output) {
output$table <- DT::renderDataTable(DT::datatable(filter='top', editable = TRUE, caption = 'mtcars',
{
data <- mtcars
data<-data[,input$variables,drop=FALSE]
column = names(mtcars)
if (!is.null(input$level)) {
column = input$level }
data
})) }
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
library(shiny)
library(DT)
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
column(width = 10,
radioButtons(inputId="variables", label="Select variables:",
choices = c("All","mpg","cyl","disp"),
selected = "All", inline = TRUE )),
column(width = 10,
selectInput(inputId = "level", label = "Choose Variables to
display", multiple = TRUE, choices = names(mtcars)[4:11]))),
mainPanel (
h2("mtcars Dashboard"),
DT::dataTableOutput("table"))
))
#server
server<-function(input, output, session) {
data <- mtcars
tbl <- reactive({
if(input$variables=='All'){
data
}else{
data[,c(input$variables,input$level),drop=FALSE]
}
})
output$table <- DT::renderDataTable(DT::datatable(filter='top', caption='mtcars', tbl()))
}
shinyApp(ui = ui, server = server)
这是我从您的要求中了解的内容,希望它能为您提供所需。始终尝试避免在render *中进行计算。