Skip to content

Commit ea95108

Browse files
Copilotkristinawlai
andcommitted
Apply PR #507 + PR #506 changes: replace ggpubr with patchwork, add graceful internet error handling
Co-authored-by: kristinawlai <57973437+kristinawlai@users.noreply.github.com>
1 parent 9fb0273 commit ea95108

File tree

12 files changed

+312
-146
lines changed

12 files changed

+312
-146
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ junit.xml
2525

2626
**/*.quarto_ipynb
2727
*.knit.md
28+
tests/vdiffr.Rout.fail

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: serocalculator
33
Title: Estimating Infection Rates from Serological Data
4-
Version: 1.4.0.9003
4+
Version: 1.4.0.9004
55
Authors@R: c(
66
person("Kristina", "Lai", , "kwlai@ucdavis.edu", role = c("aut", "cre")),
77
person("Chris", "Orwa", role = "aut"),
@@ -27,10 +27,11 @@ Imports:
2727
dplyr,
2828
foreach,
2929
ggplot2,
30-
ggpubr,
3130
lifecycle,
3231
magrittr,
32+
patchwork,
3333
Rcpp,
34+
readr,
3435
rlang,
3536
rngtools,
3637
scales,
@@ -53,7 +54,6 @@ Suggests:
5354
knitr,
5455
mixtools,
5556
pak,
56-
readr,
5757
quarto,
5858
rmarkdown,
5959
spelling,

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# serocalculator (development version)
22

3+
## Bug fixes
4+
5+
* `load_noise_params()` and `load_sr_params()` now fail gracefully with informative messages when internet resources are unavailable, complying with CRAN policy (#505)
6+
7+
* Replaced `ggpubr` with `patchwork` for arranging panel plots in `autoplot.seroincidence.by()` and `graph_seroresponse_model_1()`, removing the indirect `ggrepel` dependency that required R >= 4.5.0 (#507)
8+
39
# serocalculator 1.4.0
410

511
## New features

R/autoplot.seroincidence.by.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' @param object a '"seroincidence.by"' object (from [est_seroincidence_by()])
55
#' @param ncol number of columns to use for panel of plots
66
#' @inheritDotParams autoplot.seroincidence
7-
#' @return a `"ggarrange"` object: a single or [list()] of [ggplot2::ggplot()]s
7+
#' @return a `"patchwork"` object: a single or [list()] of [ggplot2::ggplot()]s
88
#' @export
99
#' @examples
1010
#'\donttest{
@@ -41,14 +41,19 @@ autoplot.seroincidence.by <- function(
4141
ncol = min(3, length(object)),
4242
...) {
4343
if (length(object) == 0) {
44-
stop("The input doesn't contain any fits. Did subsetting go wrong?")
44+
cli::cli_abort(
45+
"The input doesn't contain any fits. Did subsetting go wrong?"
46+
)
4547
}
4648

4749
if (!attr(object, "graphs_included")) {
48-
stop(
49-
"Graphs cannot be extracted; ",
50-
"`build_graph` was not `TRUE` in the call to `est_seroincidence_by()`"
51-
)
50+
cli::cli_abort(c(
51+
"Graphs cannot be extracted.",
52+
"i" = paste0(
53+
"`build_graph` was not `TRUE` in the call to",
54+
" `est_seroincidence_by()`"
55+
)
56+
))
5257
figure <- NULL
5358
}
5459

@@ -63,7 +68,7 @@ autoplot.seroincidence.by <- function(
6368
nrow <- ceiling(length(figs) / ncol)
6469
figure <- do.call(
6570
what = function(...) {
66-
ggpubr::ggarrange(
71+
patchwork::wrap_plots(
6772
...,
6873
ncol = ncol,
6974
nrow = nrow

R/graph_seroresponse_model_1.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ graph_seroresponse_model_1 <- function(
5959
nrow <- ceiling(length(figs) / ncol)
6060
figure <- do.call(
6161
what = function(...) {
62-
ggpubr::ggarrange(
62+
patchwork::wrap_plots(
6363
...,
6464
ncol = ncol,
6565
nrow = nrow

R/load_noise_params.R

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,55 @@
1111
#' with extra attribute `antigen_isos`)
1212
#' @export
1313
#' @examples
14-
#' noise <- load_noise_params(serocalculator_example("example_noise_params.rds"))
14+
#' noise <- load_noise_params(
15+
#' serocalculator_example("example_noise_params.rds")
16+
#' )
1517
#' print(noise)
1618
#'
1719
load_noise_params <- function(file_path, antigen_isos = NULL) {
18-
if (file_path %>% substr(1, 4) == "http") {
20+
is_url <- file_path |> substr(1, 4) == "http"
21+
22+
if (is_url) {
1923
file_path <- url(file_path)
2024
}
2125

22-
noise <-
23-
file_path %>%
24-
readRDS() %>%
25-
as_noise_params()
26+
noise <- tryCatch(
27+
{
28+
data <- if (is_url) {
29+
withCallingHandlers(
30+
readr::read_rds(file_path),
31+
warning = function(w) {
32+
invokeRestart("muffleWarning")
33+
}
34+
)
35+
} else {
36+
readr::read_rds(file_path)
37+
}
38+
39+
as_noise_params(data, antigen_isos = antigen_isos)
40+
},
41+
error = function(e) {
42+
if (is_url) {
43+
cli::cli_abort(
44+
class = "internet_resource_unavailable",
45+
message = c(
46+
"Unable to load noise parameters from internet resource.",
47+
"x" = paste(
48+
"The resource at {.url {summary(file_path)$description}}",
49+
"is not available or has changed."
50+
),
51+
"i" = paste(
52+
"Please check your internet connection",
53+
"and verify the URL is correct."
54+
),
55+
"i" = "Original error: {e$message}"
56+
)
57+
)
58+
} else {
59+
rlang::cnd_signal(e)
60+
}
61+
}
62+
)
2663

2764
return(noise)
2865
}

R/load_sr_params.R

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,49 @@
1414
#' print(curve)
1515
#'
1616
load_sr_params <- function(file_path, antigen_isos = NULL) {
17-
if (file_path |> substr(1, 4) == "http") {
17+
is_url <- file_path |> substr(1, 4) == "http"
18+
19+
if (is_url) {
1820
file_path <- url(file_path)
1921
}
2022

21-
curve_params <-
22-
file_path |>
23-
readRDS() |>
24-
as_sr_params()
23+
curve_params <- tryCatch(
24+
{
25+
data <- if (is_url) {
26+
withCallingHandlers(
27+
readr::read_rds(file_path),
28+
warning = function(w) {
29+
invokeRestart("muffleWarning")
30+
}
31+
)
32+
} else {
33+
readr::read_rds(file_path)
34+
}
35+
36+
as_sr_params(data, antigen_isos = antigen_isos)
37+
},
38+
error = function(e) {
39+
if (is_url) {
40+
cli::cli_abort(
41+
class = "internet_resource_unavailable",
42+
message = c(
43+
"Unable to load seroresponse parameters from internet resource.",
44+
"x" = paste(
45+
"The resource at {.url {summary(file_path)$description}}",
46+
"is not available or has changed."
47+
),
48+
"i" = paste(
49+
"Please check your internet connection",
50+
"and verify the URL is correct."
51+
),
52+
"i" = "Original error: {e$message}"
53+
)
54+
)
55+
} else {
56+
rlang::cnd_signal(e)
57+
}
58+
}
59+
)
2560

2661
return(curve_params)
2762
}

man/autoplot.seroincidence.by.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/load_noise_params.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)