【问题标题】:Not able to see the choices of selectInput and get its count in shinyApp无法看到 selectInput 的选择并在 shinyApp 中获得计数
【发布时间】:2019-06-19 01:34:08
【问题描述】:

我正在 mtcars 数据上构建一个 shinyApp。我在计算值和显示 cyl 按钮 选项时遇到问题。 当在显示按钮中选择all时,cyl filter会显示准确的计数并显示选择。如果在 disp 中选择了 all 以外的值,则 cyl 将给出 0 计数并且不会显示任何选择。 有人可以看看我的代码。我将不胜感激。

图书馆(闪亮)
图书馆(DT)
图书馆(dplyr) 图书馆(闪亮的主题) 图书馆(htmlwidgets) 库(闪亮的小部件) 库(闪亮的仪表板)

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)

【问题讨论】:

    标签: r input shiny radio-button dt


    【解决方案1】:

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

    • 更改了过滤条件和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)
    
    

    更新:

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

    【讨论】:

    • 谢谢老兄,但还是有问题出现。您可以看到 All 写入了 cyl 值的数量,而不是像 disp 值的数量所示的数字。你能再看看吗
    • @Doctor 那就是说,你想要 3 个?
    • 当我从 vs、carb、disp 中选择值时,我想要 cyl 中存在的唯一值的计数。与上面过滤的输入相同,显示计数相同
    • 您要计算唯一 cyl 值的计数还是行数?
    • cyl 值匹配计数
    猜你喜欢
    • 2019-05-01
    • 2019-06-08
    • 2019-06-18
    • 1970-01-01
    • 2016-01-10
    • 2019-06-14
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多