1

I am currently struggling to map multiple polygons in a shiny app. The purpose of the shiny app is to take some data pertaining to disease spread in a number of states and map the areas of highest risk. The app must be able to map multiple states at the click of the "Start!" button.

(Note: This app is very large (6000+ lines in total) so only relevant code will be shown here, I don't want to burden the ones trying to help me)

Excerpts from:

Server.R

#The purpose of col_inputs and col_names is to create a two-dimensional array with all of the input parameters for the function. This was done to maintain compatibility with some legacy code. Catted_states on the other hand   combines all states selected into a list. 
(Example: c("AZ","FL","VA")

output$gm <- renderLeaflet({
        global_map(ARG_1, ARG_2, ARG_3)
    })

Global_Map.R

The only real concerns with this code is that 'M' isn't being drawn at all after the for loop finishes.

global_map <- function(col_names, col_inputs, catted_states) {


User_para <- array(0, dim = c(16, 2))

for( I in 1:length(states) {
if (state_num > 10) {
read.csv(Loop specific file)
}

 if (state_num < 10) {
read.csv(Loop specific file) 
}
    state_num * Loop specific calculation[I]        


    pal <- colorNumeric(palette = "Purples", domain = state_output$risk)
    pal_sR <- pal(state_output$risk)
    m <- addProviderTiles(m, "CartoDB.Positron")
    m <- addLegend(m, title = "Risk", pal = pal, values = ~state_output$risk, 
        opacity = 0.7)
    m <- addPolygons(m, stroke = FALSE, smoothFactor = 0, fillOpacity = 0.5, 
        color = ~pal_sR)


} 

}

How can I get this code to map the multiple states? What is incorrect about my leaflet calls? I need this code to load multiple shape files into shiny and draw polygons once on each shape file and map them accordingly

R.DeRienzo
  • 21
  • 1
  • 6
  • 2
    you're more likely to get a response if you make your example [reproducible](http://stackoverflow.com/q/5963269/5977215) - i.e., put togther a minimal example that replicates your problem that someone else can copy & paste into their enviroment. – SymbolixAU Jun 02 '16 at 22:00
  • Thank you for the advice. I will update the question accordingly @SymbolixAU – R.DeRienzo Jun 07 '16 at 17:14

1 Answers1

0

I am not really sure if that solves your problem, but your example is absolutely not reproducible and also has several errors. If you want to produce several polygons inside a for loop and then add them to a leaflet map, here is the code:

library(shiny)
library(leaflet)

ui <- fluidPage(
  sliderInput("nPolys", "How many Loops", min = 1, max = 20, value = 3),
  ## Map
  leafletOutput("gm")
)

server <- function(input, output) {

  ## Initialize map
  m = leaflet() %>% addTiles()


  ## Render Map
  output$gm <- renderLeaflet({
    ## Loop
    for (I in 1:input$nPolys) {
      ## Create dummy polygons
      Sr1 = Polygon(cbind(c(2,4,4,1,2)*runif(1,1,10),c(2,3,5,4,2)*runif(1,1,10)))
      Sr2 = Polygon(cbind(c(5,4,2,5)*runif(1,1,10),c(2,3,2,2)*runif(1,1,10)))
      Srs1 = Polygons(list(Sr1), "s1"); Srs2 = Polygons(list(Sr2), "s2")
      SpP = SpatialPolygons(list(Srs1,Srs2), 1:2)

      ## add Polygons to map
      m <- addPolygons(m, data=SpP, stroke = FALSE, smoothFactor = 0, fillOpacity = 0.5) 
    }

    ## Call map !
    m
  })
}

shinyApp(ui, server)
SeGa
  • 9,454
  • 3
  • 31
  • 70