Search code examples
rshinydplyrleafletr-leaflet

Combining conditional evaluation within dplyr pipe operators (%>%)


So I found a quite similar question, here on SO, but I am unable to get it fixed for my problem. I am building a map in Shiny leaflet. What I want, is when a certain variable has certain values (conditions), make a addAwesomeMarkers(); else, make a addCircleMarkers(). I've tried some if (else), case_when() and mutate() statement, but am unable to fix it. So... here's my code.

Packages:

library(dplyr)
library(ggplot2)
library(leaflet)
library(reshape2)
library(shiny)
library(tidyr)

Dummy dataset:

NAME    VAR WAIT    latitude    longitude
a   4   1   52,6263 4,7312
b       3   52,2946 4,9585
c   6   8   52,3331 6,6468
d   8   5   51,2864 4,0492
e   7   6   50,9832 5,8446

Code:

leafletOutput('myMap', width = '80%', height = 600)

output$myMap <- renderLeaflet({    
getColor <- function(DATASET) {
        sapply(DATASET$WAIT, function(WAIT) {
        if(WAIT == 0 | is.na(WAIT) | is.nan(WAIT)) {"gray"}
        else if(WAIT <= 1){"darkgreen"}
        else if(WAIT <= 2){"green"}    
        else if(WAIT <= 4){"lightgreen"}
        else if(WAIT <= 6){"orange"}
        else if(WAIT <= 8){"red"}
        else {"darkred"}
        })
      }

    icons <- awesomeIcons(
      icon = 'heart-o',
      lib = 'fa',
      iconColor = "#FFFFFF",
      markerColor = getColor(DATASET))

     map <- leaflet(DATASET) %>%          
                addTiles() %>% 
# DATASET$VAR is a char in my dataset
                     {if (DATASET$VAR == "4") filter(., addAwesomeMarkers(lng = ~longitude, lat = ~latitude, icon = icons,
                              label = ~as.character(DATASET$NAME),
                              popup = paste0("<strong>Name: </strong>", DATASET$NAME)))
                       else filter(., addCircleMarkers(lng = ~longitude, lat = ~latitude, radius = 10, label = ~as.character(DATASET$NAME),
                             popup = paste0("<strong>Name: </strong>", DATASET$NAME)))} %>% 
    addProviderTiles(providers$OpenStreetMap)
        })

So my if else ain't working; giving the following error:

no applicable method for 'filter_' applied to an object of class "c('leaflet', 'htmlwidget')"

I tried implementing a mutate(). Thanks in advance for any help!


Solution

  • I've managed it (see result all the way at the bottom) by first defining two columns in my data prep script, stating whether to plot a square marker or a default marker ('IND_VAR') and whether to display a star inside the corresponding marker ('VAR_SHOWING_STAR'):

     Dataset <- Dataset %>% 
      dplyr::group_by(NAME, VAR) %>%
        dplyr::mutate(IND_VAR = ifelse(VAR == '4', 1, 0)) %>%
      dplyr::ungroup() %>%
      dplyr::mutate(NICE_ICON = ifelse(VAR_SHOWING_STAR == "NOT", "", "Star"))
    

    Second, I've defined colors in my app script:

     Dataset <- Dataset %>%
      mutate(COLOR_WAIT = case_when(
        (is.na(WAIT) | is.nan(WAIT)) ~"gray",
        (WAIT >= 0 & WAIT <= 1) ~ "darkgreen",
        (WAIT == 2) ~ "green",
        (WAIT >= 3 & WAIT <= 4) ~ "lightgreen",
        (WAIT >= 5 & WAIT <= 6) ~ "orange",
        (WAIT >= 7 & WAIT <= 8) ~ "lightred",
        (WAIT >= 9 & WAIT <= 10) ~ "red",
        TRUE ~ "darkred"))
    

    Third, I've defined icons (also in my app script), including an ifelse() regarding 'IND_VAR':

      icons <- makeAwesomeIcon(icon = Dataset$NICE_ICON, lib  = 'fa',
                               squareMarker = ifelse(Dataset$IND_VAR == 1, TRUE, FALSE),
                               iconColor = "#FFFFFF", spin = TRUE,
                               markerColor = Dataset$COLOR_WAIT)
    

    And lastly, I've implemented addAwesomeMarkers() in renderLeaflet({}):

        %>% 
    addAwesomeMarkers(lng = ~longitude, lat = ~latitude, icon = icons,
                          label = ~as.character(Dataset$SOME_LABEL),
                          popup = paste0("<strong>Pop_up: </strong>", Dataset$SOME_POPUP) %>%
    

    Result:

    enter image description here