Mapping in R - Shiny, leaflet.
Been trying to figure out how to display my line plots (library - ggplot2) as popups in my map. Primarily, I'm using popupGraph (leafpop) to do this. Basically want to display line graphs where x is time, and y is new covid cases. When the user clicks on a county, the popup should only display a graph for the plot of that county clicked.
The popups work, but they're blank, they don't show my graphs. Not sure what I'm doing wrong. I've been looking through other stackOverflow problems to help me solve mine (primarily: Control the size of popupGraph() from leaflet in r shiny, R Leaflet PopupGraph - addPopupGraphs on map_marker_click) But I'm drawing a blank, and there doesn't seem to be a lot of online resources for this function. Any suggestions or ideas on why my graphs aren't plotting? I can't tell if it's a simple syntax problem, or if I'm approaching the function completely wrong.
Here's my data for variables for US counties, "counties1" and covid-19 cases in those counties, "df": (Edited for replicability -- Autauga County in Alabama sample)
##* COVID-19 DATA:
head(df)
FIPS Admin2 Province_State Lat Long_ date value
637 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-22 0
638 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-23 1
639 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-24 2
640 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-25 3
641 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-26 4
642 1001 Autauga-AL AL 32.53953 -86.64408 2020-01-27 5
>
dput(head(df))
structure(list(
FIPS = c(1001L, 1001L, 1001L, 1001L, 1001L, 1001L),
Admin2 = c("Autauga-AL", "Autauga-AL", "Autauga-AL", "Autauga-AL", "Autauga-AL", "Autauga-AL"),
Province_State = c("AL", "AL", "AL", "AL", "AL", "AL"),
Lat = c(32.53952745, 32.53952745, 32.53952745,
32.53952745, 32.53952745, 32.53952745),
Long_ = c(-86.64408227, -86.64408227, -86.64408227, -86.64408227, -86.64408227, -86.64408227),
date = structure(c(18283, 18284, 18285, 18286, 18287, 18288), class = "Date"),
value = c(0, 1, 2, 3, 4, 5)),
row.names = 637:642, class = "data.frame")
###COUNTY (polygons) DATA:
> head(counties1)
STATEFP COUNTYFP GEOID NAME ALAND AWATER INTPTLAT
1 01 001 01001 Autauga-AL 1539614693 25744269 +32.5322367
INTPTLON STATE_NAME
0 -086.6464395 AL
> dput(head(counties1))
structure(list(
STATEFP = c("01"),
COUNTYFP = c("001"),
GEOID = c("01001"),
NAME = c("Autauga-AL"),
ALAND = c("1539614693"),
AWATER = c(""25744269""),
INTPTLAT = c("+32.5322367"),
INTPTLON = c("-086.6464395"),
STATE_NAME = c("AL"),
row.names = 0:1, class = "data.frame")
Here's my UI:
library(shiny)
library(shinydashboard)
library(leafpop)
library(sf)
library(tidyverse)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(leafletOutput("map"),
)
Here's my server, I think my problem is with the function called "subset_county". I've commented on it to show what I think I'm trying to accomplish in it. Sorry, I've only been using R Shiny for a few months and haven't yet fully grasped how to create reactive functions:
server <- function(session, input, output)
### subset counties based on click event, maybe?
subset_county <- reactive({
click_it <- input$map_shape_click ##click on county polygon to create reactive click function...
data <- df[df$Admin2 == click_it$NAME,] ## … where data from df is pulled - when the column for df$Admin2 is the same as the name of the clicked polygon (*Thanks @akrun for the edit on brackets over parentheses.)
ggplot(data= data(),
aes(x= date, y = value)) + geom_line()
})
##### or should the plot be rendered outside the subset_county function, in renderPlot()?
# output$plot <-renderPlot(
# plot <- ggplot(data= subset_county(),
# aes(x= date, y = value)) + geom_line()
# )
############ DISPLAY MAP
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addPolygons(data=counties,
color = "black",
weight = 0.1,
opacity = 1.0,
fillOpacity = 0.15,
fillColor = "transparent",
group= "counties",
popup = popupGraph(subset_county),
layerId = ~counties1$GEOID
) %>%
addLayersControl( baseGroups = c("CartoDB.DarkMatter"),
overlayGroups = c("counties"),
})
}
########################################################################################
shinyApp(ui = ui, server = server)
I hope my data is presentable enough! Any suggestions or resources for how I can solve my popupGraph issue?