Skip to content

Commit

Permalink
Merge pull request #108 from nationalparkservice/sarah-dev
Browse files Browse the repository at this point in the history
add documentation to generate_ll_from_utm
  • Loading branch information
RobLBaker authored Jun 27, 2024
2 parents c879483 + 0c79fe8 commit 33ad267
Show file tree
Hide file tree
Showing 62 changed files with 13,623 additions and 3,730 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Imports:
tidyselect,
glue,
sp,
withr
withr,
cli,
purrr
RoxygenNote: 7.3.1
Suggests:
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(get_taxon_rank)
export(get_utm_zone)
export(long2UTM)
export(order_cols)
export(removeEmptyTables)
export(replace_blanks)
export(te_check)
export(utm_to_ll)
Expand Down
36 changes: 30 additions & 6 deletions R/geography.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ fuzz_location <- function(lat,
#' of your dataframe. Note that some parameter names are not in snake_case but
#' instead reflect DarwinCore naming conventions.
#'
#' This function uses tidy evaluation (i.e. you can provide column name arguments
#' as strings or you can leave them unquoted). If you wish to store column names
#' as strings in variables, you must enclose the variables in double curly braces
#' when you pass them into the function. See code examples below.
#'
#' @param df - The dataframe with UTM coordinates you would like to convert.
#' Input the name of your dataframe.
#' @param EastingCol - The name of your Easting UTM column. You may input the name
Expand All @@ -372,6 +377,7 @@ fuzz_location <- function(lat,
#' @examples
#' \dontrun{
#'
#' # Using magrittr pipe (%>%) and unquoted column names
#' my_dataframe %>%
#' generate_ll_from_utm(
#' EastingCol = UTM_X,
Expand All @@ -380,6 +386,7 @@ fuzz_location <- function(lat,
#' DatumCol = Datum
#' )
#'
#' # Providing column names as strings (in quotes)
#' generate_ll_from_utm(
#' df = mydataframe,
#' EastingCol = "EastingCoords",
Expand All @@ -388,6 +395,23 @@ fuzz_location <- function(lat,
#' DatumCol = "datum",
#' latlong_datum = "WGS84"
#' )
#'
#' # Column names stored as strings in separate variables
#' easting <- "EastingCoords"
#' northing <- "NorthingCoords"
#' zonecol <- "zone"
#' datumcol <- "datum"
#' latlong_dat <- "WGS84"
#'
#' generate_ll_from_utm(
#' df = mydataframe,
#' EastingCol = {{easting}}, # enclose variables that store column names in {{}}
#' NorthingCol = {{northing}},
#' ZoneCol = {{zonecol}},
#' DatumCol = {{datumcol}},
#' latlong_datum = latlong_dat # this isn't a column name so it doesn't need {{}}
#' )
#'
#' }
generate_ll_from_utm <- function(df,
EastingCol,
Expand All @@ -404,12 +428,12 @@ generate_ll_from_utm <- function(df,
dplyr::select(`_UTMJOINCOL`, {{EastingCol}}, {{NorthingCol}}, {{ZoneCol}}, {{DatumCol}})

withr::with_envvar(c("PROJ_LIB" = ""), { # This is a fix for the proj library bug in R (see pinned post "sf::st_read() of geojson not getting CRS" in IMData General Discussion).
# filter out rows that are missing UTM, zone, or datum
coord_df <- coord_df %>%
dplyr::filter(!is.na({{EastingCol}}) &
!is.na({{NorthingCol}}) &
!is.na({{ZoneCol}}) &
!is.na({{DatumCol}}))
coord_df <- coord_df %>%
dplyr::filter(!is.na(!!rlang::ensym(EastingCol)) &
!is.na(!!rlang::ensym(NorthingCol)) &
!is.na(!!rlang::ensym(ZoneCol)) &
!is.na(!!rlang::ensym(DatumCol)))


na_row_count <- nrow(df) - nrow(coord_df)
if (na_row_count > 0) {
Expand Down
26 changes: 26 additions & 0 deletions R/remove_empty_tables.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Remove empty tables from a list
#'
#' @param df_list A list of tibbles or dataframes.
#'
#' @return The same list but with empty tables removed.
#' @export
#'
#' @examples
#'
#' test_list <- list(item_a = tibble::tibble,
#' item_b = mtcars,
#' item_c = iris)
#'
#' tidy_list <- removeEmptyTables(test_list)
#'
removeEmptyTables <- function(df_list) {
non_empty_list <- purrr::compact(df_list) # Remove empty dataframes
tables_removed <- setdiff(names(df_list), names(non_empty_list)) # Get names of removed dataframes

# Warn about removed empty dataframes
if (length(tables_removed) > 0) {
warning(cli::pluralize("{length(tables_removed)} empty table{?s} {?was/were} omitted from dataset. Affected table{?s}: {paste(tables_removed, collapse = ", ")}"))
}

return(non_empty_list)
}
125 changes: 50 additions & 75 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 33ad267

Please sign in to comment.