For the last few hours I have been playing with my coding, using forms of if statements, observeEvent and leafletProxy, but I can't seem to get it working. The life table is life expectancy data. It is three columns, COUNTY, male, female. I can't seem to get the radio button to work and it always stays at male.
Basically, I want my leaflet map to give different layouts based on the input of the radio button. Either getting male life expectancy and female, as well as getting the popup.
library(shiny)
library(leaflet)
library(tigris)
library(dplyr)
lnd <- counties(state = 'IN', cb = TRUE, resolution = '20m')
#Read life table
life <- read.table("life.txt",sep="\t", header=TRUE)
lnd@data <- left_join(lnd@data, life, by = c('NAME' = 'county'))
popup <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$male,2))
popup2 <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$female,2))
ui <- shinyUI(fluidPage(#theme = "bootstrap.css",
titlePanel("Life Exptectancy in Indiana Counties: "),
h3("A Spatial Analysis Project"),
br(),
sidebarLayout(position = "right",
sidebarPanel(
tags$b("Life Expectancy in Indiana, U.S.A."),
hr(),
radioButtons("gender", "Gender:",c("Male"="Male","Female"="Female")),
hr()
),
mainPanel(
leafletOutput("map")
)
)
))
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet(lnd) %>%
addPolygons() %>%
addProviderTiles(providers$CartoDB.Positron)
})
observeEvent (input$gender == "Male", {
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup)
})
observeEvent (input$gender == "Female", {
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup2)
})
}
shinyApp(ui, server)
This is a photo of my appThe app remains blue, and keeps the same male data no matter if I click female or male. I know it has to do with something with leafletProxy but cant figure it out