Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a blank input for a date in Shiny

Tags:

date

r

shiny

I have a series of inputs in my R Shiny app that I am using as arguments to a function to select specific data from a data frame. On of the inputs is dateInput:

dateInput("dateSelect", "Date", format = "yyyy-mm-dd", value = NA)

In my function, I need to specify if dateSelect is blank or not selected, to be able select All dates. See an example that is working correctly, that isn't a date, and a simple selectInput:

selectInput("teamSelect", "Team", choices = c("All", levels(newEffortstable$team)))

In the function, this works to select 'All teams':

    if(!missing(teamSelect)){
    if(teamSelect!="All"){
    selections[["teamEfforts"]] = 
    newEffortstable$effortNo[which(newEffortstable$team %in% teamSelect)]
    }else{
    selections[["teamEfforts"]] = newEffortstable$effortNo
    }
    }

I have tried the following with NA and NULL and " " and keep getting 'Error in if: argument is of length zero'

    if(!missing(dateSelect)){
    if(!dateSelect== "NA"){
    selections[["dateEfforts"]] = 
    newEffortstable$effortNo[which(newEffortstable$date == dateSelect)]
    }else{
    selections[["dateEfforts"]] = newEffortstable$effortNo
    }
    }

Thanks!

like image 546
Emily Avatar asked Jul 12 '17 14:07

Emily


1 Answers

There's one option you didn't try out yet: checking the length. When no date is given, dateInput returns an empty Date vector as illustrated in the example below.

So you could check if(length(input$dateSelect) == 0), but this is not the most solid shiny option. In order to avoid that you have to check all possibilities (i.e. NULL, NA, "", numeric(0) etc), you can use the function isTruthy() as in the example below:

shinyApp(
  ui = fluidPage(
    dateInput("dateSelect","Date"),
    verbatimTextOutput("out"),
    textOutput("text")
  ),
  server = function(input,output,session){

    output$text <- renderText({
      if(!isTruthy(input$dateSelect)){
        "NO DATE"
      } else {
        paste("The chosen date is:",input$dateSelect)
      }
    })
    output$out<- renderPrint({str(input$dateSelect)})
  }
)
like image 95
Joris Meys Avatar answered Oct 11 '22 13:10

Joris Meys