Shiny App中的SliderInput和SelectInput无法正常工作

时间:2018-10-21 08:50:11

标签: r shiny dataset dashboard

有人可以帮助我解决mtcars数据集上的sliderinput和selectinput的Shiny App查询吗? 我将非常感谢。

library(shiny)
library(DT)

ui

ui <- fluidPage(titlePanel("mtcars table"),
            sidebarLayout(
              sidebarPanel(

                column(width = 5,
                       selectInput(inputId = "gear",
                                   label = "gear:",
                                   choices = c("All",
                                               unique(as.character(mtcars$gear))))),

                column(width = 10,
                       sliderInput(inputId = "am",
                                   label = "am:",
                                   min = 0,
                                   max = 1,
                                   value = c(0,1),
                                   step = 1)),


                column(width = 10,
                       sliderInput(inputId = "hp",
                                   label = "hp:",
                                   min = 52,
                                   max = 335,
                                   value = c(52,335),
                                   step = 1)

                )),

              mainPanel ( 
                DT::dataTableOutput("table"))))

服务器

server<-function(input, output) {

output$table <- DT::renderDataTable(DT::datatable({
data <- mtcars

ifelse(test = input$gear != 'All', yes =data<- data[data$gear == input$gear,], 0)
data<-data[data$am == input$am,] 
data<-data[data$hp==input$hp,]

data
}))

}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

library(shiny);
library(DT)

ui <- fluidPage(titlePanel("mtcars table"), sidebarLayout( sidebarPanel(

  column(width = 5,
         selectInput(inputId = "gear",
                     label = "gear:",
                     choices = c("All" = 0,
                                 unique(as.character(mtcars$gear))))),

  column(width = 10,
         selectInput(inputId = "am",
                     label = "am:",
                     choices = unique(as.character(mtcars$am)))),


  column(width = 10,
         sliderInput(inputId = "hp",
                     label = "hp:",
                     min = 52,
                     max = 335,
                     value = c(52,335),
                     step = 1)

  )),

  mainPanel ( 
    DT::dataTableOutput("table"))))


server<-function(input, output) {

  output$table <- DT::renderDataTable(DT::datatable({ 

  data <- mtcars

  if(input$gear != 0){
    data<-data[data$gear == input$gear, ]
  }
  data<-data[data$am %in%  input$am,] 
  data<-data[data$hp >= input$hp[[1]] & data$hp <= input$hp[[2]],]


  data
  }))

}

shinyApp(ui = ui, server = server)