Skip to content

Commit

Permalink
move memoise into onload for #389
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Jun 5, 2024
1 parent 78e9ba7 commit e80be3b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nhdplusTools
Type: Package
Title: NHDPlus Tools
Version: 1.2.0
Version: 1.2.1
Authors@R: c(person(given = "David",
family = "Blodgett",
role = c("aut", "cre"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
nhdplusTools 1.2.1
==========
Bug fix for data cache directory #389

nhdplusTools 1.2.0
==========

Expand Down
14 changes: 13 additions & 1 deletion R/A_nhdplusTools.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ nhdplusTools_data_dir <- function(dir = NULL) {

nhdpt_dat_dir <- try(get("nhdpt_dat_dir", envir = nhdplusTools_env), silent = TRUE)

if(inherits(nhdpt_dat_dir, "try-error")) {
if(inherits(nhdpt_dat_dir, "try-error") || grepl("CRAN", nhdpt_dat_dir)) {
assign("nhdpt_dat_dir",
tools::R_user_dir("nhdplusTools"),
envir = nhdplusTools_env)
Expand Down Expand Up @@ -428,6 +428,18 @@ nhdplusTools_memoise_timeout <- function() {
}
}

.onLoad <- function(libname, pkgname) {
query_usgs_arcrest <<- memoise::memoise(query_usgs_arcrest,
~memoise::timeout(nhdplusTools_memoise_timeout()),
cache = nhdplusTools_memoise_cache())
query_usgs_geoserver <<- memoise::memoise(query_usgs_geoserver,
~memoise::timeout(nhdplusTools_memoise_timeout()),
cache = nhdplusTools_memoise_cache())
query_nldi <<- memoise::memoise(query_nldi,
~memoise::timeout(nhdplusTools_memoise_timeout()),
cache = nhdplusTools_memoise_cache())
}

#' @title Align NHD Dataset Names
#' @description this function takes any NHDPlus dataset and aligns the attribute names with those used in nhdplusTools.
#' @param x a \code{sf} object of nhdplus flowlines
Expand Down
4 changes: 2 additions & 2 deletions R/arcrest_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ get_3dhp_service_info <- memoise::memoise(function() {
#' @importFrom httr RETRY content
#' @importFrom dplyr filter
#' @importFrom methods as
query_usgs_arcrest <- memoise::memoise(function(AOI = NULL, ids = NULL,
query_usgs_arcrest <- function(AOI = NULL, ids = NULL,
type = NULL, where = NULL,
t_srs = NULL,
buffer = 0.5){
Expand Down Expand Up @@ -216,4 +216,4 @@ query_usgs_arcrest <- memoise::memoise(function(AOI = NULL, ids = NULL,
all_out[[l]] <- out
}
tryCatch(sf::st_sf(data.table::rbindlist(all_out)), error = function(e) NULL)
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())
}
6 changes: 3 additions & 3 deletions R/geoserver_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' @importFrom methods as
#' @importFrom xml2 read_xml

query_usgs_geoserver <- memoise::memoise(function(AOI = NULL, ids = NULL,
query_usgs_geoserver <- function(AOI = NULL, ids = NULL,
type = NULL, filter = NULL,
t_srs = NULL,
buffer = 0.5) {
Expand Down Expand Up @@ -177,7 +177,7 @@ query_usgs_geoserver <- memoise::memoise(function(AOI = NULL, ids = NULL,
NULL
}

}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())
}

unify_types <- function(out) {
all_class <- bind_rows(lapply(out, function(x) {
Expand Down Expand Up @@ -367,7 +367,7 @@ extact_comid_nwis <- memoise::memoise(function(nwis){
c <- rawToChar(httr::RETRY("GET", url)$content)
f.comid <- jsonlite::fromJSON(c, simplifyVector = TRUE)
f.comid$features$properties$comid
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())
})

#' @importFrom sf st_make_valid st_as_sfc st_bbox st_buffer st_transform
check_query_params <- function(AOI, ids, type, where, source, t_srs, buffer) {
Expand Down
2 changes: 1 addition & 1 deletion R/get_geoconnex.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param url character url to get
#' @return list containing parsed json on success, NULL otherwise
#' @noRd
mem_get_json <- landing <- memoise::memoise(\(url) {
mem_get_json <- memoise::memoise(\(url) {
tryCatch({
retn <- httr::RETRY("GET", url, httr::accept_json())

Expand Down
4 changes: 2 additions & 2 deletions R/get_nldi.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ get_nldi_index <- function(location) {
#' @importFrom httr GET
#' @importFrom jsonlite fromJSON
#' @noRd
query_nldi <- memoise::memoise(function(query, base_path = "/linked-data", parse_json = TRUE) {
query_nldi <- function(query, base_path = "/linked-data", parse_json = TRUE) {
nldi_base_url <- paste0(get_nldi_url(), base_path)

url <- paste(nldi_base_url, query,
Expand Down Expand Up @@ -303,7 +303,7 @@ query_nldi <- memoise::memoise(function(query, base_path = "/linked-data", parse
warning("Something went wrong accessing the NLDI.\n", e)
NULL
})
}, ~memoise::timeout(nhdplusTools_memoise_timeout()), cache = nhdplusTools_memoise_cache())
}

#' @noRd
check_nldi_feature <- function(nldi_feature, convert = TRUE) {
Expand Down

0 comments on commit e80be3b

Please sign in to comment.