Skip to content

Commit

Permalink
FINAL prototype1 changes: moved making a locations df to a separate s…
Browse files Browse the repository at this point in the history
…cript (because the stations on the main page were not showing up properly). This script creates separate location dfs for the datasets, and one for all for the main page. Also added HAB locations to the map (different colors and maps and all!)
  • Loading branch information
gklarenberg committed Sep 6, 2024
1 parent f36cd35 commit 462e688
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 68 deletions.
115 changes: 115 additions & 0 deletions 02_Cleaning_scripts/Create_location_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
########################################################################
########## NERRS Science Transfer project - GTMNERR #############
########################################################################

# Geraldine Klarenberg, PhD
# [email protected]
# 6 Sep 2024

# Load packages
library(tidyverse)

# Script to create file with locations, for mapping

#### Water Quality ####
# import all WQ data
WQ_df <- readRDS("./03_Data_for_app/WQ_all.Rds")

WQ_years <- WQ_df %>% # site friendly and station code are the names in common
filter(variable %in% c("StationCode", "site_friendly",
"geometry",
"SampleDate", # changed, from StartDate - but also not necessary for locations
"Latitude",
"Longitude",
"data_source")
) %>%
#select(c(RowID, variable, value)) %>% # not necessary
#distinct(RowID, variable, value) %>% # not necessary
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
mutate(
SampleDate = parse_date_time(SampleDate, c("ymd_HMS", "ymd")), # get a warning about 311 failed to parse, but they did, only as ymd
year = year(SampleDate),
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude)
) %>%
group_by(Latitude, Longitude, site_friendly) %>%
summarize(minYear = min(year(SampleDate)),
maxYear = max(year(SampleDate)))

# make dataframe for map display and hover data
WQ_data_locations = WQ_df %>% # site friendly and station code are the names in common
filter(variable %in% c("StationCode", "site_friendly",
"geometry",
"SampleDate", # changed, from StartDate - but also not necessary for locations
"Latitude",
"Longitude",
"data_source")
) %>%
#select(c(RowID, variable, value)) %>% # not necessary
#distinct(RowID, variable, value) %>% # not necessary
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
mutate(
#SampleDate = ymd_hms(SampleDate), # get a warning about 311 failed to parse, but they did, only as ymd
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude)
) %>%
select(-SampleDate) %>%
distinct(Latitude, Longitude, geometry, data_source, StationCode, site_friendly)

WQ_data_locations <- WQ_data_locations %>%
left_join(WQ_years)

# Save data
saveRDS(WQ_data_locations, "03_Data_for_app/WQ_data_locations.Rds")

WQ_data_locations <- WQ_data_locations %>%
select(-geometry)

#### Algae ####
HAB_df <- readRDS("03_Data_for_app/HAB.Rds")

# Get min/max years of measurements
HAB_years <- HAB_df %>%
mutate(date = dmy(`Sample Date`),
year = year(date)) %>%
group_by(Latitude, Longitude, Site) %>%
summarize(minYear = min(year),
maxYear = max(year)) %>%
rename(site_friendly = Site)

HAB_data_locations <- HAB_df %>%
distinct(Latitude, Longitude, Site) %>%
rename(site_friendly = Site) %>%
mutate(data_source = "FWC") # or GTMNERR?

HAB_data_locations <- HAB_data_locations %>%
left_join(HAB_years)

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


#### Shellfish ####



#### All - main page ####
# Add dataset type to dfs
WQ_data_locations["type"] <- "Water Quality"
HAB_data_locations["type"] <- "Algae"


# Merge
all_data_locations <- WQ_data_locations %>%
full_join(HAB_data_locations)

# Save
saveRDS(all_data_locations, "03_Data_for_app/all_data_locations.Rds")

Binary file added 03_Data_for_app/HAB_data_locations.Rds
Binary file not shown.
Binary file added 03_Data_for_app/WQ_data_locations.Rds
Binary file not shown.
Binary file added 03_Data_for_app/all_data_locations.Rds
Binary file not shown.
87 changes: 43 additions & 44 deletions modules/main_page.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,46 @@
# I am putting this here right now, but I feel we should move this to a cleaning
# script so it doesn't need to be run every time someone uses the app (as with
# WQ locations)
WIN_df <- readRDS("./03_Data_for_app/WIN.Rds")
# WIN_df <- readRDS("./03_Data_for_app/WIN.Rds")
#
# WIN_data_locations = WIN_df %>%
# filter(variable %in% c("geometry",
# "StationCode",
# "SampleDate",
# "Latitude",
# "Longitude")
# ) %>%
# select(c(RowID, variable, value)) %>%
# distinct(RowID, variable, value) %>%
# pivot_wider(
# names_from = variable,
# values_from = value,
# values_fill = list(value = NA)
# ) %>%
# distinct(geometry, StationCode, SampleDate, Latitude, Longitude) %>%
# mutate(
# SampleDate = ymd_hms(SampleDate),
# Latitude = as.numeric(Latitude),
# Longitude = as.numeric(Longitude),
# type = "Water quality",
# dataset = "Watershed Information Network (DEP)", # Update this so we use data_source
# minYear = min(year(SampleDate)),
# maxYear = max(year(SampleDate))
# ) %>%
# select(-geometry, -SampleDate)

