简体   繁体   English

"运行App时如何消除“警告:seq.default中的错误:'from'必须是有限数”?"

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

I'm running a version of the below reduced code in an App that allows the user to stratify the elements in a large data frame as of a single point-in-time.我正在一个应用程序中运行以下简化代码的一个版本,它允许用户在单个时间点对大型数据框中的元素进行分层。 The user can choose which measure to use for time: Period_1<\/code> or Period_2<\/code> , and which time period to select within each of those 2 time categories.用户可以选择用于时间的度量: Period_1<\/code>或Period_2<\/code> ,以及在这 2 个时间类别中选择哪个时间段。 The full App offers the user several more stratification variables, and in the renderTable<\/code> section below there remain vestiges of this broader functionality.完整的 App 为用户提供了更多的分层变量,并且在下面的renderTable<\/code>部分中仍然保留了这种更广泛功能的痕迹。

The example produces the intended results, but it generates in the R console: " Warning: Error in seq.default: 'from' must be a finite number<\/em> ".该示例产生了预期的结果,但它在 R 控制台中生成:“警告:seq.default 中的错误:'from' 必须是有限数<\/em>”。 (I get this same warning in both the below example and the full App this example derives from). (我在下面的示例和此示例派生的完整应用程序中都收到了相同的警告)。

Other warnings (" Warning in min..., na.rm = TRUE) : no non-missing arguments to min; returning Inf<\/em> ") were popping up before I added the custom_min<\/code> and custom_max<\/code> functions below.在我添加下面的custom_min<\/code>和custom_max<\/code>函数之前,会弹出其他警告(“ Mining 中的警告...,na.rm = TRUE):min 没有非缺失参数;返回 Inf<\/em> ”)。

I don't want to suppress the warning, I'd rather have the code work correctly.我不想抑制警告,我宁愿让代码正常工作。 Any clues how to address?任何线索如何解决?

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)

This happen because your function custom_min(stratData_1()[[4]])<\/code> can return non-finite numbers, namely Inf<\/code> , when length(x) > 0<\/code> .发生这种情况是因为您的函数custom_min(stratData_1()[[4]])<\/code>可以在length(x) > 0<\/code>时返回非有限数字,即Inf<\/code> 。

You can change your code to something like :您可以将代码更改为:

    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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gganimate 错误:seq.default 中的错误(范围 [1],范围 [2],长度.out = nframes):'from' 必须是有限数 - gganimate error: Error in seq.default(range[1], range[2], length.out = nframes) : 'from' must be a finite number ggplot2 错误消息:seq.default(range[1], range[2], length.out = nframes) 中的错误:&#39;from&#39; 必须是一个有限数 - ggplot2 error message: Error in seq.default(range[1], range[2], length.out = nframes) : 'from' must be a finite number ggplot错误:seq.default中的错误(范围[1],范围[2],长度.out = self$detail):'from'必须是有限数 - ggplot error: Error in seq.default(range[1], range[2], length.out = self$detail) : 'from' must be a finite number R 错误'seq.default 中的错误(min(x,na.rm = T),max(x,na.rm = T),长度 = 长度(ColRamp)):'来自'必须是有限数' - R Error 'Error in seq.default(min(x, na.rm = T), max(x, na.rm = T), length = length(ColRamp)) : 'from' must be a finite number' glmnet 4.0 负二项式回归:“错误 seq.default(log(lambda_max), log(lambda_max * lambda.min.ratio), : 'from' 必须是有限数” - glmnet 4.0 negative binomial regression: “Error in seq.default(log(lambda_max), log(lambda_max * lambda.min.ratio), : 'from' must be a finite number” Shiny 错误:警告:seq.int 中的错误:'to' 必须是有限数 - Shiny Error : Warning: Error in seq.int: 'to' must be a finite number seq.default(a,length = max(0,b-a-1))中的错误:长度必须为非负数 - Error in seq.default(a, length = max(0, b - a - 1)) : length must be non-negative number R-编程-seq.default(a,length = max(0,b-a-1))中的错误:长度必须为非负数 - R- programming - Error in seq.default(a, length = max(0, b - a - 1)) : length must be non-negative number seq.default中的aggregate()错误 - aggregate() error in seq.default 如何解决ggplot中的“seq.int(0,to0 - from,by)中的错误:'to'必须是有限数”? - How to solve "Error in seq.int(0, to0 - from, by) : 'to' must be a finite number" in ggplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM