How to create a reactive shiny leaflet map with for loop

leaflet
rstudio

#1

My aim is to create a reactive shiny leaflet app based on two user inputs: income and percentage willing to save per year in order to render a new map that would tell them how many years they would need to save in order to buy a dwelling in that area. I already have a column in my dataset for years needed to save, but I’d like to overwrite that with the new user inputs.

I’m having a problem joining the user inputs to my out map. Any suggestions would be greatly appreciated :slight_smile:

UI:

ui <-tabsetPanel(
    tabPanel("Application",fluidPage(theme = "bootstrap.css",

 h1("2016 Housing Landscape",  align = "center"),

leafletOutput("map1", height = "600px", width = "100%"),
h3("Here I've mapped out blah blah blah"),
  #gimme a title
  absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                draggable = TRUE, top = 60, left = 20, right = 20, bottom = "auto",
                width = 330, height = "auto", style='padding:15px',

                numericInput("num1",h3("Income"), value = 0),
                numericInput("num2",h3("% Willing to Save per Year"), value = 0),
                actionButton("recalc", "Go!"),
                #gimme a drop down to select variables from the shapefile 
                #boundaries dataset
                selectInput("variable", "Variable",
                            names(CTVan16@data)[11:15]),
                #gimme some colour options from colourbrewer
                #selectInput("colourbrewerpalette", "Color Scheme",
                           # rownames(subset(brewer.pal.info, category %in% c("seq", "div")))
                #),
                selectInput("classIntStyle", "Interval Style",
                            c("Jenks Natural Breaks" = "jenks",
                              "Quantile" = "quantile",
                              "Equal Interval" = "equal",
                              "Pretty" = "pretty"))



      )
    )
)

Server:

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

  yrsav <- function(income,pct_save,mean_dwelling_cost){
    downpayment <- 0
    if(mean_dwelling_cost <= 500000) downpayment <- 0.05
    else if(mean_dwelling_cost <= 999999) downpayment <- 0.1
    else downpayment <- 0.2
    dec_save <- pct_save/100
    return ((mean_dwelling_cost * (dec_save+downpayment))/income)
  }

  #2016  

  points <- eventReactive(input$recalc, {

    i <- 0
    for(i in (c(1:length(CTVan16@data$Average.Dwelling.Cost)))){
      CTVan16@data$Years.Needed.to.Save[i] <- yrsav(input$num1,input$num2,CTVan16@data$Average.Dwelling.Cost[i])
    }

    }, ignoreNULL = FALSE)

  output$map1 <- renderLeaflet({
    leaflet(CTVan16) %>% addProviderTiles("CartoDB.Positron") %>%
      setView(-123.116226, 49.246292, zoom = 10) %>%
        addPolygons(data = points(),
                    stroke = F, 
                    fillOpacity = 0.5,
                    smoothFactor = 0.5,
                    opacity = 1,
                    fillColor = "white")
  })