Skip to content

Commit

Permalink
Suppress warning when printing cals. Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Roe committed Nov 20, 2024
1 parent 07afed4 commit 410b76a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions R/cal.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ pillar_shaft.c14_cal <- function(x, ...) {
#' @noRd
#' @keywords internal
circa_point_yr <- function(x) {
y <- cal_point(x)
y <- cal_point(x, quiet = TRUE)
ret <- sprintf("c. %s", y)
format(ret, justify = "right")
}

#' @noRd
#' @keywords internal
circa_point_yr_colour <- function(x) {
y <- cal_point(x)
y <- cal_point(x, quiet = TRUE)
ret <- sprintf(
"%s %d %s",
pillar::style_subtle("c."),
Expand Down
12 changes: 7 additions & 5 deletions R/cal_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' function or an interval estimate is not appropriate, in which case
#' `method = "mode"` (the default) is recommended.
#'
#' @param x `cal` object. A vector of calibrated radiocarbon dates.
#' @param x `cal` object. A vector of calibrated radiocarbon dates
#' @param method Character. Method of estimation:
#' \describe{
#' \item{`"mode"` (default)}{age corresponding to the maximum peak of the probability distribution}
Expand All @@ -27,6 +27,7 @@
#' }
#' @param interval Numeric. Only used for `method = "local_mode"` and
#' `method = "central"`.
#' @param quiet Set `quiet = TRUE` to suppress warnings and messages
#'
#' @details
#'
Expand All @@ -48,7 +49,8 @@
#' cal_point(c14_calibrate(10000, 30))
cal_point <- function(x,
method = c("mode", "median", "mean", "local_mode", "central"),
interval = 0.954) {
interval = 0.954,
quiet = FALSE) {
# TODO: Check/cast x
method <- rlang::arg_match(method)

Expand All @@ -62,7 +64,7 @@ cal_point <- function(x,
)

# Flatten to era_yr
vec_c(!!!furrr::future_map(x, f, interval))
vec_c(!!!furrr::future_map(x, f, interval = interval, quiet = quiet))
}

#' Mode of a calibrated radiocarbon date
Expand All @@ -74,11 +76,11 @@ cal_point <- function(x,
#'
#' @noRd
#' @keywords internal
cal_mode <- function(x, ...) {
cal_mode <- function(x, quiet = FALSE, ...) {
y <- x$age[x$pdens == max(x$pdens, na.rm = TRUE) & !is.na(x$pdens)]
if (length(y) > 1) {
y <- y[1]
rlang::warn(
if (!isTRUE(quiet)) rlang::warn(
"`x` has more than one modal value. Only the first will be returned.",
"c14_ambiguous_summary"
)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ ppnd |>
filter(site == "Ganj Dareh") |>
select(lab_id, cra, error) |>
mutate(cal = c14_calibrate(cra, error))
#> Warning: `x` has more than one modal value. Only the first will be returned.
#> `x` has more than one modal value. Only the first will be returned.
#> # A tibble: 9 × 4
#> lab_id cra error cal
#> <chr> <int> <int> <cal>
Expand Down
7 changes: 5 additions & 2 deletions man/cal_point.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-cal.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ y <- c14_calibrate(c(6000, 5000, 3000), rep(10, 3))
y_era <- era::yr_era(cal_age(y)[[1]])
y_yr_ptype <- era::yr(era = y_era)

test_that("warning is suppressed when printing a multimodal cal", {
multimodal_cal <- c14_calibrate(10400, 20)
expect_no_warning(multimodal_cal, class = "c14_ambiguous_summary")
expect_no_warning(tibble::tibble(cal = multimodal_cal),
class = "c14_ambiguous_summary")
})

test_that("cal_crop() with default min_pdens does nothing", {
expect_equal(x, cal_crop(x))
})
Expand All @@ -20,3 +27,8 @@ test_that("cal_age_common() returns a yr vector", {
test_that("cal_interpolate() is normalised", {
expect_equal(sum(cal_pdens(cal_interpolate(x))[[1]]), 1)
})

test_that("cal_interpolate() does not propagate NAs", {
cal_with_nas <- cal_interpolate(c14_calibrate(5000, 10), seq(5500, 6000, 10))
expect_false(rlang::is_na(cal_with_nas))
})
10 changes: 5 additions & 5 deletions tests/testthat/test-cal_summary.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@


# Point estimates ---------------------------------------------------------

test_that("cal_mode() does not propagate NAs", {
cal_with_nas <- cal_interpolate(c14_calibrate(5000, 10), seq(5500, 6000, 10))
expect_false(rlang::is_na(cal_with_nas))
test_that("cal_point() warns about multiple modes unless quiet = TRUE", {
multimodal_cal <- c14_calibrate(10400, 20)
expect_warning(cal_point(multimodal_cal), class = "c14_ambiguous_summary")
expect_no_warning(cal_point(multimodal_cal, quiet = TRUE),
class = "c14_ambiguous_summary")
})

# Simple ranges -----------------------------------------------------------
Expand Down

0 comments on commit 410b76a

Please sign in to comment.