From 32d5f53a043a2d0e8340f719c01ea03d4362eaf6 Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Mon, 23 Mar 2020 15:41:31 -0700 Subject: [PATCH] add rnoaa_options function for handlling pkg level options within rnoaa_options added cache_messages param setting to toggle messages added a test for rnoaa_options aaded message suppression handling in the four functions that currently use hoardr caching: cpc, arc2, lcd, bsw --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/arc2.R | 4 +++- R/bsw.R | 2 ++ R/cpc.R | 4 +++- R/isd.R | 2 +- R/lcd.R | 10 ++++++---- R/rnoaa_options.R | 30 +++++++++++++++++++++++++++++ man/arc2.Rd | 2 +- man/cpc_prcp.Rd | 2 +- man/lcd.Rd | 8 ++++---- man/rnoaa_options.Rd | 24 +++++++++++++++++++++++ tests/testthat/test-rnoaa_options.R | 17 ++++++++++++++++ 13 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 R/rnoaa_options.R create mode 100644 man/rnoaa_options.Rd create mode 100644 tests/testthat/test-rnoaa_options.R diff --git a/DESCRIPTION b/DESCRIPTION index 06fed693..05a10485 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,7 +7,7 @@ Description: Client for many 'NOAA' data sources including the 'NCDC' climate for 'NOAA' sea ice data, the 'NOAA' severe weather inventory, 'NOAA' Historical Observing 'Metadata' Repository ('HOMR') data, 'NOAA' storm data via 'IBTrACS', tornado data via the 'NOAA' storm prediction center, and more. -Version: 0.9.5.96 +Version: 0.9.5.97 License: MIT + file LICENSE Encoding: UTF-8 Language: en-US diff --git a/NAMESPACE b/NAMESPACE index 58278c67..9e282cad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -103,6 +103,7 @@ export(noaa_plot) export(noaa_seaice) export(noaa_stations) export(readshpfile) +export(rnoaa_options) export(se_data) export(se_files) export(sea_ice) diff --git a/R/arc2.R b/R/arc2.R index 806496c6..7af2fd7a 100644 --- a/R/arc2.R +++ b/R/arc2.R @@ -12,7 +12,7 @@ #' - precip - precipitation #' #' @examples \dontrun{ -#' arc2(date = "1983-01-01") +#' x = arc2(date = "1983-01-01") #' arc2(date = "2017-02-14") #' } arc2 <- function(date, ...) { @@ -36,6 +36,8 @@ arc2_get <- function(year, month, day, cache = TRUE, overwrite = FALSE, ...) { res <- suppressMessages( arc2_GET_write(sub("/$", "", key), file, overwrite, ...)) file <- res$content + } else { + cache_mssg(file) } return(file) } diff --git a/R/bsw.R b/R/bsw.R index f0956740..cc763373 100644 --- a/R/bsw.R +++ b/R/bsw.R @@ -133,6 +133,8 @@ bsw_get <- function(year, month, day, uv_stress, resolution, ) if (!file.exists(file)) { suppressMessages(bsw_GET_write(key, file, overwrite, ...)) + } else { + cache_mssg(file) } return(file) } diff --git a/R/cpc.R b/R/cpc.R index ab9b586f..ae09d797 100644 --- a/R/cpc.R +++ b/R/cpc.R @@ -34,7 +34,7 @@ #' you can easily do it yourself by e.g., `subset(x, precip >= 0)` #' #' @examples \dontrun{ -#' cpc_prcp(date = "2017-01-15") +#' x = cpc_prcp(date = "2017-01-15") #' cpc_prcp(date = "2015-06-05") #' cpc_prcp(date = "2017-01-15") #' cpc_prcp(date = "2005-07-09") @@ -73,6 +73,8 @@ cpc_get <- function(year, month, day, us, cache = TRUE, overwrite = FALSE, ...) if (!file.exists(file)) { res <- suppressMessages(cpc_GET_write(sub("/$", "", key), file, overwrite, ...)) file <- res$content + } else { + cache_mssg(file) } return(file) } diff --git a/R/isd.R b/R/isd.R index 023a9814..475b89ad 100644 --- a/R/isd.R +++ b/R/isd.R @@ -196,7 +196,7 @@ isdbase <- function() 'https://www1.ncdc.noaa.gov/pub/data/noaa' read_isd <- function(x, cleanup, force, additional, parallel, cores, progress) { path_rds <- x if (file.exists(path_rds) && !force) { - message("found in cache") + cache_mssg(path_rds) df <- readRDS(path_rds) } else { df <- isdparser::isd_parse( diff --git a/R/lcd.R b/R/lcd.R index 39709e65..44c17c1f 100644 --- a/R/lcd.R +++ b/R/lcd.R @@ -38,10 +38,10 @@ #' column name #' #' @examples \dontrun{ -#' lcd(station = "01338099999", year = 2017, verbose = TRUE) -#' lcd(station = "01338099999", year = 2015, verbose = TRUE) -#' lcd(station = "02413099999", year = 2009, verbose = TRUE) -#' lcd(station = "02413099999", year = 2001, verbose = TRUE) +#' x = lcd(station = "01338099999", year = 2017) +#' lcd(station = "01338099999", year = 2015) +#' lcd(station = "02413099999", year = 2009) +#' lcd(station = "02413099999", year = 2001) #' #' # pass curl options #' lcd(station = "02413099999", year = 2002, verbose = TRUE) @@ -65,6 +65,8 @@ lcd_get <- function(station, year, overwrite = FALSE, ...) { sprintf("%s_%s.csv", year, station)) if (!file.exists(file)) { suppressMessages(lcd_GET_write(key, file, overwrite, ...)) + } else { + cache_mssg(file) } return(file) } diff --git a/R/rnoaa_options.R b/R/rnoaa_options.R new file mode 100644 index 00000000..16c438f0 --- /dev/null +++ b/R/rnoaa_options.R @@ -0,0 +1,30 @@ +roenv <- new.env() +roenv$cache_messages <- TRUE + +#' rnoaa options +#' @export +#' @param cache_messages (logical) whether to emit messages with information +#' on caching status for function calls that can cache data. default: `TRUE` +#' @details rnoaa package level options; stored in an internal +#' package environment `roenv` +#' @examples \dontrun{ +#' rnoaa_options(cache_messages = FALSE) +#' } +rnoaa_options <- function(cache_messages = TRUE) { + roenv$cache_messages <- cache_messages + return(NULL) +} + +stract <- function(str, pattern) regmatches(str, regexpr(pattern, str)) + +cache_mssg <- function(file) { + if (roenv$cache_messages) { + fi <- file.info(file) + size <- round(fi$size/1000000, 3) + chaftdec <- nchar(stract(as.character(size), '^[0-9]+')) + if (chaftdec > 1) size <- round(size, 1) + message("using cached file: ", file) + message( + sprintf("date created/size(mb): %s / %s", fi$ctime, size)) + } +} diff --git a/man/arc2.Rd b/man/arc2.Rd index de24d298..fcc10cbb 100644 --- a/man/arc2.Rd +++ b/man/arc2.Rd @@ -24,7 +24,7 @@ Arc2 - Africa Rainfall Climatology version 2 } \examples{ \dontrun{ -arc2(date = "1983-01-01") +x = arc2(date = "1983-01-01") arc2(date = "2017-02-14") } } diff --git a/man/cpc_prcp.Rd b/man/cpc_prcp.Rd index 2d5b38a4..b6938011 100644 --- a/man/cpc_prcp.Rd +++ b/man/cpc_prcp.Rd @@ -46,7 +46,7 @@ you can easily do it yourself by e.g., \code{subset(x, precip >= 0)} \examples{ \dontrun{ -cpc_prcp(date = "2017-01-15") +x = cpc_prcp(date = "2017-01-15") cpc_prcp(date = "2015-06-05") cpc_prcp(date = "2017-01-15") cpc_prcp(date = "2005-07-09") diff --git a/man/lcd.Rd b/man/lcd.Rd index 9a720954..14b6fc0d 100644 --- a/man/lcd.Rd +++ b/man/lcd.Rd @@ -48,10 +48,10 @@ Local Climitalogical Data from NOAA } \examples{ \dontrun{ -lcd(station = "01338099999", year = 2017, verbose = TRUE) -lcd(station = "01338099999", year = 2015, verbose = TRUE) -lcd(station = "02413099999", year = 2009, verbose = TRUE) -lcd(station = "02413099999", year = 2001, verbose = TRUE) +x = lcd(station = "01338099999", year = 2017) +lcd(station = "01338099999", year = 2015) +lcd(station = "02413099999", year = 2009) +lcd(station = "02413099999", year = 2001) # pass curl options lcd(station = "02413099999", year = 2002, verbose = TRUE) diff --git a/man/rnoaa_options.Rd b/man/rnoaa_options.Rd new file mode 100644 index 00000000..358ce71d --- /dev/null +++ b/man/rnoaa_options.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/rnoaa_options.R +\name{rnoaa_options} +\alias{rnoaa_options} +\title{rnoaa options} +\usage{ +rnoaa_options(cache_messages = TRUE) +} +\arguments{ +\item{cache_messages}{(logical) whether to emit messages with information +on caching status for function calls that can cache data. default: \code{TRUE}} +} +\description{ +rnoaa options +} +\details{ +rnoaa package level options; stored in an internal +package environment \code{roenv} +} +\examples{ +\dontrun{ +rnoaa_options(cache_messages = FALSE) +} +} diff --git a/tests/testthat/test-rnoaa_options.R b/tests/testthat/test-rnoaa_options.R new file mode 100644 index 00000000..4a4d76c4 --- /dev/null +++ b/tests/testthat/test-rnoaa_options.R @@ -0,0 +1,17 @@ +# delete any cached files +arc2_cache$delete_all() + +test_that("rnoaa_options", { + skip_on_cran() + + expect_is(rnoaa_options, "function") + expect_null(rnoaa_options()) + + # after setting param its in the env var + expect_true(roenv$cache_messages) + rnoaa_options(FALSE) + expect_false(roenv$cache_messages) + + # reset options + rnoaa_options() +})