diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml new file mode 100644 index 0000000..23f3c55 --- /dev/null +++ b/.github/workflows/coverage.yaml @@ -0,0 +1,75 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [develop] + pull_request: + branches: [develop] + +name: test-coverage + +permissions: + contents: write + checks: write + pull-requests: write + +jobs: + test-coverage: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::covr, any::xml2 + needs: coverage + + - name: Test coverage + run: | + cov <- covr::package_coverage( + quiet = FALSE, + clean = FALSE, + install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") + ) + print(cov) + covr::to_cobertura(cov) + shell: Rscript {0} + + - name: Show testthat output + if: always() + run: | + ## -------------------------------------------------------------------- + find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true + shell: bash + + - name: Upload test results + if: failure() + uses: actions/upload-artifact@v4 + with: + name: coverage-test-failures + path: ${{ runner.temp }}/package + if-no-files-found: ignore # Suppress warnings if no files are found + + - name: Upload coverage report + uses: actions/upload-artifact@v4 + with: + name: coverage-report + path: ./cobertura.xml + + - name: Comment coverage + uses: 5monkeys/cobertura-action@master + with: + report_name: coverage-report + path: ./cobertura.xml + minimum_coverage: 10 + show_missing: true + link_missing_lines: true + + diff --git a/DESCRIPTION b/DESCRIPTION index 82e4a72..72a92be 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: StreamCatTools Type: Package Title: 'StreamCatTools' -Version: 0.7.0 +Version: 0.8.0 Authors@R: c(person(given = "Marc", family = "Weber", role = c("aut", "cre"), @@ -22,6 +22,10 @@ Authors@R: c(person(given = "Marc", family = "Rebhuhn", role = "ctb", email = "rebhuhnd@gmail.com"), + person(given = "Michael", + family = "Dumelle", + role = "ctb", + email = "dumelle.michael@epa.gov"), person(given = "Zachary", family = "Smith", role = "ctb")) @@ -55,6 +59,6 @@ URL: https://usepa.github.io/StreamCatTools/, https://github.com/USEPA/StreamCat BugReports: https://github.com/USEPA/StreamCatTools/issues VignetteBuilder: knitr,rmarkdown LazyData: true -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 License: CC0 NeedsCompilation: no diff --git a/NAMESPACE b/NAMESPACE index 495d937..4ac1597 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,10 +3,12 @@ export(lc_fullname) export(lc_get_comid) export(lc_get_data) +export(lc_get_metric_names) export(lc_get_params) export(lc_nlcd) export(sc_fullname) export(sc_get_comid) export(sc_get_data) +export(sc_get_metric_names) export(sc_get_params) export(sc_nlcd) diff --git a/NEWS.md b/NEWS.md index 1eae8d8..3cb0c5e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# StreamCatTools 0.8.0 + +- Added code coverage to the package +- Added two new functions - `sc_get_metric_names` and `lc_get_metric_names` + to make metric names and descriptions more searchable and slightly updated + parameters in both `sc_get_params` and `lc_get_params` functions +- Both `sc_get_data` and `lc_get_data` now accept vectors as well as comma- + separated strings for the metric, state, county and region parameters + # StreamCatTools 0.7.0 - Updated both `sc_get_data` and `lc_get_data` to pass parameters in diff --git a/R/lc_get_comid.R b/R/lc_get_comid.R index 087ca56..c91331d 100644 --- a/R/lc_get_comid.R +++ b/R/lc_get_comid.R @@ -27,7 +27,7 @@ #' @return A new sf data frame with a populated 'COMID' column #' #' @examples -#' \donttest{ +#' \dontrun{ #' #' dd <- data.frame(x = c(-89.198,-114.125,-122.044), #' y = c(45.502,47.877,43.730)) diff --git a/R/lc_get_data.R b/R/lc_get_data.R index 4bd84a7..3e82bf1 100644 --- a/R/lc_get_data.R +++ b/R/lc_get_data.R @@ -54,7 +54,7 @@ #' @return A tibble of desired StreamCat metrics. If data are missing for all rows of a given metric, then the column for that metric will not exist. If data are missing for only some rows, then they will be specified with NA. #' #' @examples -#' \donttest{ +#' \dontrun{ #' df <- lc_get_data(comid='23794487', aoi='cat', metric='fert') #' #' df <- lc_get_data(metric='pcturbmd2006', aoi='ws', @@ -94,17 +94,29 @@ lc_get_data <- function(comid = NULL, # Base API URL. req <- httr2::request('https://api.epa.gov/StreamCat/lakes/metrics') # Collapse comids into a single string separated by a comma. + if ((is.null(comid) & is.null(state) & is.null(county) & is.null(region) & is.null(conus)) | is.null(metric) | is.null(aoi)){ + stop('Must provide at a minimum valid comid, metric and aoi to the function') + } + # Collapse vectors into a single string separated by a comma. if (!is.null(comid)){ comid <- paste(comid, collapse = ",") } + metric <- paste(metric, collapse = ",") + aoi <- paste(aoi, collapse = ",") + if (!is.null(state)){ + state <- paste(state, collapse = ",") + } + if (!is.null(county)){ + county <- paste(county, collapse = ",") + } + if (!is.null(region)){ + region <- paste(region, collapse = ",") + } # Force old and odd naming convention to behave correctly if (!is.null(aoi)){ if (aoi == 'catchment') aoi <- 'cat' if (aoi == 'watershed') aoi <- 'ws' } - if ((is.null(comid) & is.null(state) & is.null(county) & is.null(region) & is.null(conus)) | is.null(metric) | is.null(aoi)){ - stop('Must provide at a minimum valid comid, metric and aoi to the function') - } if (!is.null(conus) & metric=='all'){ stop('If you are requesting all metrics please request for regions, states or counties rather than all of conus') } @@ -178,7 +190,7 @@ lc_get_data <- function(comid = NULL, #' @return A tibble of desired StreamCat metrics #' #' @examples -#' \donttest{ +#' \dontrun{ #' #' df <- lc_nlcd(comid='23783629', year='2019', aoi='ws') #' diff --git a/R/lc_get_params.R b/R/lc_get_params.R index bd3b69d..8fd05f8 100644 --- a/R/lc_get_params.R +++ b/R/lc_get_params.R @@ -8,7 +8,7 @@ #' #' @param param List of available parameters in the API for the following options: #' name, areaofInterest, region, state, county. State and county return a data -#' frame that includes FIPS codes, names and state abbreviations +#' frame that includes FIPS codes, names and state abbreviations #' Syntax: param=, #' Values: name|area #' @@ -16,12 +16,14 @@ #' @export #' #' @examples -#' \donttest{ +#' \dontrun{ #' params <- lc_get_params(param='variable_info') #' params <- lc_get_params(param='metric_names') -#' params <- lc_get_params(param='areaOfInterest') +#' params <- sc_get_params(param='categories') +#' params <- lc_get_params(param='aoi') #' params <- lc_get_params(param='state') #' params <- lc_get_params(param='county') +#' params <- sc_get_params(param='datasets') #' } lc_get_params <- function(param = NULL) { @@ -29,8 +31,9 @@ lc_get_params <- function(param = NULL) { INDICATOR_CATEGORY <- METRIC_NAME <- AOI <- YEAR <- NULL WEBTOOL_NAME <- METRIC_UNITS <- METRIC_DESCRIPTION <- DSID <- NULL SOURCE_NAME <- SOURCE_URL <- UUID <- DATE_DOWNLOADED <- NULL + DSNAME <- NULL resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/metrics")$items - if (param=='areaOfInterest'){ + if (param=='aoi'){ params <- strsplit(stringr::str_sub(resp$aoi_param_info[[1]]$options,2,-2),",")[[1]] params <- c(gsub(" ","", params),'other') params <- params[order(params)] @@ -40,16 +43,30 @@ lc_get_params <- function(param = NULL) { params <- params[!duplicated(params)] params <- params[order(params)] } else if(param == 'variable_info') { - params <- httr2::request('https://api.epa.gov/StreamCat/streams/variable_info') |> + params <- httr2::request('https://api.epa.gov/StreamCat/lakes/variable_info') |> httr2::req_perform() |> httr2::resp_body_string() |> - readr::read_csv(show_col_types = FALSE) |> - dplyr::select(-UUID,-DATE_DOWNLOADED,-METADATA) |> - dplyr::rename(dataset=FINAL_TABLE,category=INDICATOR_CATEGORY, - metric=METRIC_NAME,aoi=AOI, year=YEAR, + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(-UUID,-DATE_DOWNLOADED,-METADATA) |> + dplyr::rename(dataset=FINAL_TABLE,category=INDICATOR_CATEGORY, + metric=METRIC_NAME,aoi=AOI, year=YEAR, short_description=WEBTOOL_NAME,units=METRIC_UNITS, long_description=METRIC_DESCRIPTION, dsid=DSID, source_name=SOURCE_NAME, source_URL=SOURCE_URL) + } else if(param == 'categories'){ + params <- httr2::request('https://api.epa.gov/StreamCat/lakes/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(INDICATOR_CATEGORY) + params <- sort(unique(params$INDICATOR_CATEGORY)) + } else if(param == 'datasets'){ + params <- httr2::request('https://api.epa.gov/StreamCat/lakes/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(DSNAME) + params <- sort(unique(params$DSNAME[!is.na(params$DSNAME)])) } else if(param == 'region'){ params <- resp$region_options[[1]][[1]] params <- params[order(params)] @@ -93,3 +110,85 @@ lc_fullname <- function(metric = NULL) { result <- unique(resp$short_display_name[resp$metric_prefix %in% metric]) return(result) } + +#' Get LakeCat Metric Names +#' +#' @description +#' Function to filter LakeCat metrics metrics by category, area of interest, +#' dataset or year. Use `lc_get_params(categories)` or `lc_get_params(datasets)` +#' to see all the valid category or dataset options +#' +#' @author +#' Marc Weber +#' +#' @param category Filter LakeCat metrics based on the metric category +#' @param aoi Filter LakeCat metrics based on the area of interest +#' @param year Filter LakeCat metrics based on a particular year or years +#' @param dataset Filter LakeCat metrics based on the dataset name +#' +#' @return A dataframe of merics and description that match filter criteria +# #' @importFrom rlang .data +#' @export +#' +#' @examples +#' \dontrun{ +#' metrics <- lc_get_metric_names(category='Natural') +#' metrics <- lc_get_metric_names(category = c('Anthropogenic','Natural'), +#' aoi=c('Cat','Ws')} + + +lc_get_metric_names <- function(category = NULL, + aoi = NULL, + year = NULL, + dataset = NULL) { + if (!is.null(aoi)){ + if (any(stringr::str_detect(aoi,'catchment'))) { + aoi <- gsub('catchment','Cat',aoi) + } + if (any(stringr::str_detect(aoi,'watershed'))) { + aoi <- gsub('watershed','Ws',aoi) + } + if (any(stringr::str_detect(aoi,'riparian_catchment'))) { + aoi <- gsub('riparian_catchment','CatRp100',aoi) + } + if (any(stringr::str_detect(aoi,'riparian_watershed'))) { + aoi <- gsub('riparian_watershed','WsRp100',aoi) + } + aoi <- stringr::str_to_title(aoi) + } + resp <- params <- httr2::request('https://api.epa.gov/StreamCat/lakes/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) + + filters <- list(INDICATOR_CATEGORY = category, AOI = aoi, YEAR = year, + DSNAME = dataset) + + filter_data <- function(data, filters) { + temp_col <- col_name <- NULL + # Filter the data frame for each non-null filter + filtered_data <- purrr::reduce( + names(filters), + .init = data, + .f = function(df, col_name) { + filter_values <- filters[[col_name]] + if (!is.null(filter_values)) { + temp_col <- stringr::str_split(df[[col_name]], ",") + df <- df[purrr::map_lgl(temp_col, ~ any(.x %in% filter_values)), , drop = FALSE] + } + df + } + ) + return(filtered_data) + } + results <- filter_data(resp, filters) + names_keep <- c("INDICATOR_CATEGORY", "METRIC_NAME", "AOI", "YEAR", + "WEBTOOL_NAME", "METRIC_DESCRIPTION", + "METRIC_UNITS", "SOURCE_NAME", "DSNAME") + results <- results[, names_keep, drop = FALSE] + names_new <- c("Category", "Metric", "AOI", "Year", "Short_Name", + "Metric_Description", "Units", "Source", "Dataset") + names(results) <- names_new + + return(results) +} diff --git a/R/sc_get_comid.R b/R/sc_get_comid.R index 073ae6a..0a22c9a 100644 --- a/R/sc_get_comid.R +++ b/R/sc_get_comid.R @@ -24,7 +24,7 @@ #' @return A new sf data frame with a populated 'COMID' column #' #' @examples -#' \donttest{ +#' \dontrun{ #' #' dd <- data.frame(x = c(-122.649,-100.348,-75.186,-106.675), #' y = c(45.085, 35.405,42.403,38.721)) @@ -32,7 +32,8 @@ #' comids <- sc_get_comid(dd, xcoord='x', #' ycoord='y', crsys=4269) #' -#' dd <- sf::read_sf(system.file("shape/nc.shp", package="sf")) +#' dd <- sf::st_point_on_surface(sf::read_sf(system.file("shape/nc.shp", package="sf"))) +#' #' comids <- sc_get_comid(dd) #' #' comids <- sc_get_comid(dd, xcoord='x', @@ -53,10 +54,10 @@ sc_get_comid <- function(dd = NULL, xcoord = NULL, } else { dd <- sf::st_as_sf(dd, coords = c(xcoord, ycoord), crs = crsys, remove = FALSE) } - + geom_col <- attr(dd, "sf_column") run_for <- 1:nrow(dd) output <- do.call(rbind, lapply(1:nrow(dd), function(i){ - comid <- nhdplusTools::discover_nhdplus_id(dd[i,c('geometry')]) + comid <- nhdplusTools::discover_nhdplus_id(dd[i,c(geom_col)]) if (length(comid)==0L) comid <- NA else comid <- comid return(comid) })) diff --git a/R/sc_get_data.R b/R/sc_get_data.R index dfb0495..51a83f1 100644 --- a/R/sc_get_data.R +++ b/R/sc_get_data.R @@ -59,7 +59,7 @@ #' @return A data frame of StreamCat metrics. If data are missing for all rows of a given metric, then the column for that metric will not exist. If data are missing for only some rows, then they will be specified with NA. #' #' @examples -#' \donttest{ +#' \dontrun{ #' df <- sc_get_data(comid='179', aoi='cat', metric='fert') #' #' df <- sc_get_data(metric='pctgrs2006', aoi='ws', region='Region01') @@ -100,10 +100,24 @@ sc_get_data <- function(comid = NULL, # Base API URL. base_url <- 'https://api.epa.gov/StreamCat/streams/metrics' req <- httr2::request(base_url) - # Collapse comids into a single string separated by a comma. + if ((is.null(comid) & is.null(state) & is.null(county) & is.null(region) & is.null(conus)) | is.null(metric) | is.null(aoi)){ + stop('Must provide at a minimum valid comid, metric and aoi to the function') + } + # Collapse vectors into a single string separated by a comma. if (!is.null(comid)){ comid <- paste(comid, collapse = ",") } + metric <- paste(metric, collapse = ",") + aoi <- paste(aoi, collapse = ",") + if (!is.null(state)){ + state <- paste(state, collapse = ",") + } + if (!is.null(county)){ + county <- paste(county, collapse = ",") + } + if (!is.null(region)){ + region <- paste(region, collapse = ",") + } # Force old and odd naming convention to behave correctly if (!is.null(aoi)){ if (stringr::str_detect(aoi,'catchment')) { @@ -119,9 +133,7 @@ sc_get_data <- function(comid = NULL, aoi <- gsub('riparian_watershed','wsrp100',aoi) } } - if ((is.null(comid) & is.null(state) & is.null(county) & is.null(region) & is.null(conus)) | is.null(metric) | is.null(aoi)){ - stop('Must provide at a minimum valid comid, metric and aoi to the function') - } + if (!is.null(conus) & metric=='all'){ stop('If you are requesting all metrics please request for regions, states or counties rather than all of conus') } @@ -210,7 +222,7 @@ sc_get_data <- function(comid = NULL, #' @return A tibble of desired StreamCat metrics #' #' @examples -#' \donttest{ +#' \dontrun{ #' df <- sc_nlcd(year='2001', aoi='cat',comid='179,1337,1337420') #' #' df <- sc_nlcd(year='2001', aoi='ws', region='Region01') diff --git a/R/sc_get_params.R b/R/sc_get_params.R index 19d7bd2..4d024fa 100644 --- a/R/sc_get_params.R +++ b/R/sc_get_params.R @@ -8,7 +8,7 @@ #' #' @param param List of available parameters in the API for the following options: #' name, areaofInterest, region, state, county. State and county return a data -#' frame that includes FIPS codes, names and state abbreviations +#' frame that includes FIPS codes, names and state abbreviations #' Syntax: param=, #' Values: name|area #' @@ -16,20 +16,23 @@ #' @export #' #' @examples -#' \donttest{ +#' \dontrun{ #' params <- sc_get_params(param='variable_info') #' params <- sc_get_params(param='metric_names') -#' params <- sc_get_params(param='areaOfInterest') +#' params <- sc_get_params(param='categories') +#' params <- sc_get_params(param='aoi') #' params <- sc_get_params(param='state') #' params <- sc_get_params(param='county') -#' } +#' params <- sc_get_params(param='datasets')} + sc_get_params <- function(param = NULL) { UUID <- DATE_DOWNLOADED <- METADATA <- FINAL_TABLE<- NULL INDICATOR_CATEGORY <- METRIC_NAME <- AOI <- YEAR <- NULL WEBTOOL_NAME <- METRIC_UNITS <- METRIC_DESCRIPTION <- DSID <- NULL SOURCE_NAME <- SOURCE_URL <- UUID <- DATE_DOWNLOADED <- NULL + DSNAME <- NULL resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/streams/metrics")$items - if (param=='areaOfInterest'){ + if (param=='aoi'){ params <- strsplit(stringr::str_sub(resp$aoi_param_info[[1]]$options,2,-2),",")[[1]] params <- c(gsub(" ","", params),'other') params <- params[order(params)] @@ -41,14 +44,29 @@ sc_get_params <- function(param = NULL) { params <- httr2::request('https://api.epa.gov/StreamCat/streams/variable_info') |> httr2::req_perform() |> httr2::resp_body_string() |> - readr::read_csv(show_col_types = FALSE) |> - dplyr::select(-UUID,-DATE_DOWNLOADED,-METADATA) |> - dplyr::rename(dataset=FINAL_TABLE,category=INDICATOR_CATEGORY, - metric=METRIC_NAME,aoi=AOI, year=YEAR, + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(-UUID,-DATE_DOWNLOADED,-METADATA) |> + dplyr::rename(dataset=FINAL_TABLE,category=INDICATOR_CATEGORY, + metric=METRIC_NAME,aoi=AOI, year=YEAR, short_description=WEBTOOL_NAME,units=METRIC_UNITS, long_description=METRIC_DESCRIPTION, dsid=DSID, source_name=SOURCE_NAME, source_URL=SOURCE_URL) - } else if(param == 'region'){ + } else if(param == 'categories'){ + params <- httr2::request('https://api.epa.gov/StreamCat/streams/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(INDICATOR_CATEGORY) + params <- sort(unique(params$INDICATOR_CATEGORY)) + } else if(param == 'datasets'){ + params <- httr2::request('https://api.epa.gov/StreamCat/streams/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) |> + dplyr::select(DSNAME) + params <- sort(unique(params$DSNAME[!is.na(params$DSNAME)])) + } + else if(param == 'region'){ params <- resp$region_options[[1]][[1]] params <- params[order(params)] } else if(param == 'state'){ @@ -91,3 +109,89 @@ sc_fullname <- function(metric = NULL) { result <- unique(resp$short_display_name[resp$metric_prefix %in% metric]) return(result) } + + +#' Get StreamCat Metric Names +#' +#' @description +#' Function to filter StreamCat metrics metrics by category, area of interest, +#' dataset or year. Use `sc_get_params(categories)` or `sc_get_params(datasets)` +#' to see all the valid category or dataset options +#' +#' @author +#' Marc Weber +#' +#' @param category Filter StreamCat metrics based on the metric category +#' @param aoi Filter StreamCat metrics based on the area of interest +#' @param year Filter StreamCat metrics based on a particular year or years +#' @param dataset Filter StreamCat metrics based on the dataset name +#' +#' @return A dataframe of merics and description that match filter criteria +# #' @importFrom rlang .data +#' @export +#' +#' @examples +#' \dontrun{ +#' metrics <- sc_get_metric_names(category='Wildfire') +#' metrics <- sc_get_metric_names(category = c('Deposition','Climate'), +#' aoi=c('Cat','Ws') +#' metrics <- sc_get_metric_names(aoi='Other', +#' dataset=c('Canal Density','Predicted Channel Widths Depths')) +#' +#' } + + +sc_get_metric_names <- function(category = NULL, + aoi = NULL, + year = NULL, + dataset = NULL) { + if (!is.null(aoi)){ + if (any(stringr::str_detect(aoi,'catchment'))) { + aoi <- gsub('catchment','Cat',aoi) + } + if (any(stringr::str_detect(aoi,'watershed'))) { + aoi <- gsub('watershed','Ws',aoi) + } + if (any(stringr::str_detect(aoi,'riparian_catchment'))) { + aoi <- gsub('riparian_catchment','CatRp100',aoi) + } + if (any(stringr::str_detect(aoi,'riparian_watershed'))) { + aoi <- gsub('riparian_watershed','WsRp100',aoi) + } + aoi <- stringr::str_to_title(aoi) + } + resp <- params <- httr2::request('https://api.epa.gov/StreamCat/streams/variable_info') |> + httr2::req_perform() |> + httr2::resp_body_string() |> + readr::read_csv(show_col_types = FALSE) + + filters <- list(INDICATOR_CATEGORY = category, AOI = aoi, YEAR = year, + DSNAME = dataset) + + filter_data <- function(data, filters) { + # Filter the data frame for each non-null filter + filtered_data <- purrr::reduce( + names(filters), + .init = data, + .f = function(df, col_name) { + filter_values <- filters[[col_name]] + if (!is.null(filter_values)) { + temp_col <- stringr::str_split(df[[col_name]], ",") + df <- df[purrr::map_lgl(temp_col, ~ any(.x %in% filter_values)), , drop = FALSE] + } + df + } + ) + return(filtered_data) + } + results <- filter_data(resp, filters) + names_keep <- c("INDICATOR_CATEGORY", "METRIC_NAME", "AOI", "YEAR", + "WEBTOOL_NAME", "METRIC_DESCRIPTION", + "METRIC_UNITS", "SOURCE_NAME", "DSNAME") + results <- results[, names_keep, drop = FALSE] + names_new <- c("Category", "Metric", "AOI", "Year", "Short_Name", + "Metric_Description", "Units", "Source", "Dataset") + names(results) <- names_new + + return(results) +} diff --git a/README.md b/README.md index 52d8f77..373894c 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ [![cran checks](https://badges.cranchecks.info/worst/StreamCatTools.svg)](https://cran.r-project.org/web/checks/check_results_StreamCatTools.html) [![R-CMD-check](https://github.com/USEPA/StreamCatTools/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/USEPA/StreamCatTools/actions/workflows/R-CMD-check.yaml) [![Downloads](https://cranlogs.r-pkg.org/badges/StreamCatTools)](https://cran.r-project.org/package=StreamCatTools) +[![CodeCov](https://img.shields.io/badge/test%20coverage-86.9%25-388600.svg)](https://img.shields.io/badge/test%20coverage-86.9%25-388600.svg) ## StreamCatTools: Tools to work with the [StreamCat](https://www.epa.gov/national-aquatic-resource-surveys/streamcat-dataset) API within R and access the full suite of StreamCat and [LakeCat](https://www.epa.gov/national-aquatic-resource-surveys/lakecat-dataset) metrics. diff --git a/man/lc_get_comid.Rd b/man/lc_get_comid.Rd index 65fa21b..d56c481 100644 --- a/man/lc_get_comid.Rd +++ b/man/lc_get_comid.Rd @@ -38,7 +38,7 @@ sf object. The function generates a vector of NHDPlus Waterbody COMID values a user can then pass to lc_get_data function } \examples{ -\donttest{ +\dontrun{ dd <- data.frame(x = c(-89.198,-114.125,-122.044), y = c(45.502,47.877,43.730)) diff --git a/man/lc_get_data.Rd b/man/lc_get_data.Rd index 79db8dd..db08c5e 100644 --- a/man/lc_get_data.Rd +++ b/man/lc_get_data.Rd @@ -70,7 +70,7 @@ Function to return LakeCat metrics using the StreamCat API. The function allows specific metric data aggregated by area of interest, returned by comid(s), hydroregion(s), state(s), or county(ies). } \examples{ -\donttest{ +\dontrun{ df <- lc_get_data(comid='23794487', aoi='cat', metric='fert') df <- lc_get_data(metric='pcturbmd2006', aoi='ws', diff --git a/man/lc_get_metric_names.Rd b/man/lc_get_metric_names.Rd new file mode 100644 index 0000000..523ee82 --- /dev/null +++ b/man/lc_get_metric_names.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lc_get_params.R +\name{lc_get_metric_names} +\alias{lc_get_metric_names} +\title{Get LakeCat Metric Names} +\usage{ +lc_get_metric_names(category = NULL, aoi = NULL, year = NULL, dataset = NULL) +} +\arguments{ +\item{category}{Filter LakeCat metrics based on the metric category} + +\item{aoi}{Filter LakeCat metrics based on the area of interest} + +\item{year}{Filter LakeCat metrics based on a particular year or years} + +\item{dataset}{Filter LakeCat metrics based on the dataset name} +} +\value{ +A dataframe of merics and description that match filter criteria +} +\description{ +Function to filter LakeCat metrics metrics by category, area of interest, +dataset or year. Use `lc_get_params(categories)` or `lc_get_params(datasets)` +to see all the valid category or dataset options +} +\examples{ +\dontrun{ +metrics <- lc_get_metric_names(category='Natural') +metrics <- lc_get_metric_names(category = c('Anthropogenic','Natural'), +aoi=c('Cat','Ws')} +} +\author{ +Marc Weber +} diff --git a/man/lc_get_params.Rd b/man/lc_get_params.Rd index 168275a..3195dc0 100644 --- a/man/lc_get_params.Rd +++ b/man/lc_get_params.Rd @@ -9,7 +9,7 @@ lc_get_params(param = NULL) \arguments{ \item{param}{List of available parameters in the API for the following options: name, areaofInterest, region, state, county. State and county return a data -frame that includes FIPS codes, names and state abbreviations +frame that includes FIPS codes, names and state abbreviations Syntax: param=, Values: name|area} } @@ -20,12 +20,14 @@ A list of all the current LakeCat values for a given parameter Function to return available LakeCat parameters using the StreamCat API. } \examples{ -\donttest{ +\dontrun{ params <- lc_get_params(param='variable_info') params <- lc_get_params(param='metric_names') -params <- lc_get_params(param='areaOfInterest') +params <- sc_get_params(param='categories') +params <- lc_get_params(param='aoi') params <- lc_get_params(param='state') params <- lc_get_params(param='county') +params <- sc_get_params(param='datasets') } } \author{ diff --git a/man/lc_nlcd.Rd b/man/lc_nlcd.Rd index 7beef4c..512be94 100644 --- a/man/lc_nlcd.Rd +++ b/man/lc_nlcd.Rd @@ -45,7 +45,7 @@ A tibble of desired StreamCat metrics Function to specifically retrieve all NLCD metrics for a given year using the StreamCat API. } \examples{ -\donttest{ +\dontrun{ df <- lc_nlcd(comid='23783629', year='2019', aoi='ws') diff --git a/man/sc_get_comid.Rd b/man/sc_get_comid.Rd index bc03c0e..db36764 100644 --- a/man/sc_get_comid.Rd +++ b/man/sc_get_comid.Rd @@ -29,7 +29,7 @@ sf object. The function generates a vector of COMID values a user can then pass to sc_get_data function } \examples{ -\donttest{ +\dontrun{ dd <- data.frame(x = c(-122.649,-100.348,-75.186,-106.675), y = c(45.085, 35.405,42.403,38.721)) @@ -37,7 +37,8 @@ y = c(45.085, 35.405,42.403,38.721)) comids <- sc_get_comid(dd, xcoord='x', ycoord='y', crsys=4269) -dd <- sf::read_sf(system.file("shape/nc.shp", package="sf")) +dd <- sf::st_point_on_surface(sf::read_sf(system.file("shape/nc.shp", package="sf"))) + comids <- sc_get_comid(dd) comids <- sc_get_comid(dd, xcoord='x', diff --git a/man/sc_get_data.Rd b/man/sc_get_data.Rd index 8022e01..09bccc0 100644 --- a/man/sc_get_data.Rd +++ b/man/sc_get_data.Rd @@ -75,7 +75,7 @@ Function to return StreamCat catchment and watershed metrics using the StreamCat specific metric data aggregated by area of interest, returned by comid(s), hydroregion(s), state(s), or county(ies). } \examples{ -\donttest{ +\dontrun{ df <- sc_get_data(comid='179', aoi='cat', metric='fert') df <- sc_get_data(metric='pctgrs2006', aoi='ws', region='Region01') diff --git a/man/sc_get_metric_names.Rd b/man/sc_get_metric_names.Rd new file mode 100644 index 0000000..1f11eb1 --- /dev/null +++ b/man/sc_get_metric_names.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sc_get_params.R +\name{sc_get_metric_names} +\alias{sc_get_metric_names} +\title{Get StreamCat Metric Names} +\usage{ +sc_get_metric_names(category = NULL, aoi = NULL, year = NULL, dataset = NULL) +} +\arguments{ +\item{category}{Filter StreamCat metrics based on the metric category} + +\item{aoi}{Filter StreamCat metrics based on the area of interest} + +\item{year}{Filter StreamCat metrics based on a particular year or years} + +\item{dataset}{Filter StreamCat metrics based on the dataset name} +} +\value{ +A dataframe of merics and description that match filter criteria +} +\description{ +Function to filter StreamCat metrics metrics by category, area of interest, +dataset or year. Use `sc_get_params(categories)` or `sc_get_params(datasets)` +to see all the valid category or dataset options +} +\examples{ +\dontrun{ +metrics <- sc_get_metric_names(category='Wildfire') +metrics <- sc_get_metric_names(category = c('Deposition','Climate'), +aoi=c('Cat','Ws') +metrics <- sc_get_metric_names(aoi='Other', +dataset=c('Canal Density','Predicted Channel Widths Depths')) + +} +} +\author{ +Marc Weber +} diff --git a/man/sc_get_params.Rd b/man/sc_get_params.Rd index 47a721f..bbb59a5 100644 --- a/man/sc_get_params.Rd +++ b/man/sc_get_params.Rd @@ -9,7 +9,7 @@ sc_get_params(param = NULL) \arguments{ \item{param}{List of available parameters in the API for the following options: name, areaofInterest, region, state, county. State and county return a data -frame that includes FIPS codes, names and state abbreviations +frame that includes FIPS codes, names and state abbreviations Syntax: param=, Values: name|area} } @@ -20,13 +20,14 @@ A list of all the current StreamCat values for a given parameter Function to return available StreamCat parameters using the StreamCat API. } \examples{ -\donttest{ +\dontrun{ params <- sc_get_params(param='variable_info') params <- sc_get_params(param='metric_names') -params <- sc_get_params(param='areaOfInterest') +params <- sc_get_params(param='categories') +params <- sc_get_params(param='aoi') params <- sc_get_params(param='state') params <- sc_get_params(param='county') -} +params <- sc_get_params(param='datasets')} } \author{ Marc Weber diff --git a/man/sc_nlcd.Rd b/man/sc_nlcd.Rd index 2db3bd0..6cd7d2a 100644 --- a/man/sc_nlcd.Rd +++ b/man/sc_nlcd.Rd @@ -64,7 +64,7 @@ A tibble of desired StreamCat metrics Function to retrieve all NLCD metrics for a given year using the StreamCat API. } \examples{ -\donttest{ +\dontrun{ df <- sc_nlcd(year='2001', aoi='cat',comid='179,1337,1337420') df <- sc_nlcd(year='2001', aoi='ws', region='Region01') diff --git a/tests/testthat/test-lc_get_params.R b/tests/testthat/test-lc_get_params.R index 20d2224..47d62d5 100644 --- a/tests/testthat/test-lc_get_params.R +++ b/tests/testthat/test-lc_get_params.R @@ -6,25 +6,25 @@ test_that("lc_get_params for metric_names parameters", { expect_gt(length(params),500) }) -test_that("sc_get_params for variable_info parameters", { - params <- sc_get_params(param='variable_info') +test_that("lc_get_params for variable_info parameters", { + params <- lc_get_params(param='variable_info') expect_true(exists("params")) expect_gt(nrow(params),100) }) -test_that("lc_get_params for area of interest parameters", { - params <- lc_get_params(param='areaOfInterest') - expect_true(exists("params")) - expect_equal(params,c("cat","ws")) +test_that("lc_get_metric_names", { + metrics <- lc_get_metric_names(category='Natural') + expect_true(exists("metrics")) + expect_gt(nrow(metrics),70) + expect_equal(names(metrics), c("Category","Metric","AOI","Year", + "Short_Name","Metric_Description","Units", + "Source","Dataset")) }) -test_that("lc_get_params for region parameters", { - params <- sc_get_params(param='region') +test_that("lc_get_params for area of interest parameters", { + params <- lc_get_params(param='aoi') expect_true(exists("params")) - expect_equal(params,c("Region01","Region02","Region03N","Region03S","Region03W", - "Region04","Region05","Region06","Region07","Region08", - "Region09","Region10L","Region10U","Region11","Region12", - "Region13","Region14","Region15","Region16","Region17","Region18")) + expect_equal(params,c("cat","ws")) }) test_that("lc_get_params for name parameters", { diff --git a/tests/testthat/test-sc_get_params.R b/tests/testthat/test-sc_get_params.R index 4488b44..75ab546 100644 --- a/tests/testthat/test-sc_get_params.R +++ b/tests/testthat/test-sc_get_params.R @@ -2,7 +2,7 @@ context("Test that sc_get_params is pulling in StreamCat API parameters") test_that("sc_get_params for area of interest parameters", { - params <- sc_get_params(param='areaOfInterest') + params <- sc_get_params(param='aoi') expect_true(exists("params")) expect_equal(params,c("cat","catrp100","other","ws","wsrp100")) }) @@ -28,6 +28,18 @@ test_that("sc_get_params for variable_info parameters", { expect_gt(nrow(params),100) }) +test_that("sc_get_params for StreamCat metric categories", { + params <- sc_get_params(param='categories') + expect_true(exists("params")) + expect_gt(length(params),8) +}) + +test_that("sc_get_params for StreamCat datasets", { + params <- sc_get_params(param='datasets') + expect_true(exists("params")) + expect_gt(length(params),30) +}) + test_that("sc_get_params for state parameters", { params <- sc_get_params(param='state') expect_true(exists("params")) @@ -42,3 +54,22 @@ test_that("sc_get_params for county parameters", { expect_equal(names(params), c("fips","state","county_name")) }) +test_that("sc_get_metric_names for categories and AOIs", { + metrics <- sc_get_metric_names(category = c('Deposition','Climate'),aoi=c('Cat','Ws')) + expect_true(exists("metrics")) + expect_gt(nrow(metrics),10) + expect_equal(names(metrics), c("Category","Metric","AOI","Year", + "Short_Name","Metric_Description","Units", + "Source","Dataset")) +}) + +test_that("sc_get_metric_names for datasets", { + metrics <- sc_get_metric_names(aoi='Other', + dataset=c('Canal Density', + 'Predicted Channel Widths Depths')) + expect_true(exists("metrics")) + expect_lt(nrow(metrics),4) + expect_equal(names(metrics), c("Category","Metric","AOI","Year", + "Short_Name","Metric_Description","Units", + "Source","Dataset")) +}) \ No newline at end of file diff --git a/vignettes/Introduction.Rmd b/vignettes/Introduction.Rmd index 53c4aa3..37922a3 100644 --- a/vignettes/Introduction.Rmd +++ b/vignettes/Introduction.Rmd @@ -51,7 +51,7 @@ res$items ## List API parameters List StreamCat parameters: Get a list of available StreamCat values for certain parameters using the `sc_get_params` function via the API ```{r params} -region_params <- sc_get_params(param='areaOfInterest') +region_params <- sc_get_params(param='aoi') name_params <- sc_get_params(param='metric_names') diff --git a/vignettes/LakeCat.Rmd b/vignettes/LakeCat.Rmd index 382dd19..166e829 100644 --- a/vignettes/LakeCat.Rmd +++ b/vignettes/LakeCat.Rmd @@ -41,7 +41,7 @@ List LakeCat parameters: Get a list of available LakeCat values for certain para ```{r name_params} library(StreamCatTools) -region_params <- lc_get_params(param='areaOfInterest') +region_params <- lc_get_params(param='aoi') name_params <- lc_get_params(param='metric_names')