如何在运行 App 时淘汰"Warning: Error in seq.default: 'from' must be a finite number"?

How to eliminate "Warning: Error in seq.default: 'from' must be a finite number" when running App?

我是 运行 应用程序中以下简化代码的一个版本,它允许用户从单个时间点开始对大型数据框中的元素进行分层。用户可以选择使用哪个时间度量:Period_1Period_2,以及在这 2 个时间类别中的每个时间段 select。完整的应用程序为用户提供了更多的分层变量,在下面的 renderTable 部分中,仍然保留了这种更广泛功能的痕迹。

该示例产生了预期的结果,但它在 R 控制台中生成:“警告:seq.default 中的错误:'from' 必须是有限数”。 (我在下面的例子和这个例子派生的完整应用程序中都收到了同样的警告)。

其他警告(“警告在分钟内...,na.rm = TRUE):没有非缺失参数到分钟;返回 Inf”)正在弹出在我添加下面的 custom_mincustom_max 函数之前。

我不想禁止显示警告,我希望代码能够正常工作。任何线索如何解决?

library(shiny)
library(tidyverse)
library(shinyWidgets)

custom_min <- function(x) {if (length(x)>0) min(x, na.rm=TRUE) else Inf}
custom_max <- function(x) {if (length(x)>0) max(x, na.rm=TRUE) else Inf}

ui <-
  fluidPage(
    uiOutput("period"),
    uiOutput("strat_time"),
    tableOutput("stratData"))

server <- function(input, output, session) {
  
  stratData <- reactive({
    data.frame(
      ID = c(1,1,2,2,2,2,3,3,3),
      Period_1 = c("2020-03", "2020-04", "2020-01", "2020-02", "2020-03", "2020-04", "2020-02", "2020-03", "2020-04"),
      Period_2 = c(1, 2, 1, 2, 3, 4, 1, 2, 3),
      Values = c(-5, 25, 35, 45, 55, 87, 10, 20, 30)
    )
  })
  
  output$period <- renderUI({
    radioButtons(
      inputId = 'period',
      label = NULL,
      choiceNames = c('By Period_1','By Period_2'), 
      choiceValues = c('Period_1','Period_2'),
      selected = 'Period_1',
      inline = TRUE
    )
  })
  
  output$strat_time <- renderUI({
    req(input$period)
    chc <- unique(na.omit(stratData()[[input$period]]))
    selectInput(inputId = "strat_time", 
                label = "Specify stratification point-in-time:",
                choices = chc,
                selected = chc[1])
  })
  
  output$stratData <- renderTable({
    qs <- ifelse(is.character(stratData()[[input$period]]), "'", "")
    filter_exp1 <- parse(text=paste0(input$period,  "==", qs,input$strat_time, qs))
    stratData_1 <- reactive({stratData() %>% filter(eval(filter_exp1))}) 
    breaks <- seq(custom_min(stratData_1()[[4]]), 
                  custom_max(stratData_1()[[4]]), 
                  by=10)
    if(max(breaks) < max(stratData_1()[[4]], na.rm=TRUE)){breaks <- c(breaks, max(breaks) + 10)}
    
    tmp <- stratData() %>% 
      filter(eval(filter_exp1)) %>%
      mutate(sumvar = cut(!!sym("Values"), breaks=breaks, include.lowest=TRUE, right = TRUE, dig.lab = 5)) %>% 
      group_by(sumvar) %>% 
      summarise(Count = n(),Values = sum(!!sym("Values"))) %>% 
      complete(sumvar, fill = list(Count = 0,Values = 0)) %>% 
      ungroup %>% 
      mutate(Count_pct = Count/sum(Count)*100, Values_pct = Values/sum(Values)*100) %>% 
      dplyr::select(everything(), Count, Count_pct, Values, Values_pct)
    tmp
  })
  
}

shinyApp(ui, server)

发生这种情况是因为您的函数 custom_min(stratData_1()[[4]]) 可以 return non-finite 个数字,即 Inf,当 length(x) > 0.

您可以将代码更改为:

    min <- custom_min(stratData_1()[[4]])
    max <- custom_max(stratData_1()[[4]])
    breaks <- if(any(is.infinite(c(min,max)))) c(0, 10) else seq(min, max, by = 10)