Peter Nsanze
Peter Nsanze

Reputation: 69

radius of circles in leaflet app to be driven by the selectInput

I am trying to make the radius of my circle markers in this Shiny leaflet app be driven by the selectInput variable.

The dropdown has three values, "Week.1", "Week.2" and "Week.3", which are all numeric vectors (of large dollar revenue values in millions) in the d at the bottom of this question.

selectInput("weekView", "Week's Revenue:",
          c("1" = "Week.1",
            "2" = "Week.2",
            "3" = "Week.3")),

I am currently loading Week.1 and get thr result. I wNt the input to change the weeks and react with the corresponding $ on the map. When I try and use input$weekView instead of the static ~(Week.1), I get errors as it is "Week.1". I've tried using quote = False in place to remove them before the radius is calculated but I'm failing to find the right place to do so...

leaflet(data = P) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircleMarkers(~Long, ~Lat, popup = ~Week.1,
                   radius = ~(Week.1)/40000,
                   stroke = FALSE, 
                   fillOpacity = 0.5)

Can any one advise on how best to go about this? What I want to say is radius = input$weekView/40000

All the script:

library(shiny)
library(leaflet)   

ui <- fluidPage(
  
  selectInput("weekView", "Week's Revenue:",
              c("1" = "Week.1",
                "2" = "Week.2",
                "3" = "Week.3")), 
  
  leafletOutput("mymap"),
  p()
)

server <- function(input, output, session) {
 
  
  output$mymap <- renderLeaflet({
    
    x <- input$weekView
    
    P<- read.csv("Lebara_weeks_rev4.csv")
    
    as.numeric(P$Long)
    as.numeric(P$Lat)
    as.character(P$Week.1)
    
    P$Week.1 <- as.numeric(gsub(",","",P$Week.1))
    P$Week.2 <- as.numeric(gsub(",","",P$Week.2))
    P$Week.3 <- as.numeric(gsub(",","",P$Week.3))
    
    long <- P$Long
    Lat <- P$Lat
    
    leaflet(data = P) %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      addCircleMarkers(~Long, ~Lat, popup = ~Week.1,
                       radius = ~(Week.1)/40000,
                       stroke = FALSE, 
                       fillOpacity = 0.5)
  })
}

shinyApp(ui, server)

Head of the df = P

    > head(P)
    group      Lat      Long        Country  Week.1  Week.2  Week.3  Week.4
1 178.100 55.37805 -3.435973 United Kingdom 649,613 665,147 640,732 649,642
2 174.890 51.16569 10.451526        Germany 117,766 120,402 104,167  91,157
3 144.100 46.22764  2.213749         France 135,784 117,759 109,337 101,873
4 174.211 52.13263  5.291266    Netherlands 403,950 397,438 377,855 389,345
5 174.990 40.46367 -3.749220          Spain  94,472  95,742  88,313  86,400
6 178.600 56.26392  9.501785        Denmark  70,094  72,487  67,597  66,769

I used tidyr and subsetWeek1 <- P[1:7,] then made variables for the subsetWeek1$value/40000 to make them small enough for the radius values) of those subsets. I tried to make the selectInput 1 = subsetWeek1$value, 2 = subsetWeek2$value and 3 = subsetWeek3$value...

library(tidyr)
P <- tidyr::gather(P, week, value, Week.1:Week.3)

subsetWeek1 <- P[1:7,]
subsetWeek2 <- P[8:14,]
subsetWeek3 <- P[15:21,]

Week1val <- subsetWeek1$value
Week2val <- subsetWeek2$value
Week3val <- subsetWeek3$value

This doesn't seem to pick up the selectInput value and change the map. There are no markers on the map and no errors provided. Could it be the scale of the circles makes them invisible? In the non-app version they are OK.

Upvotes: 0

Views: 2214

Answers (1)

alistaire
alistaire

Reputation: 43334

You're close, you just need to update variable names and clean up a bit. Generally, if you're repeating the same line with a slightly different parameter, there's a better way. An option, using tidyr for gather and extract_numeric:

library(shiny)
library(tidyr)
library(leaflet)

P <- read.csv("Lebara_weeks_rev4.csv")

# do munging that won't change based on input here
P2 <- gather(P, week, value, Week.1:Week.4)    # gather to long form
P2$value <- extract_numeric(P2$value)    # convert to numeric

ui <- fluidPage(
    selectInput("weekView", "Week's Revenue:",
                c("1" = "Week.1",
                  "2" = "Week.2",
                  "3" = "Week.3",
                  "4" = "Week.4")),

    leafletOutput("mymap")
)

server <- function(input, output, session) {

    output$mymap <- renderLeaflet({
        # do munging dependent on input here
        P3 <- P2[P2$week == input$weekView, ]    # subset based on input

        leaflet(data = P3) %>%
            addTiles() %>%  # Add default OpenStreetMap map tiles
            addCircleMarkers(lng = ~Long, lat = ~Lat, 
                             popup = ~format(value, big.mark = ','),    # reinsert commas
                             radius = ~value/40000,
                             stroke = FALSE, 
                             fillOpacity = 0.5)
    })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions