为什么这个 selectizeInput 最小值函数对于 2 位数字不能正常工作,即使它对于一位数字工作正常?

Why does this selectizeInput minimum values function not work correctly for 2 digit numbers even though it works fine for single digit numbers?

这个让我很困惑。下面的代码摘录使用 selectizeInput() 允许用户选择 to/from 个时间段用于数据分析(后者未显示),selectizeInput() 框中提供的选项遵循以下规则( a) 右侧框中提供的“至”期间大于左侧框中显示的“起始”期间,显示的默认“至”期间 = 1 大于所选的“起始”,以及 (b)左侧框中显示的最大“起始”期间比所有期间的最大值小 1。

下面的代码工作正常,直到您到达“从”第 9 期,此时应用程序崩溃并显示以下消息:“Warning in min(all_choices_reactive()[all_choices_reactive() > input$transFrom]) : no non-missing arguments to min; returning Inf”。一旦您超过“从”期间 9,唯一显示的“到”期间就是 2。

如何解决这个问题?

请注意,在从中提取此可重现代码的完整代码中,有唯一的 40 个句点,并且这些函数在处理大于 9(或 2 位数字)的值时会遇到相同的故障。我一直在摆弄使用 strtoi() 函数将字符串转换为整数(每个 https://stat.ethz.ch/R-manual/R-devel/library/base/html/strtoi.html),as.numeric() 在这里和那里,还没有运气。

底部的图像显示了问题。

可重现代码:

library(shiny)

transitDF <- data.frame(Period = c(1,2,3,4,5,6,7,8,9,10,11,12))  

all_choices_p1 <- unique(transitDF$Period)

ui <- fluidPage(
  br(),
  fluidRow(
    column(3,h5(strong("Select from/to periods:"))),
    column(2,selectizeInput(
               inputId = "transFrom",
               label = NULL,
               choices = all_choices_p1[-length(all_choices_p1)],
               selected = 1
             )),
    column(2,selectizeInput(
               inputId = "transTo",
               label = NULL,
               choices = all_choices_p1[-1],
               selected = 2
              ))
  ),
  fluidRow(
    column(3,h5(strong("Output to period:"))),
    column(2,textOutput("toResults"),style = "padding-top: 9px;")
    )
)

server <- function(input, output, session) {

  all_choices_reactive <- reactiveVal(all_choices_p1)
  
  observeEvent(input$transFrom, {
    freezeReactiveValue(input,"transTo")
    updateSelectizeInput(
      session,
      inputId = "transTo",
      choices = all_choices_reactive()[all_choices_reactive() > input$transFrom],
      selected = min(all_choices_reactive()[all_choices_reactive() > input$transFrom])
      )
    }, ignoreInit = TRUE)
  
  toResults <- reactive({
      req(input$transTo)
      toResults <- min(all_choices_reactive()[all_choices_reactive() > input$transFrom])
  })
  
  output$toResults <- renderText({toResults()})
  
}

shinyApp(ui, server)

下面的代码通过使用每个 stefan 评论的 strtoi(...) 函数将字符串转换为整数来工作,如下所示:

library(shiny)

transitDF <- data.frame(Period = as.numeric(c(1,2,3,4,5,6,7,8,9,10,11,12)))  

all_choices_p1 <- (unique(transitDF$Period))

ui <- fluidPage(
  br(),
  fluidRow(
    column(3,h5(strong("Select from/to periods:"))),
    column(2,selectizeInput(
               inputId = "transFrom",
               label = NULL,
               choices = all_choices_p1[-length(all_choices_p1)],
               selected = 1
             )),
    column(2,selectizeInput(
               inputId = "transTo",
               label = NULL,
               choices = all_choices_p1[-1],
               selected = 2
              ))
  ),
  fluidRow(
    column(3,h5(strong("Output to period:"))),
    column(2,textOutput("toResults"),style = "padding-top: 9px;")
    )
)

server <- function(input, output, session) {

  all_choices_reactive <- reactiveVal(all_choices_p1)
  
  observeEvent(input$transFrom, {
    freezeReactiveValue(input,"transTo")
    updateSelectizeInput(
      session,
      inputId = "transTo",
      choices = all_choices_reactive()[all_choices_reactive() > strtoi(input$transFrom)],
      selected = min(all_choices_reactive()[all_choices_reactive() > strtoi(input$transFrom)])
      )
    }, ignoreInit = TRUE)
  
  toResults <- reactive({
      req(input$transTo)
      toResults <- as.numeric(min(all_choices_reactive()[all_choices_reactive() > strtoi(input$transFrom)]))
  })
  
  output$toResults <- renderText({toResults()})
  
}

shinyApp(ui, server)