Skip to content

Commit

Permalink
Updated code to have a separate RData/Rds file to use for the locatio…
Browse files Browse the repository at this point in the history
…ns on the map. Otherwise it is too slow (I suspect it maps it as many times as there are observations).
  • Loading branch information
gklarenberg committed Mar 20, 2024
1 parent bf10cd7 commit a1f031e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
18 changes: 17 additions & 1 deletion 02_Cleaning_scripts/WQ_GTMNERR.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,25 @@ which(is.na(WQ$long))
WQ[which(is.na(WQ$lat)),] # duplicates?? Remove for now; emailed Nikki
WQ <- WQ[-which(is.na(WQ$lat)),]

# Create a separate dataframe with only station info, not the data (makes map
# too heavy)
WQ_locations <- WQ %>%
mutate(Year = year(SampleDate)) %>%
select(site_friendly, Year, site_acronym, lat, long, wbid, location) %>%
group_by(site_friendly, site_acronym, lat, long, wbid, location) %>%
summarize(maxYear = max(Year), minYear = min(Year))

WQ_data_available <- WQ %>%
mutate(Year = year(SampleDate)) %>%
select(StationCode, Year, SampleType, ComponentShort, ComponentLong, site_friendly,
site_acronym, lat, long, wbid, location) %>%
distinct()

### 4. Save data ---------------------------------------------------------------

# Save it as an .Rdata (and .Rds?) file so it can be read into the Shiny app
save(WQ, file = "03_Data_for_app/WQ.RData")

saveRDS(WQ, "03_Data_for_app/WQ.Rds")

save(WQ_locations, file = "03_Data_for_app/WQ_locations.RData")
saveRDS(WQ_locations, "03_Data_for_app/WQ_locations.Rds")
Binary file added 03_Data_for_app/WQ_locations.RData
Binary file not shown.
Binary file added 03_Data_for_app/WQ_locations.Rds
Binary file not shown.
13 changes: 8 additions & 5 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ HAB_data_locations <- HAB %>%
distinct() %>%
st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)

#### WQ locations data ------------------------------------------------
load("./03_Data_for_app/WQ_locations.RData")
WQ_data_locations <- WQ_locations %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)

#### WQ data ------------------------------------------------
load("./03_Data_for_app/WQ.RData")
WQ_data_locations <- WQ %>%
distinct() %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)

#### GTMNERR shapefile ------------------------------------------------
GTMNERR <- st_read("./03_Data_for_app/shapefiles_new/GTMNERR.shp")
Expand Down Expand Up @@ -178,8 +180,9 @@ server <- function(input, output) {
# group = "HAB") %>%
addMarkers(data = WQ_data_locations,
popup = ~paste("Station: ", site_friendly, "<br>",
"Location: ", wbid,
"Sample type: ", SampleType),
"Location: ", wbid, "<br>",
"Latest year of sampling: ", maxYear, "<br",
"Sampling start year: ", minYear, "<br"),
group = "WQ") %>%
# # Layers control (turning layers on and off)
addLayersControl(overlayGroups = c("Counties", "GTMNERR boundaries", "WQ"),
Expand Down

0 comments on commit a1f031e

Please sign in to comment.