无法看到selectInput的选择并无法在ShinyApp中获得计数

时间:2019-01-25 08:26:00

标签: r input shiny radio-button dt

我正在基于mtcars数据构建S​​hinyApp。我在计数值和显示 cyl button 的选择时遇到问题。 在显示按钮中选择了全部时,<圆筒>过滤器会显示准确的计数并显示选择。如果在 disp 中选择了 all 以外的其他值,则 cyl将给出0计数,并且不会显示任何选择。 有人可以看看我的代码吗?我将非常感谢。

图书馆(发光)
    库(DT)
    图书馆(dplyr)     图书馆(shinythemes)     库(htmlwidgets)     库(shinyWidgets)     库(shinydashboard)

data_table<-mtcars    
data_table$hp<-NULL
data_table$qsec<-NULL
data_table$drat<-NULL

#ui
ui = fluidPage( 
  sidebarLayout(
    sidebarPanel (

      uiOutput("vs_selector"),
      uiOutput("carb_selector"),
      uiOutput("gear_selector"),
      uiOutput("cyl_selector"),
      valueBoxOutput("count_gear"),
      valueBoxOutput("count_cyl")),


    mainPanel(

      DT::dataTableOutput('mytable') 

    )

  ))




#server
server = function(input, output, session) {

  output$vs_selector <- renderUI({

    selectInput(inputId = "vs",
                label = "vs:", multiple = TRUE,
                choices = c( unique(data_table$vs)),
                selected = c(0))

  })

  output$carb_selector <- renderUI({

    req(input$vs)

    available0 <- data_table[c(data_table$vs %in% input$vs ), "carb"]  

    selectInput(
      inputId = "carb", 
      label = "carb:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available0))),
      selected = c('4'))

  })


  output$gear_selector <- renderUI({

    req(input$vs, input$carb)

    available <- data_table[["gear"]][data_table$vs %in% input$vs]
    if(! "All" %in% input$carb){
      available <- available[data_table$carb %in% input$carb]
    }

    selectInput(
      inputId = "gear", 
      label = "gear:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = c('4'))

  })



  output$cyl_selector <- renderUI({

    req(input$vs, input$carb, input$gear)

    available <- data_table[["cyl"]][data_table$vs %in% input$vs]




    selectInput(
      inputId = "cyl", 
      label = "cyl:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = c('6', '8', '4'))

  })





  output$count_gear <- renderValueBox({


    valueBox(
      value = length(unique(thedata()[["gear"]])), 


      subtitle = sprintf("Number of gear values" ))   

  })




  output$count_cyl <- renderValueBox({

    #req(input$vs, input$carb, input$gear)

    #available <- data_table[["cyl"]][data_table$vs %in% input$vs]





    valueBox(


      # if("All" %in% input$cyl) {
      #    value = "All"
      #  } else(value = length(unique(input$cyl))),

      value = length(unique(thedata()[["cyl"]])),

      subtitle = sprintf("Number of cyl values" ))   

  })







  thedata <- reactive({

    req(input$gear, input$vs, input$carb, input$cyl)

    data_table<-data_table[data_table$vs %in% input$vs,]

    if(! "All" %in% input$carb){
      data_table<-data_table[data_table$carb %in% input$carb,]
    }

    if(! "All" %in% input$gear){
      data_table<-data_table[data_table$gear %in% input$gear,]
    }


    if(! "All" %in% input$cyl){
      data_table<-data_table[data_table$cyl %in% input$cyl,]
    }


    data_table

  })


  output$mytable = DT::renderDataTable({

    DT::datatable( {     

      thedata()   # Call reactive thedata()

    })

  })

}  

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您可能需要找到优化代码的方法,因为它以复杂的方式完成了许多简单的事情。无论如何,这个问题已经解决了。

  • 更改了过滤条件和if条件
  • 使用input$cyl值更改了计数
library(shiny)   
library(DT)     
library(dplyr) 
library(shinythemes) 
library(htmlwidgets) 
library(shinyWidgets) 
library(shinydashboard)


data_table<-mtcars    

#ui
ui = fluidPage( 
  sidebarLayout(
    sidebarPanel (

      uiOutput("vs_selector"),
      uiOutput("carb_selector"),
      uiOutput("disp_selector"),
      uiOutput("cyl_selector"),
      valueBoxOutput("count_disp"),
      valueBoxOutput("count_cyl")),


    mainPanel(

      DT::dataTableOutput('mytable') 

    )

  ))




#server
server = function(input, output, session) {

  output$vs_selector <- renderUI({

    selectInput(inputId = "vs",
                label = "vs:", multiple = TRUE,
                choices = c( unique(data_table$vs)),
                selected = c(0,1))

  })

  output$carb_selector <- renderUI({

    req(input$vs)

    available0 <- data_table[c(data_table$vs %in% input$vs ), "carb"]  

    selectInput(
      inputId = "carb", 
      label = "carb:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available0))),
      selected = 'All')

  })


  output$disp_selector <- renderUI({

    req(input$vs, input$carb)

    available <- data_table[["disp"]][data_table$vs %in% input$vs]
    if(! "All" %in% input$carb){
      available <- available[data_table$carb %in% input$carb]
    }

    selectInput(
      inputId = "disp", 
      label = "disp:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = c(160,108, 258, 360))

  })



  output$cyl_selector <- renderUI({

     req(input$vs, input$carb, input$disp)

     available <- data_table[["cyl"]][data_table$vs %in% input$vs]




    selectInput(
      inputId = "cyl", 
      label = "cyl:",
      multiple = TRUE,
      choices = c('All',as.character(unique(available))),
      selected = 'All')

  })





  output$count_disp <- renderValueBox({


    valueBox(
      value = length(unique(thedata()[["disp"]])), 


      subtitle = sprintf("Number of disp values" ))   

  })




  output$count_cyl <- renderValueBox({

    #req(input$vs, input$carb, input$disp)

    #available <- data_table[["cyl"]][data_table$vs %in% input$vs]





    valueBox(


     # if("All" %in% input$cyl) {
    #    value = "All"
    #  } else(value = length(unique(input$cyl))),

     value = length(unique(thedata()[["cyl"]])),

      subtitle = sprintf("Number of cyl values" ))   

  })







  thedata <- reactive({

    req(input$disp, input$vs, input$carb, input$cyl)

    data_table<-data_table[data_table$vs %in% input$vs,]

    if(! "All" %in% input$carb){
      data_table<-data_table[data_table$carb %in% input$carb,]
    }

    if(! "All" %in% input$disp){
      data_table<-data_table[data_table$disp %in% input$disp,]
    }


    if(! "All" %in% input$cyl){
      data_table<-data_table[data_table$cyl %in% input$cyl,]
    }


    data_table

  })


  output$mytable = DT::renderDataTable({

    DT::datatable( {     

      thedata()   # Call reactive thedata()

    })

  })

}  

shinyApp(ui = ui, server = server)

更新:

通过选择列从输出数据中获取气缸数。