For this assignment, you will learn how to deploy a custom RShiny app using iNaturalist data.

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.

Please refer to this template while developing your own app. This is approximately what your final product should look like


Let’s Get Started!

First, make sure that you have downloaded observations for your taxa/region of interest as a .csv file:

  • Go to the iNaturalist website
    • Make an account if needed, and log in
  • On the home page task bar, click ‘Explore’
  • To the right of the search bars, click ‘Filters’. Check the ‘Research Grade’ box
  • Specify your taxa and region of interest (can be state specific or US-wide)
    • This assignment will work best if you have between 200 and 10,000 observations
    • Suitable examples include: Rattlesnakes in PA, Pawpaw tress in PA, Litocala moth in U.S., Wild Blue Indigo in U.S.
    • When you are happy with your taxa selection, click the gray ‘Filters’ button again and in the bottom right corner, click ‘Download’.
      • This will direct you to a new page that says ‘Export Observations’
      • Scroll down to “3 / Choose columns
        • Under the ‘Geo’ section, check “place_county_name” if data is state-specific, or “place_state_name” if data is nationwide.
      • Scroll further down to “4 / Create Export
      • Your .csv will be available for download on this same webpage

Pairing iNat and CDL data

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!