Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement package-level quiet option #14

Merged
merged 1 commit into from Feb 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions R/models.R
Expand Up @@ -4,7 +4,6 @@
#'
#' @param time Relative time of observation (typically seconds), numeric
#' @param conc Greenhouse gas concentration, numeric
#' @param quiet Be quiet? Logical
#' @return A wide-form \code{\link{data.frame}} with fit statistics for linear,
#' robust linear, and polynomial models. By default, extensive details are
#' provided only for the linear fi; for robust linear and polynomial, only
Expand All @@ -25,11 +24,11 @@
#' dat$SECONDS <- dat$SECONDS - min(dat$SECONDS) # normalize time to start at 0
#' plot(dat$SECONDS, dat$CO2)
#' wtf_fit_models(dat$SECONDS, dat$CO2)
wtf_fit_models <- function(time, conc, quiet = FALSE) {
wtf_fit_models <- function(time, conc) {
# Basic linear model
try(mod <- lm(conc ~ time))
if(!exists("mod")) {
if(!quiet) warning("Could not fit linear model")
wtf_warning("Could not fit linear model")
return(NULL)
}

Expand All @@ -49,7 +48,7 @@ wtf_fit_models <- function(time, conc, quiet = FALSE) {
coefficients(robust)[2]
},
error = function(e) {
if(!quiet) warning("Could not fit robust linear model")
wtf_warning("Could not fit robust linear model")
NA_real_
})

Expand All @@ -59,7 +58,7 @@ wtf_fit_models <- function(time, conc, quiet = FALSE) {
summary(poly)$r.squared
},
error = function(e) {
if(!quiet) warning("Could not fit polynomial model")
wtf_warning("Could not fit polynomial model")
NA_real_
})

Expand Down
17 changes: 7 additions & 10 deletions R/read_functions.R
Expand Up @@ -4,13 +4,12 @@
#'
#' @param file Filename to read, character
#' @param model Instrument model name, string
#' @param quiet Be quiet? Logical
#' @details The is an internal function used by \code{\link{wtf_read_LI7810}}
#' and \code{\link{wtf_read_LI7820}}, and not normally called by users.
#' @importFrom utils read.table
#' @importFrom lubridate ymd_hms
#' @return A \code{\link{data.frame}} with the parsed data.
wtf_read_LI78x0 <- function(file, model, quiet) {
wtf_read_LI78x0 <- function(file, model) {

dat_raw <- readLines(file)

Expand Down Expand Up @@ -41,36 +40,34 @@ wtf_read_LI78x0 <- function(file, model, quiet) {
dat$MODEL <- model
dat$DATE <- dat$TIME <- NULL

if(!quiet) message(basename(file), ": read ", nrow(dat), " rows of ", sn, " data, ",
min(dat$TIMESTAMP), " to ", max(dat$TIMESTAMP), " ", tz)
wtf_message(basename(file), ": read ", nrow(dat), " rows of ", sn, " data, ",
min(dat$TIMESTAMP), " to ", max(dat$TIMESTAMP), " ", tz)

return(dat)
}

#' Read a LI-7810 data file
#'
#' @param file Filename to read, character
#' @param quiet Be quiet? Logical
#' @return A \code{\link{data.frame}} with the parsed data.
#' @details Currently LI-7810 and LI-7820 files are handled identically.
#' @export
#' @examples
#' f <- system.file("extdata/TG10-01087.data", package = "whattheflux")
#' dat <- wtf_read_LI7810(f)
wtf_read_LI7810 <- function(file, quiet = FALSE) {
wtf_read_LI78x0(file, "LI-7810", quiet)
wtf_read_LI7810 <- function(file) {
wtf_read_LI78x0(file, "LI-7810")
}

#' Read a LI-7820 data file
#'
#' @param file Filename to read, character
#' @param quiet Be quiet? Logical
#' @return A \code{\link{data.frame}} with the parsed data.
#' @details Currently LI-7810 and LI-7820 files are handled identically.
#' @export
#' @examples
#' f <- system.file("extdata/TG20-01182.data", package = "whattheflux")
#' dat <- wtf_read_LI7820(f)
wtf_read_LI7820 <- function(file, quiet = FALSE) {
wtf_read_LI78x0(file, "LI-7820", quiet)
wtf_read_LI7820 <- function(file) {
wtf_read_LI78x0(file, "LI-7820")
}
15 changes: 15 additions & 0 deletions R/zzz.R
@@ -0,0 +1,15 @@
# zzz.R - miscellany

wtf_message <- function(...) {
if (getOption("whattheflux.quiet", default = FALSE)) {
return()
}
message(...)
}

wtf_warning <- function(...) {
if (getOption("whattheflux.quiet", default = FALSE)) {
return()
}
warning(...)
}
4 changes: 1 addition & 3 deletions man/wtf_fit_models.Rd

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

4 changes: 1 addition & 3 deletions man/wtf_read_LI7810.Rd

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

4 changes: 1 addition & 3 deletions man/wtf_read_LI7820.Rd

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

4 changes: 1 addition & 3 deletions man/wtf_read_LI78x0.Rd

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

14 changes: 7 additions & 7 deletions tests/testthat/test-read_functions.R
@@ -1,24 +1,24 @@
test_that("wtf_read_LI7810 works", {

options(whattheflux.quiet = TRUE)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in test files, I myself tend to use withr::local_options() to be sure it's reset. 😇

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @maelle ! I'm hesitant to introduce another package dependency just for this, but will consider.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testthat itself imports withr, so it's already there, it'd just be one more line in Suggests 😇 but more seriously I completely understand, no pressure 😀


# Good data
x <- wtf_read_LI7810("data/TG10-01087-good.data", quiet = TRUE)
x <- wtf_read_LI7810("data/TG10-01087-good.data")
expect_s3_class(x, "data.frame")
expect_true(is.na(x$CO2[1])) # parsed missing values
expect_true("SN" %in% names(x)) # parsed serial number from header

suppressMessages({
expect_message(wtf_read_LI7810("data/TG10-01087-good.data", quiet = FALSE))
})

# Bad data
expect_error(wtf_read_LI7810("data/TG10-01087-bad.data", quiet = TRUE),
expect_error(wtf_read_LI7810("data/TG10-01087-bad.data"),
regexp = "does not appear")
})

test_that("wtf_read_LI7820 works", {

options(whattheflux.quiet = TRUE)

# Good data
x <- wtf_read_LI7820("data/TG20-01182-good.data", quiet = TRUE)
x <- wtf_read_LI7820("data/TG20-01182-good.data")
expect_s3_class(x, "data.frame")
expect_true("SN" %in% names(x)) # parsed serial number from header

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-zzz.R
@@ -0,0 +1,10 @@
test_that("quiet functionality works", {
# Check that the wtf_message and wtf_warning functions respect quiet option
options(whattheflux.quiet = TRUE)
expect_no_message(wtf_message("hi"))
expect_no_warning(wtf_warning("hi"))

options(whattheflux.quiet = FALSE)
expect_message(wtf_message("hi"), regexp = "hi")
expect_warning(wtf_warning("hi"), regexp = "hi")
})