This crash course introduction to RShiny should give you the initial tools and guidelines for making effective data visualization tools that can serve
…depending on different needs/interests. The goal is for you use the structure below to develop materials and resources to communicate your own research publicly and effectively.
Now that we have the data, we need to pair the lat/longs associated with each logged iNaturalist observation to 2024 land use data (under “class_name”) provided by the USDA Cropland Data Layer (CDL). You can query the USDA CDL database directly in R Studio using the CropscapeR package - and I encourage you to explore this on your own time if you are interested.
For simplicity, click here to download this static file of the 2024 CDL layer, at 30 x 30 m resolution across the domestic US. It is a 2.4 GB file that will allow us to pull land use categories from any observation, anywhere in the contiguous US.
You will also need to download this cdl_colormap.csv before moving to the next step.
Once the files are downloaded, and you have opened a new RStudio project, click and drag all three files (YourTaxa.csv from iNat and 2024_30m_cdls.tif/cdl_colormap.csv from OneDrive) into your working directory
if you are up for it, you can instead import these files directly using your R terminal.
Once you have all three files data parked in your working directory, it’s time to return to RStudio to install/load the necessary packages.
If the packages below are not already installed, you will need do that first via the install.packages command, and then load them.
# ---- PACKAGES ----
#install.packages("terra") #etc if not already installed
library(terra) # for CDL raster + extract
library(sf) # for spatial points
library(dplyr)
library(readr)
library(lubridate)
Now you need to define inputs for the different files that are referenced/generated when connecting land use data to your iNat observations. Please make sure to update files names ‘YourTaxa.csv’ and ‘YourTaxa_CDL.csv’ to match whatever file names make sense to you/your taxa. This will also be important later.
# ---- INPUTS ----
csv_path <- "data/YourTaxa.csv" # your iNat observations
cdl_path <- "data/2024_30m_cdls.tif" # 2024 CDL GeoTIFF (downloaded from USDA)
lut_path <- "data/cdl_colormap.csv" # lookup table for labels
out_path <- "data/YourTaxa_CDL.csv" # new file with CDL info
Next is the section where we are telling R to reference the lat/longs from YourTaxa.csv, retrieve the associated CDL land use categories, and create a new .csv that adds a “class_name” column to the end of the iNat metadata.
Please do not change any of the code below before running (it will break). Just copy/paste/run and move on for now. Feel free to explore and experiment with this on your own time.
# ---- LOAD POINT DATA ----
Species1 <- read_csv(csv_path, show_col_types = FALSE)
# Convert to sf (WGS84)
pts_sf <- st_as_sf(Species1, coords = c("longitude","latitude"), crs = 4326, remove = FALSE)
# ---- LOAD & PREP RASTER ----
cdl <- terra::rast(cdl_path) # SpatRaster (CDL)
r_crs <- terra::crs(cdl, proj=TRUE)
# Reproject points to raster CRS
pts_sf_proj <- st_transform(pts_sf, r_crs)
pts_vect <- terra::vect(pts_sf_proj)
# ---- EXTRACT CDL VALUES ----
cdl_info <- terra::extract(cdl, pts_vect) # returns ID + layer value
# identify the value column
val_col <- setdiff(names(cdl_info), "ID")[1]
Species1$cdl_value <- cdl_info[[val_col]]
# ---- JOIN HUMAN-READABLE CLASS NAMES ----
cdl_lut <- read_csv(lut_path, show_col_types = FALSE)
cdl_lut <- cdl_lut %>%
mutate(value = as.integer(value)) %>%
select(value, class_name)
Species1 <- Species1 %>%
mutate(cdl_value = as.integer(cdl_value)) %>%
left_join(cdl_lut, by = c("cdl_value" = "value"))
# ---- SAVE UPDATED CSV ----
write_csv(Species1, out_path)
cat("Added CDL columns and wrote to:", out_path, "\n")
MyTaxa_CDL.csv should now pop up in the bottom left window of RStudio - in which case you are ready to start Building your app by using this assignment’s second HTML tutorial.
On your RStudio taskbar go to File -> New File -> Shiny Web App… and we will meet you there!