WIN_data_locations = WIN_df %>%
filter(variable %in% c("geometry",
"StationCode",
"SampleDate",
"Latitude",
"Longitude")
) %>%
select(c(RowID, variable, value)) %>%
distinct(RowID, variable, value) %>%
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
distinct(geometry, StationCode, SampleDate, Latitude, Longitude) %>%
mutate(
SampleDate = ymd_hms(SampleDate),
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude),
type = "Water quality",
dataset = "Watershed Information Network (DEP)", # Update this so we use data_source
minYear = min(year(SampleDate)),
maxYear = max(year(SampleDate))
) %>%
select(-geometry, -SampleDate)
#### Location data ------------------------------------------------
all_data_locations <- readRDS("./03_Data_for_app/all_data_locations.Rds")

#### WQ locations data ------------------------------------------------
WQ_data_locations <- readRDS("./03_Data_for_app/WQ_locations.Rds")
# add info for icons and colors
all_data_locations <- all_data_locations %>%
mutate(group_icon = case_when(
type == "Water Quality" ~ "flask",
type == "Algae" ~ "microscope"),
group_color = case_when(
type == "Water Quality" ~ "orange",
type == "Algae" ~ "purple"))

# For now, for testing, make WQ_locations the dataframe to be used for filtering
datasets_location <- full_join(WQ_data_locations, WIN_data_locations)
datasets_location <- st_as_sf(datasets_location, coords = c("Longitude", "Latitude"), crs = 4326)
datasets_location <- datasets_location[!duplicated(datasets_location),]

color_palette <- colorFactor(palette = c("red", "goldenrod1"), #, "blue", "green"
domain = datasets_location$dataset)

#### HAB locations data ------------------------------------------------
# Add this at some point so there is another dataset that shows up in the dropdown
# menu

### Define the UI -------------------------------------------------------------
mainPageUI <- function(id) {
Expand Down Expand Up @@ -92,8 +90,8 @@ mainPageServer <- function(input, output, session) {
selectInput(
inputId = ns("datatype_selector"),
label = "Select a type of data to see locations with data availability",
choices = unique(datasets_location$type),
selected = unique(datasets_location$type)[1]
choices = unique(all_data_locations$type),
selected = unique(all_data_locations$type)[1]
)
})

Expand Down Expand Up @@ -147,15 +145,16 @@ mainPageServer <- function(input, output, session) {
# Select dataset to add markers to the plot
observeEvent(input$datatype_selector, {
# Filter data based on selected group
filtered_data <- datasets_location[datasets_location$type == input$datatype_selector,]
filtered_data <- all_data_locations[all_data_locations$type == input$datatype_selector,]
print(filtered_data)
# Add markers to the map - commented out the popup bc this only works for WQ data
print("Adding markers")
leafletProxy(ns("map")) %>%
clearMarkers() %>%
addAwesomeMarkers(icon = makeAwesomeIcon(icon = "flask", markerColor = "orange", library = "fa",
iconColor = "black"),
addAwesomeMarkers(
data = filtered_data,
icon = makeAwesomeIcon(icon = ~group_icon, markerColor = ~group_color, library = "fa",
iconColor = "black"),
options = markerOptions(riseOnHover = TRUE), # Brings marker forward when hovering
popup = ~paste("<b>Station:</b> ", site_friendly, "<br>", # popups appear when clicking
"<b>Sampling start year:</b> ", minYear, "<br>",
Expand Down
28 changes: 4 additions & 24 deletions modules/waterquality.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,11 @@ library(tidyverse)
# import all WQ data for this page (WIN and Guana spreadsheet)
WQ_df <- readRDS("./03_Data_for_app/WQ_all.Rds")

#### Process data further ####
# MOVE THIS TO THE CLEANING SCRIPT!!
# make datframe for map display and hover data
WQ_data_locations = WQ_df %>% # site friendly and station code are the names in common
filter(variable %in% c("StationCode", "site_friendly",
"geometry",
#"HUC12Name", # Changed for now (bc GTM WQ data does not have that variable) BUT we should also include a station name of some sort
"SampleDate", # changed, from StartDate - but also not necessary for locations
"Latitude",
"Longitude",
"data_source")
) %>%
#select(c(RowID, variable, value)) %>% # not necessary
#distinct(RowID, variable, value) %>% # not necessary
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
distinct(Latitude, Longitude, data_source, geometry, StationCode, site_friendly) %>%
mutate(
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude)
)
# Location data
WQ_data_locations <- readRDS("./03_Data_for_app/WQ_data_locations.Rds")

#### Process data further ####
# Also move this to cleaning script
WQ_data_units = WQ_df %>%
filter(variable %in% c("ComponentLong", # some will be duplicated because they differ between the 2 sets
"Unit", "data_source")
Expand Down

0 comments on commit 462e688

Please sign in to comment.