Home > Software design >  Ifelse fails in render function
Ifelse fails in render function

Time:12-11

The following code works. The functional line of code is p1 p2 p3 p4 show. However, if I comment that line out and uncomment the commented lines, the app still works, but the associated map doesn't render. The error is argument is of length zero.

Why does this happen, and how do I fix it?

Thanks as always!

UI

    tabPanel("US Map",
             fluidRow(
               sliderInput("daterange","Date Range",
                           min = lubridate::as_date("2022-05-01"),
                           max = lubridate::as_date("2022-12-31"),
                           value = c(lubridate::as_date("2022-05-01"),
                                     lubridate::as_date("2022-12-31"))),
               checkboxInput("tweets","Show Tweets",value = TRUE),
               checkboxInput("actions","Show Actions",value = TRUE),
               hr(),
               tmapOutput("geo2")
             ))
#> Error in tabPanel("US Map", fluidRow(sliderInput("daterange", "Date Range", : could not find function "tabPanel"

Created on 2022-12-10 with reprex v2.0.2

Server

  output$geo2 <- renderTmap({
    
    tweets_sf %<>% filter(date>=input$daterange[1],date<=input$daterange[2])
    
    actions_sf <- actions %>%
      # filter(incident_date >= input$daterange[1],incident_date <= input$daterange[2]) %>% 
      st_as_sf(coords = c("lon","lat"),crs=4326) %>% 
      st_cast("POINT")
    
    tmap_mode("view")
    ## tmap mode set to plotting
    
    p1<-tm_shape(usa)  
      tm_fill("#acc480",alpha = 0.4,id = "NAME")
    p2<-tm_shape(usa)  
      tm_borders("black", lwd = .5) 
    p3<-tm_shape(actions_sf)  
      tm_text("target_type", size = 1,scale=1)  
      tm_symbols(col = "brown1",shape = "target_type",shapes = rep(5,6), 
                 size = 1,alpha=0.4, scale = .25)  
      tm_legend(show = TRUE)
    p4<-tm_shape(tweets_sf)  
      tm_text("ngram", size = 1,scale=.7)  
      tm_symbols(col = "deepskyblue1", id = "date", 
                 size = 1,alpha=0.4, scale = 0.15,palette = "RdBu")  
      tm_legend(show = TRUE)
    show<-tm_view(set.view = c(-98.5795,39.8283,4))
    
    # if(input$tweets==TRUE & input$action==TRUE){
    #   p1 p2 p3 p4 show
    # }else if(input$tweets==TRUE & input$actions==FALSE){
    #   p1 p2 p4 show
    # }else if(input$tweets==FALSE & input$actions==TRUE){
    #   p1 p2 p3 show
    # }else{p1 p2 show}
    
    p1 p2 p3 p4 show
    
  })
#> Error in renderTmap({: could not find function "renderTmap"

Created on 2022-12-10 with reprex v2.0.2

CodePudding user response:

argument is of length zero is because the left parameter of the condition is empty.

Use DescTools::Coalesce() (or other similar function)

if(DescTools::Coalesce(input$tweets),TRUE)==TRUE
 &  DescTools::Coalesce(input$actions,TRUE)==FALSE)

etc

https://www.rdocumentation.org/packages/DescTools/versions/0.99.47/topics/Coalesce

  • Related