Home > Enterprise >  How to eliminate "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"

Time:02-07

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 or Period_2, and which time period to select within each of those 2 time categories. The full App offers the user several more stratification variables, and in the renderTable section below there remain vestiges of this broader functionality.

The example produces the intended results, but it generates in the R console: "Warning: Error in seq.default: 'from' must be a finite number". (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") were popping up before I added the custom_min and custom_max functions below.

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)

CodePudding user response:

This happen because your function custom_min(stratData_1()[[4]]) can return non-finite numbers, namely Inf, when length(x) > 0.

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)
  •  Tags:  
  • Related