I am facing a really weird error in R Shiny. I have a dropdown filter which shows the type of date - Due Date, Payment Date and GL Date. Based on the type of date from the drop down - I have date range selectors respectively for each of these mentioned above.
UI code is as below :
output$select_DateType <- renderUI({
selectInput("date_type","Select Date Type", choices=c("Due Date","GL Date","Payment Date"),selected = "Due Date")
})
output$daterange_due <- renderUI({
x<-reactive({req(input$date_type)})
y <- x()
if(y == "Due Date"){
dateRangeInput(inputId = "duedate",label="Due date range",start = min(mydata ()$duedate),end=max(mydata()$duedate),separator="-")
} else if(y == "GL Date"){
dateRangeInput(inputId = "gldate",label="GL date range",start = min(mydata()$gldate ),end=max(mydata()$gldate),separator="-")
} else {
dateRangeInput(inputId = "paydate",label="Payment date range",start = min(mydata ()$paydate),end=max(mydata()$paydate),separator="-") }
})
These are used to calculate a lot of other values wherein the date ranges are used to filter the dataset.
Server code
output$dues <- renderValueBox({
x<-reactive({req(input$date_type)}) y<-x()
if(y == "Due Date"){ mydata <- mydata()%>%filter(duedate >= input$duedate[1] & duedate <= input$duedate[2])
valueBox(subtitle = "Total Spend",prettyNum(round(sum(as.numeric(mydata$invamt.base/1000000),na.rm = TRUE),2),big.mark=","),color = "navy", width = 4) }else if(y == "GL Date"){ mydata <- mydata()%>%filter(gldate >= input$gldate[1] & duedate <= input$gldate[2])
valueBox(subtitle = "Total Spend",prettyNum(round(sum(as.numeric(mydata$invamt.base/1000000),na.rm = TRUE),2),big.mark=","),color = "navy", width = 4)
}else {
mydata <- mydata()%>%filter(paydate >= input$paydate[1] & duedate <= input$paydate[2])
mydata$paydate <- as.numeric(mydata$paydate)
mydata$paydate[is.na(mydata$paydate)] <- 0
valueBox(subtitle = "Total Spend",prettyNum(round(sum(as.numeric(mydata$invamt.base/1000000),na.rm = TRUE),2),big.mark=","),color = "navy", width = 4) }
})
There are about 20 such calculations: What happens is that , when I launch the app it gives me an error in some or all of the value boxes as: Error in if argument of length is zero.
Also, if I run the app like 3-4 times, the app runs fine without any error.
Please help me with. I am stuck badly.
Sample Dataset :
UID invamt.base duedate gldate 1 200 12-02-19 05-01-19 2 300 15-03-19 01-02-19 3 200 12-02-19 02-02-19 4 300 12-02-19 19-02-19 5 400 15-03-19 12-02-19 6 200 17-04-19 16-02-19 7 100 17-04-19 19-02-19
This is what the dataset looks like: