Skip to content

Commit

Permalink
Merge pull request #8 from gl-eb/develop
Browse files Browse the repository at this point in the history
Check whether location is in eBird data
  • Loading branch information
gl-eb authored Sep 9, 2022
2 parents 9fc937f + 5cc6ecd commit 44e1d30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions R/yardlistr_functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# functions ---------------------------------------------------------------
# universal functions -----------------------------------------------------

# return list of unique values without changing their type
unique_values <- function(x) x |> unique() |> tibble::deframe() |> sort()
Expand Down Expand Up @@ -28,14 +28,18 @@ tetrad <- function(x, method = "ebird") {
}
}


# specific functions ------------------------------------------------------

# make best guess which file from input folder to use
select_file <- function(dir_dat) {
# get all files with csv suffix and return last one
files <- fs::dir_ls(dir_dat, type = "file", glob = "*.csv")
return(files[length(files)])
}

clean_ebird_data <- function(data_file, yard_location) {
clean_ebird_data <- function(data_file, yard_location,
call = rlang::caller_env()) {
message_file <- stringr::str_glue("Importing Data from ", data_file)
rlang::inform(message_file)

Expand All @@ -44,18 +48,20 @@ clean_ebird_data <- function(data_file, yard_location) {
readr::read_csv(show_col_types = FALSE) |>
suppressWarnings()

# filter data by location
dat_location <- check_location(raw_dat, yard_location, call)

# filter data by location and sort by datetime
dat <- raw_dat |>
dat_cleaned <- dat_location |>
rename(
"common" = "Common Name",
"scientific" = "Scientific Name",
"taxon" = "Taxonomic Order",
"complete" = "All Obs Reported"
) |>
filter(
Location == yard_location & # only selected location
!stringr::str_detect(scientific, "sp.") & # filter out spuhs
!stringr::str_detect(scientific, "/") # filter out slashes
!stringr::str_detect(scientific, "sp.") & # filter out spuhs
!stringr::str_detect(scientific, "/") # filter out slashes
) |>
tidyr::drop_na(any_of(c("Date", "Time"))) |>
tidyr::unite("datetime", Date:Time, sep = " ") |>
Expand All @@ -72,3 +78,17 @@ clean_ebird_data <- function(data_file, yard_location) {
tidyr::unite("species", c("common", "scientific"), sep = " ") |>
tidyr::unite("month_tetrad", c("month", "tetrad"), remove = FALSE)
}

# check if specified location is present in data
check_location <- function(raw_dat, yard_location, call) {
dat_location <- raw_dat |> dplyr::filter(Location == yard_location)

if (dim(dat_location)[1] == 0) {
message_location <- stringr::str_glue(
"Location {yard_location} not found in eBird data"
)
rlang::abort(message_location, call = call)
}

return(dat_location)
}
2 changes: 1 addition & 1 deletion R/yardlistr_main.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ yardlistr <- function(location, dir_dat, dir_img) {
dat <- clean_ebird_data(file_data, location)

# inform user which location is being worked on
rlang::inform(stringr::str_glue("Analyzing data for ", location))
rlang::inform(stringr::str_glue("Analyzing data for {location}"))

# list of unique checklist
checklists <- dat |>
Expand Down

0 comments on commit 44e1d30

Please sign in to comment.