Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: StreamCatTools
Type: Package
Title: 'StreamCatTools'
Version: 0.8.0
Version: 0.8.0.9000
Authors@R: c(person(given = "Marc",
family = "Weber",
role = c("aut", "cre"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# StreamCatTools (development version)

# StreamCatTools 0.8.0

- Added code coverage to the package
Expand Down
27 changes: 20 additions & 7 deletions R/lc_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ lc_get_data <- function(comid = NULL,
state=state,county=county,region=region,conus=conus,
countOnly=countOnly
)
df <- req |>
df <- tryCatch({
req |>
httr2::req_method("POST") |>
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded") |>
httr2::req_method("POST") |>
Expand All @@ -146,12 +147,24 @@ lc_get_data <- function(comid = NULL,
httr2::req_perform() |>
httr2::resp_body_string() |>
jsonlite::fromJSON()
# Return a data frame
if (is.null(countOnly)){
df <- df$items |>
dplyr::select(comid, dplyr::everything())
return(df)
} else return(df$items)
},error = function(e) {
message("An error occurred during req_perform(); the service may be down or function parameters may be mis-specified: ", e$message)
return(NULL)
})
# Return a data frame if success
if (exists("df") && !is.null(df)){
# Return a data frame
if (is.null(countOnly)){
df <- df$items |>
dplyr::select(comid, dplyr::everything())
return(df)
} else return(df$items)
if (is.null(countOnly)){
df <- df$items |>
dplyr::select(comid, dplyr::everything())
return(df)
} else return(df$items)
}
}


Expand Down
129 changes: 71 additions & 58 deletions R/lc_get_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,59 +32,62 @@ lc_get_params <- function(param = 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=='aoi'){
params <- strsplit(stringr::str_sub(resp$aoi_param_info[[1]]$options,2,-2),",")[[1]]
params <- c(gsub(" ","", params),'other')
params <- params[order(params)]
params <- params[!params %in% c('catrp100','wsrp100','other')]
} else if(param == 'metric_names') {
params <- resp$name_options[[1]][[1]]
params <- params[!duplicated(params)]
params <- params[order(params)]
} else if(param == '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,
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)]
} else if(param == 'state'){
params <- resp$state_options[[1]]
params <- params[!params$st_abbr %in% c('AK','HI','PR'),]
params$st_fips <- as.character(params$st_fips)
params$st_fips[nchar(params$st_fips) < 2] <- paste0('0',params$st_fips[nchar(params$st_fips) < 2])
params <- params[order(params$st_name),]
rownames(params) <- 1:nrow(params)
} else if(param == 'county'){
params <- resp$county_options[[1]]
params$fips <- as.character(params$fips)
params$fips[nchar(params$fips) < 5] <- paste0('0',params$fips[nchar(params$fips) < 5])
params <- params[with(params,order(state,county_name)),]
rownames(params) <- 1:nrow(params)
}
return(params)
result <- tryCatch({
resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/metrics")$items
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)]
params <- params[!params %in% c('catrp100','wsrp100','other')]
} else if(param == 'metric_names') {
params <- resp$name_options[[1]][[1]]
params <- params[!duplicated(params)]
params <- params[order(params)]
} else if(param == '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,
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)]
} else if(param == 'state'){
params <- resp$state_options[[1]]
params <- params[!params$st_abbr %in% c('AK','HI','PR'),]
params$st_fips <- as.character(params$st_fips)
params$st_fips[nchar(params$st_fips) < 2] <- paste0('0',params$st_fips[nchar(params$st_fips) < 2])
params <- params[order(params$st_name),]
} else if(param == 'county'){
params <- resp$county_options[[1]]
params$fips <- as.character(params$fips)
params$fips[nchar(params$fips) < 5] <- paste0('0',params$fips[nchar(params$fips) < 5])
params <- params[with(params,order(state,county_name)),]
}
},error = function(e) {
message("An error occurred during req_perform(); the service may be down or function parameters may be mis-specified: ", e$message)
return(NULL)
})
return(result)
}

#' @title Lookup Full Metric Name
Expand All @@ -106,8 +109,13 @@ lc_get_params <- function(param = NULL) {
#' fullname <- lc_fullname(metric='clay')

lc_fullname <- function(metric = NULL) {
resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/datadictionary")$items
result <- unique(resp$short_display_name[resp$metric_prefix %in% metric])
result <- tryCatch({
resp <-jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/datadictionary")$items
resp <- unique(resp$short_display_name[resp$metric_prefix %in% metric])
},error = function(e) {
message("An error occurred during req_perform(); the service may be down or function parameters may be mis-specified: ", e$message)
return(NULL)
})
return(result)
}

Expand All @@ -134,7 +142,7 @@ lc_fullname <- function(metric = NULL) {
#' \dontrun{
#' metrics <- lc_get_metric_names(category='Natural')
#' metrics <- lc_get_metric_names(category = c('Anthropogenic','Natural'),
#' aoi=c('Cat','Ws')}
#' aoi=c('Cat','Ws'))}


lc_get_metric_names <- function(category = NULL,
Expand All @@ -156,10 +164,15 @@ lc_get_metric_names <- function(category = NULL,
}
aoi <- stringr::str_to_title(aoi)
}
resp <- params <- httr2::request('https://api.epa.gov/StreamCat/lakes/variable_info') |>
resp <- tryCatch({
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)
},error = function(e) {
message("An error occurred during req_perform(); the service may be down or function parameters may be mis-specified: ", e$message)
return(NULL)
})

filters <- list(INDICATOR_CATEGORY = category, AOI = aoi, YEAR = year,
DSNAME = dataset)
Expand All @@ -173,7 +186,7 @@ lc_get_metric_names <- function(category = NULL,
.f = function(df, col_name) {
filter_values <- filters[[col_name]]
if (!is.null(filter_values)) {
temp_col <- stringr::str_split(df[[col_name]], ",")
temp_col <- stringr::str_split(df[[col_name]], ", ")
df <- df[purrr::map_lgl(temp_col, ~ any(.x %in% filter_values)), , drop = FALSE]
}
df
Expand Down
15 changes: 14 additions & 1 deletion R/sc_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ sc_get_data <- function(comid = NULL,
state=state,county=county,region=region,conus=conus,
countOnly=countOnly
)
df <- req |>
df <- tryCatch({
req |>
httr2::req_method("POST") |>
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded") |>
httr2::req_method("POST") |>
Expand All @@ -163,12 +164,24 @@ sc_get_data <- function(comid = NULL,
httr2::req_perform() |>
httr2::resp_body_string() |>
jsonlite::fromJSON()
},error = function(e) {
message("An error occurred during req_perform(); the service may be down or function parameters may be mis-specified: ", e$message)
return(NULL)
})
# Return a data frame if success
if (exists("df") && !is.null(df)){
# Return a data frame
if (is.null(countOnly)){
df <- df$items |>
dplyr::select(comid, dplyr::everything())
return(df)
} else return(df$items)
if (is.null(countOnly)){
df <- df$items |>
dplyr::select(comid, dplyr::everything())
return(df)
} else return(df$items)
}
}


Expand Down
Loading