Skip to content

Commit 9b1bda5

Browse files
committed
Switch from MapLibre GL to Leaflet for better shinyapps.io compatibility
- Replace mapgl/sf packages with leaflet (avoids terra dependency issues) - Update GitHub Actions workflow to install leaflet instead of mapgl/sf - Update README technical stack to reflect Leaflet usage - No API tokens required with Leaflet (simpler deployment)
1 parent 31ef640 commit 9b1bda5

File tree

3 files changed

+31
-57
lines changed

3 files changed

+31
-57
lines changed

.github/workflows/deploy-shinyapps.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
4040
- name: Install R dependencies
4141
run: |
42-
R -e "install.packages(c('rsconnect', 'shiny', 'bslib', 'dplyr', 'lubridate', 'DT', 'shinyjs', 'tidyr', 'plotly', 'jsonlite', 'scales', 'mapgl', 'sf', 'httr2', 'tidygeocoder'))"
42+
R -e "install.packages(c('rsconnect', 'shiny', 'bslib', 'dplyr', 'lubridate', 'DT', 'shinyjs', 'tidyr', 'plotly', 'jsonlite', 'scales', 'leaflet', 'httr2', 'tidygeocoder'))"
4343
4444
- name: Determine app to deploy
4545
id: determine-app

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ These tools solve real problems I've encountered in client work.
5555
- **GitHub Actions** for automated deployment
5656
- **Plotly** for interactive visualizations
5757
- **DT** for data tables
58-
- **MapLibre GL** for interactive maps
58+
- **Leaflet** for interactive maps
5959

6060
## 📈 Want Custom Solutions?
6161

grant-research-assistant/app.R

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ library(jsonlite)
66
library(dplyr)
77
library(DT)
88
library(plotly)
9-
library(mapgl)
10-
library(sf)
11-
12-
# Set Mapbox token (will use environment variable if available)
13-
if (Sys.getenv("MAPBOX_PUBLIC_TOKEN") == "") {
14-
# Fallback: check for old variable name
15-
if (Sys.getenv("MAPBOX_ACCESS_TOKEN") != "") {
16-
Sys.setenv(MAPBOX_PUBLIC_TOKEN = Sys.getenv("MAPBOX_ACCESS_TOKEN"))
17-
}
18-
}
9+
library(leaflet)
10+
# library(mapgl) # Removed to avoid terra/sf dependency on shinyapps.io
11+
# library(sf) # Removed to avoid terra dependency on shinyapps.io
12+
13+
# Using Leaflet for mapping (no API tokens required)
1914

2015
# Load foundation data
2116
load_foundation_data <- function() {
@@ -53,7 +48,7 @@ load_metadata <- function() {
5348
return(fromJSON(meta_file))
5449
} else {
5550
return(list(
56-
last_updated = Sys.time(),
51+
last_updated = format(Sys.Date(), "%B %Y"),
5752
states_included = c("CT", "MA", "ME", "NH", "RI", "VT"),
5853
total_foundations = nrow(foundations_data),
5954
data_source = "ProPublica Nonprofit Explorer API (Sample Data)"
@@ -363,7 +358,7 @@ ui <- fluidPage(
363358

364359
# Map with overlay legend
365360
div(style = "position: relative;",
366-
mapboxglOutput("foundation_map", height = "700px"),
361+
leafletOutput("foundation_map", height = "700px"),
367362

368363
# Compact legend overlay (top-left corner)
369364
div(style = "position: absolute; top: 10px; left: 10px; background: rgba(255, 255, 255, 0.95); border: 2px solid #e0e0e0; padding: 12px 15px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); max-width: 280px; font-size: 12px; z-index: 1000;",
@@ -807,7 +802,7 @@ server <- function(input, output, session) {
807802
})
808803

809804
# Render map
810-
output$foundation_map <- renderMapboxgl({
805+
output$foundation_map <- renderLeaflet({
811806
data <- filtered_foundations()
812807

813808
# Filter to only rows with valid coordinates
@@ -817,11 +812,9 @@ server <- function(input, output, session) {
817812
if (nrow(map_data) == 0) {
818813
# Return empty map if no geocoded data
819814
return(
820-
mapboxgl(
821-
style = mapbox_style("light"),
822-
center = c(-71.5, 43.5), # Center on New England
823-
zoom = 6
824-
)
815+
leaflet() %>%
816+
addTiles() %>%
817+
setView(lng = -71.5, lat = 43.5, zoom = 6) # Center on New England
825818
)
826819
}
827820

@@ -935,44 +928,25 @@ server <- function(input, output, session) {
935928
return(html)
936929
})
937930

938-
# Convert to sf object for mapgl
939-
map_sf <- st_as_sf(
940-
map_data,
941-
coords = c("longitude", "latitude"),
942-
crs = 4326 # WGS84 coordinate system
943-
)
944-
945-
# Create map
946-
mapboxgl(
947-
style = mapbox_style("light"),
948-
center = c(
949-
mean(map_data$longitude, na.rm = TRUE),
950-
mean(map_data$latitude, na.rm = TRUE)
951-
),
952-
zoom = 6,
953-
pitch = 0,
954-
bearing = 0
955-
) %>%
956-
add_circle_layer(
957-
id = "foundations",
958-
source = map_sf,
959-
circle_color = match_expr(
960-
"color",
961-
values = c("#27ae60", "#f1c40f", "#e74c3c", "#95a5a6"),
962-
stops = c("#27ae60", "#f1c40f", "#e74c3c", "#95a5a6")
963-
),
964-
circle_radius = interpolate(
965-
column = "circle_size",
966-
values = c(5, 20),
967-
stops = c(5, 20)
968-
),
969-
circle_opacity = 0.8,
970-
circle_stroke_color = "#ffffff",
971-
circle_stroke_width = 2,
972-
popup = "popup_html" # Changed from tooltip to popup for click interaction
931+
# Create Leaflet map
932+
leaflet(map_data) %>%
933+
addTiles() %>%
934+
setView(
935+
lng = mean(map_data$longitude, na.rm = TRUE),
936+
lat = mean(map_data$latitude, na.rm = TRUE),
937+
zoom = 6
973938
) %>%
974-
add_navigation_control(position = "top-right") %>%
975-
add_fullscreen_control(position = "top-right")
939+
addCircleMarkers(
940+
lng = ~longitude,
941+
lat = ~latitude,
942+
radius = ~circle_size,
943+
color = "white",
944+
weight = 2,
945+
opacity = 1,
946+
fillColor = ~color,
947+
fillOpacity = 0.8,
948+
popup = ~popup_html
949+
)
976950
})
977951
}
978952

0 commit comments

Comments
 (0)