2
votes

I am using R 3.2.3 through RStudio Version 0.99.491, on Windows 10 64bit... I am creating a leaflet shiny app, using graduated circlemarkers. I want to display different months to show the change in data using selectInput(), but i don't know how to connect it to the 'radius =' argument of addCirclemarker() to make it dynamic. I know I'm just making it up with the 'radius =' argument of addCirclemarker() but I can't tell if I have selectInput() wrong too. here's the data I'm using. The result shows no error message and the map worked when the radius argument was given a single column assignment, ie a static map.

ui.r:

library(shiny)
library(leaflet)

shinyUI(fluidPage(
titlePanel("CAT Rider Count Map"),
sidebarLayout(
sidebarPanel(
  selectInput("var", label = "1. Select the Month", 
              choices = c("April" = 3, "May" = 4, "June" = 5),
              selected = 4)),
mainPanel(leafletOutput('crossact.map')

))))

server.r

library(shiny)
library(googlesheets)
library(leaflet)
gs_auth()
ttt <- gs_auth()
saveRDS(ttt, "ttt.rds")
gs_auth(token = ttt)
gs_auth(token = "ttt.rds")
crossact <- gs_title("crossact")
crossact <- crossact%>% gs_read_csv()



shinyServer(
  function(input, output, session){

colm <- reactive({
  as.numeric(input$var)
})

output$crossact.map <-  renderLeaflet({
################################################################## 
#RADIUS SECTION  
##################################################################  
  crossact.map <- leaflet(crossact) %>% 
    addTiles('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png')
  crossact.map%>% setView(-71.43381, 42.48649, zoom = 17)
  crossact.map %>% ***addCircleMarkers(~lng, ~lat, popup=~crossact$name, weight =1,
    radius=~(crossact[,colm()]), 
                                    color="#ffa500", stroke = TRUE,     fillOpacity = 0.6)
})
})

thanks!

1
I think I've found a solution. I'll post the code once I've marked up the code - Stephen Engley

1 Answers

1
votes

For the solution to my specific problem, I used code from the superzip app, for anyone making leaflet shiny apps with markers, this seems to have it all.
http://shiny.rstudio.com/gallery/superzip-example.html (hit the Get Code button and it will send you to Github)

Correct me if I'm wrong, but, sizeBy <- input$size pull the values from the choice argument, and is the bridge to the selectInput() function. radius <- crossact[[sizeBy]] assigns the overlapping strings from the data.frame object to the selectInput() variable sizeBy by making the variable radius. For this to work, the map function must have an observer({}) wrapper to have it update itself when the selection changes.

ui.r

library(shiny)
library(leaflet)

#this is the assignment of columns to the choices argument in selectinput() 
vars <- c(
  "April" = "April",
  "May" = "May",
  "June" = "June")


shinyUI(fluidPage(
  h5("Integrating Leaflet With Shiny"), 
  titlePanel("CAT Rider Count Map"),
  sidebarLayout(

sidebarPanel(
  selectInput("size", "Size", vars, selected = "April")),
  mainPanel(leafletOutput('crossact.map') 
))))

Server.r

library(shiny) 
library(googlesheets)
library(leaflet)

#google authorization, token storage, file acquisition and assignment

gs_auth()
ttt <- gs_auth()
saveRDS(ttt, "ttt.rds")
gs_auth(token = ttt)
gs_auth(token = "ttt.rds")
crossact <- gs_title("crossact")
crossact <- crossact%>% gs_read_csv()



shinyServer(
  function(input, output, session){

####observer is used to maintain the circle size. 

observe({
 #####this connects selectInput and assigns the radius value
  sizeBy <- input$size
  radius <- crossact[[sizeBy]]

output$crossact.map <-  renderLeaflet({
  crossact.map <- leaflet(crossact) %>% 
    addTiles('http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png')
  crossact.map%>% setView(-71.43381, 42.48649, zoom = 17)
  crossact.map %>% addCircleMarkers(~lng, ~lat, popup=~crossact$name, weight = 1,radius = radius, 
                                    color="#ffa500", stroke = TRUE, fillOpacity = 0.6)
    })
  })
})