为什么此SelectizeIgeInput最小值功能在2位数字上效果不正常,即使它适用于单位数字数字?

发布于 2025-01-27 02:05:18 字数 2510 浏览 2 评论 0原文

这个让我困惑。以下代码提取selectizeInput()允许用户为数据分析的目的选择/从周期中选择/从selectizeIzeInput()中提供的选择(后者未显示)按照(a)右框中提供的“到”期限为&gt的规则;比左框中显示的“来自”时期,默认时期到“显示的周期” = 1比选定的“来自”,(b)(b)左框中显示的“最大”期间比1小。所有时期的最大值。

以下代码正常工作,直到您到达“从”期9中,此时,应用程序崩溃了以下消息:“ 警告min(all_choices_reaeactive()[all_choices_reaeactive()> input $ trastfrom]):否返回inf的无误论点。一旦您超过“从“时期9”,“唯一的”周期是2。

如何修复?

请注意,在完整的代码中,此可重复的代码是从中提取的,有40个时期,并且在处理值&GT时,这些功能会遭受相同的故障; 9(或2位数字)。我已经摆弄了使用strtoi()将字符串转换为整数的函数(per https://stat.ch/r-manual/r-manual/r-devel/library/library/base/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)

“在此处输入图像描述”

This one has me puzzled. The below code extract uses selectizeInput() to allow the user to choose to/from periods for purposes of data analysis (latter not shown), with the choices offered in the selectizeInput() boxes following the rules that (a) the "To" periods on offer in the right box are > than the "From" Periods shown in the left box, with the default "To" period shown = 1 greater than the selected "From", and (b) the maximum "From" period shown in the left box is 1 less than the max of all periods.

The below code works fine until you get to "From" period 9, at which point the App crashes with the following message: "Warning in min(all_choices_reactive()[all_choices_reactive() > input$transFrom]) : no non-missing arguments to min; returning Inf". And once you exceed "From" period 9, the only "To" period shown is 2.

How can this be fixed?

Please note that in the full code this reproducible code is extracted from, there are unique 40 periods and these functions suffer from the same glitch when dealing with values > 9 (or 2 digit numbers). I've fiddled with using strtoi() function for converting strings to integers (per https://stat.ethz.ch/R-manual/R-devel/library/base/html/strtoi.html), as.numeric() here and there, with no luck yet.

Image at the bottom shows the problem.

Reproducible code:

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)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2025-02-03 02:05:18

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

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)

Code below works by converting character strings to integers using strtoi(...) function per stefan comment as shown below:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文