0
votes

I keep getting the following Inf/-Inf errors when I attempt to subset a data frame in Shiny based on the indices of the dates the user selects in a sliderInput control.

Warning in min(inputData$rawdata$TmStamp) :
  no non-missing arguments to min; returning Inf
Warning in max(inputData$rawdata$TmStamp) :
  no non-missing arguments to max; returning -Inf
Warning in min(inputData$rawdata$TmStamp) :
  no non-missing arguments to min; returning Inf
Warning in max(inputData$rawdata$TmStamp) :
  no non-missing arguments to max; returning -Inf
Warning: Error in seq.int: 'to' must be a finite number

My code is below. I am rendering the control in the server using renderUI so it can be reactive to the sliderInput control. I think the problem is that, to start with, the renderDataTable function doesn't know the indices should be the 1 and the # rows of the data frame (input$rawdata). However, I do not know how to fix this. How do I get this to work properly? I don't understand why raw_begin_ind and raw_end_ind aren't being set right away (if that's the problem). Thank you.

    output$rawDateRange <- renderUI({
            tagList(
                    tags$style(type = 'text/css', '#raw_slider .irs-grid-text {color: black; font-size: 12px; transform: rotate(-90deg) translate(-20px)}'), 
                    div(id = 'raw_slider',
                        sliderInput(inputId='rawDateRange',
                                    label='Select Time Period to Show:',
                                    min = as.Date(min(inputData$rawdata$TmStamp)),
                                    max = as.Date(max(inputData$rawdata$TmStamp)),
                                    value = c(as.Date(min(inputData$rawdata$TmStamp)),as.Date(max(inputData$rawdata$TmStamp)))
                        )
                    )
            )
    })
    
    observeEvent(input$rawDateRange,
            {
                    raw_begin_ind <- min(which(substr(inputData$rawdata$TmStamp,1,10) == input$rawDateRange[1]))
                    raw_end_ind <- max(which(substr(inputData$rawdata$TmStamp,1,10) == input$rawDateRange[2]))
                    
                    output$rawdata <- renderDataTable({
                            inputData$rawdata[raw_begin_ind:raw_end_ind,]
                    })
            }
    )
1
you could use cat to display values in console in order to debug your code. For example just before which(min(...)) insert cat(substr(inputData$rawdata$TmStamp,1,10),'\n') to make sure the condition works as you think.Waldi
"Reactivity" can seem unpredictable. With some (often larger but not always) shiny apps, a reactive block can fire before the input element it's dependent on is instantiated, so input$... may be NULL or empty, defeating the general assumption of what to expect out of inputs. This often cascades, resulting in a 0-length (0-row) reactive-data cascading to follow-on elements. This is one reason where req(...) is very useful: it stops any further work in a reactive block (and subsequent dependent blocks) when its input is unstable.r2evans
Try adding req(inputData$rawdata) to the beginning (first expression) of these two reactive blocks and see if it ameliorates the problem.r2evans
BTW: min(which(...)) is a slower version of which(...)[1]; similarly max(which(...)) is the slower version of tail(which(...),1). Granted, the difference in time is miniscule. But the results are not equivalent: min(which(...)) returns Inf when none of the ... condition is true; similarly max(which(...)) returns -Inf with nothing found in .... Perhaps that will be a problem, too? (I know it's not the warning you're showing in the question.)r2evans

1 Answers

0
votes

I thought I would answer my own question, so here is the code I used to fix the problem. One part of the issue was that the date ("LDT" in code below) column was not in POSIX format. Another odd issue is that without the "min" or "max" in as.Date(min(input$Date....)), I still got the Inf/-Inf error. I'm telling it which row to get the date from, so this is very strange behavior. Anyway, thanks for all the help!

    # Date Range Input controls for raw and qc data
    output$rawDateRange <- renderUI({
            req(inputData$rawdata)
            tagList(
                    tags$style(type = 'text/css', '#raw_DateSlider .irs-grid-text {color: black; font-size: 11px; transform: rotate(-75deg) translate(-20px)}'), 
                    div(id = 'raw_DateSlider',
                        sliderInput("rawDateRange", label = "Choose Date Range to View",
                                    min = as.Date(min(inputData$rawdata$LDT[1])),
                                    max = as.Date(max(inputData$rawdata$LDT[nrow(inputData$rawdata)])),
                                    value = c(as.Date(min(inputData$rawdata$LDT[1])),as.Date(max(inputData$rawdata$LDT[nrow(inputData$rawdata)])))
                        )
                    )
            )
    })
    
    observeEvent(input$rawDateRange, {
            output$rawdata <- renderDataTable({
                    inputData$rawdata %>% filter(LDT >= input$rawDateRange[1] & LDT <= input$rawDateRange[2])
            })
    })
    
    output$qcDateRange <- renderUI({
            req(inputData$qcdata)
            tagList(
                    tags$style(type = 'text/css', '#qc_DateSlider .irs-grid-text {color: black; font-size: 11px; transform: rotate(-75deg) translate(-20px)}'), 
                    div(id = 'qc_DateSlider',
                        sliderInput("qcDateRange", label = "Choose Date Range to View",
                                    min = as.Date(min(inputData$qcdata$LDT[1])),
                                    max = as.Date(max(inputData$qcdata$LDT[nrow(inputData$qcdata)])),
                                    value = c(as.Date(min(inputData$qcdata$LDT[1])),as.Date(max(inputData$qcdata$LDT[nrow(inputData$qcdata)])))
                        )
                    )
            )
    })
    
    observeEvent(input$qcDateRange, {
            output$qcdata <- renderDataTable({
                    inputData$qcdata %>% filter(LDT >= input$qcDateRange[1] & LDT <= input$qcDateRange[2])
            })
    })