【问题标题】:Shiny selectinput filtering闪亮的选择输入过滤
【发布时间】:2018-01-05 06:50:34
【问题描述】:

我有闪亮的代码:

# ui.R
fluidPage(
  selectInput("ci_risk_1000M", "<=", 
              choices = c("a" = 1, "b" = 2, "c" = 3, "d" = 4, "e" = 5, "f" = 6, 
                          "g" = 7, "h" = 8, "i" = 9, "j" = 10, "k" = 11), 
              selected = 11), 
  tableOutput("ats")

)
# server.R
x <- 1:11
    function(input, output, session) {
     output$ats <- renderTable({x[x <= input$ci_risk_1000M]}) 
    }

我想通过选定的输入过滤 x 值。但是,基本答案应该是行从 1 到 11 的表格,但它只显示值 1、10、11。你知道为什么以及如何解决这个问题吗? sliderInput 不是选项,除非它可以显示为字符值,而不是整数。

【问题讨论】:

    标签: r filter


    【解决方案1】:

    问题出在input$ci_risk_1000M 的输出中,即character 类。

    如果我们检查它在字符向量上的行为

    x <= "11"
    #[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
    

    即只有第一个、第十个和第十一个为TRUE,所以子集返回

    x[x <= "11"]
    #[1]  1 10 11
    

    另外,检查character 向量与sorting 的行为方式

    sort(as.character(x))
    #[1] "1"  "10" "11" "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9" 
    

    我们需要将其转换为numeric/integer

    output$ats <- renderTable({x[x <= as.integer(input$ci_risk_1000M)]}) 
    

    -完整代码

    library(shiny)
    ui <- fluidPage(
      selectInput("ci_risk_1000M", "<=", 
                  choices = c("a" = 1, "b" = 2, "c" = 3, "d" = 4, "e" = 5, "f" = 6, 
                              "g" = 7, "h" = 8, "i" = 9, "j" = 10, "k" = 11), 
                  selected = 11), 
      tableOutput("ats")
    
    )
    # server.R
    x <- 1:11
    server <- function(input, output, session) {
      output$ats <- renderTable({x[x <= as.integer(input$ci_risk_1000M)]}) 
    }
    shinyApp(ui, server)
    

    -输出

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2016-03-16
      • 2020-08-01
      相关资源
      最近更新 更多