Skip to content

Commit 75e31a3

Browse files
committed
use more rlang
Closes #198 * stop -> abort * warning -> warn * message -> inform * setNames -> set_names
1 parent b5ad462 commit 75e31a3

22 files changed

+268
-272
lines changed

R/available-module-functions.R

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@
1717
#' available_ppc("grouped", invert = TRUE)
1818
#'
1919
available_ppc <- function(pattern, fixed = FALSE, invert = FALSE) {
20-
.list_module_functions("ppc", .pattern = pattern,
21-
fixed = fixed, invert = invert)
20+
.list_module_functions("ppc",
21+
.pattern = pattern,
22+
fixed = fixed,
23+
invert = invert)
2224
}
2325

2426
#' @rdname available_ppc
2527
#' @export
2628
available_mcmc <- function(pattern, fixed = FALSE, invert = FALSE) {
27-
.list_module_functions("mcmc", .pattern = pattern,
28-
fixed = fixed, invert = invert)
29+
.list_module_functions("mcmc",
30+
.pattern = pattern,
31+
fixed = fixed,
32+
invert = invert)
2933
}
3034

3135
#' @export
3236
print.bayesplot_function_list <- function(x, ...) {
3337
atts <- attributes(x)
3438
cat("bayesplot", toupper(atts[["module"]]), "module:\n")
35-
if (!is.null(atts[["pattern"]]))
36-
cat(paste0(
37-
"(",
38-
ifelse(atts[["inverted"]], "excluding", "matching"),
39-
" pattern '",
40-
atts[["pattern"]],
41-
"')"
42-
), "\n")
43-
39+
if (!is.null(atts[["pattern"]])) {
40+
msg <- paste0("(", ifelse(atts[["inverted"]], "excluding", "matching"),
41+
" pattern '", atts[["pattern"]], "')")
42+
cat(msg, "\n")
43+
}
4444
cat(paste0(" ", x), sep = "\n")
4545
invisible(x)
4646
}
@@ -54,26 +54,28 @@ print.bayesplot_function_list <- function(x, ...) {
5454
invert = FALSE) {
5555

5656
.module <- match.arg(.module)
57-
if (missing(.pattern))
57+
if (missing(.pattern)) {
5858
.pattern <- NULL
59+
}
5960

60-
funs <- sort(grep(
61-
paste0("^", .module, "_"),
62-
getNamespaceExports("bayesplot"),
61+
all_funs <- grep(
62+
pattern = paste0("^", .module, "_"),
63+
x = getNamespaceExports("bayesplot"),
6364
value = TRUE
64-
))
65+
)
66+
return_funs <- sort(all_funs)
67+
68+
if (!is.null(.pattern)) {
69+
return_funs <- grep(
70+
pattern = .pattern,
71+
x = return_funs,
72+
value = TRUE,
73+
fixed = fixed,
74+
invert = invert
75+
)
76+
}
6577
structure(
66-
.Data =
67-
if (is.null(.pattern))
68-
funs
69-
else
70-
grep(
71-
pattern = .pattern,
72-
x = funs,
73-
value = TRUE,
74-
fixed = fixed,
75-
invert = invert
76-
),
78+
return_funs,
7779
class = c("bayesplot_function_list", "character"),
7880
module = .module,
7981
pattern = .pattern,

R/bayesplot-colors.R

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ NULL
129129
#' @rdname bayesplot-colors
130130
#' @export
131131
color_scheme_set <- function(scheme = "blue") {
132-
stopifnot(is.character(scheme))
132+
if (!is.character(scheme)) {
133+
abort("'scheme' should be a character vector of length 1 or 6.")
134+
}
135+
133136
if (length(scheme) == 1) {
134137
x <- scheme_from_string(scheme)
135138
} else if (length(scheme) == 6) {
136139
x <- prepare_custom_colors(scheme)
137140
} else {
138-
stop("'scheme' should be a character vector of length 1 or 6.")
141+
abort("'scheme' should be a character vector of length 1 or 6.")
139142
}
140143
.bayesplot_aesthetics[["scheme"]] <- x
141144
invisible(x)
@@ -193,7 +196,7 @@ print.bayesplot_scheme <- function(x, ...) {
193196

194197
#' @export
195198
plot.bayesplot_scheme <- function(x, ...) {
196-
scheme <- attr(x, "scheme_name") %||% stop("Scheme name not found.")
199+
scheme <- attr(x, "scheme_name") %||% abort("Scheme name not found.")
197200
plot_scheme(scheme)
198201
}
199202

@@ -248,25 +251,23 @@ scheme_level_names <- function() {
248251
#' @noRd
249252
#' @param scheme A string (length 1) naming a scheme
250253
scheme_from_string <- function(scheme) {
251-
stopifnot(length(scheme) == 1)
252254
if (identical(substr(scheme, 1, 4), "mix-")) {
253255
# user specified a mixed scheme (e.g., "mix-blue-red")
254256
to_mix <- unlist(strsplit(scheme, split = "-"))[2:3]
255-
x <- setNames(mixed_scheme(to_mix[1], to_mix[2]), scheme_level_names())
257+
x <- set_names(mixed_scheme(to_mix[1], to_mix[2]), scheme_level_names())
256258
return(structure(x, mixed = TRUE, scheme_name = scheme))
257259
} else if (identical(substr(scheme, 1, 7), "brewer-")) {
258260
# user specified a ColorBrewer scheme (e.g., "brewer-Blues")
259261
if (!requireNamespace("RColorBrewer", quietly = TRUE)) {
260-
stop("Please install the 'RColorBrewer' package to use a ColorBrewer scheme.",
261-
call. = FALSE)
262+
abort("Please install the 'RColorBrewer' package to use a ColorBrewer scheme.")
262263
}
263264
clrs <- RColorBrewer::brewer.pal(n = 6, name = gsub("brewer-", "", scheme))
264-
x <- setNames(as.list(clrs), scheme_level_names())
265+
x <- set_names(as.list(clrs), scheme_level_names())
265266
return(structure(x, mixed = FALSE, scheme_name = scheme))
266267
} else {
267268
# check for scheme in master_color_list
268269
scheme <- match.arg(scheme, choices = names(master_color_list))
269-
x <- setNames(master_color_list[[scheme]], scheme_level_names())
270+
x <- set_names(master_color_list[[scheme]], scheme_level_names())
270271
return(structure(x, mixed = FALSE, scheme_name = scheme))
271272
}
272273
}
@@ -331,8 +332,7 @@ full_level_name <- function(x) {
331332
# Custom color scheme if 6 colors specified
332333
prepare_custom_colors <- function(scheme) {
333334
if (length(scheme) != 6) {
334-
stop("Custom color schemes must contain exactly 6 colors.",
335-
call. = FALSE)
335+
abort("Custom color schemes must contain exactly 6 colors.")
336336
}
337337

338338
not_found <- character(0)
@@ -343,16 +343,15 @@ prepare_custom_colors <- function(scheme) {
343343
}
344344
}
345345
if (length(not_found)) {
346-
stop(
346+
abort(paste(
347347
"Each color must specified as either a hexidecimal color value ",
348348
"(e.g. '#C79999') or the name of a color (e.g. 'blue'). ",
349-
"The following provided colors were not found: ",
350-
paste(unlist(not_found), collapse = ", "),
351-
call. = FALSE
352-
)
349+
"The following provided colors were not found:",
350+
paste(unlist(not_found), collapse = ", ")
351+
))
353352
}
354353

355-
x <- setNames(as.list(scheme), scheme_level_names())
354+
x <- set_names(as.list(scheme), scheme_level_names())
356355
attr(x, "scheme_name") <- "custom"
357356
x
358357
}

R/bayesplot-extractors.R

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ log_posterior.stanfit <- function(object, inc_warmup = FALSE, ...) {
8686
inc_warmup = inc_warmup,
8787
...)
8888
lp <- lapply(lp, as.array)
89-
lp <- setNames(reshape2::melt(lp),
90-
c("Iteration", "Value", "Chain"))
89+
lp <- set_names(reshape2::melt(lp), c("Iteration", "Value", "Chain"))
9190
validate_df_classes(lp, c("integer", "numeric", "integer"))
9291
}
9392

@@ -96,9 +95,7 @@ log_posterior.stanfit <- function(object, inc_warmup = FALSE, ...) {
9695
#' @method log_posterior stanreg
9796
#'
9897
log_posterior.stanreg <- function(object, inc_warmup = FALSE, ...) {
99-
log_posterior.stanfit(object$stanfit,
100-
inc_warmup = inc_warmup,
101-
...)
98+
log_posterior.stanfit(object$stanfit, inc_warmup = inc_warmup, ...)
10299
}
103100

104101

@@ -136,24 +133,27 @@ nuts_params.stanreg <-
136133
#' @export
137134
#' @method nuts_params list
138135
nuts_params.list <- function(object, pars = NULL, ...) {
139-
if (!all(sapply(object, is.matrix)))
140-
stop("All list elements should be matrices.")
136+
if (!all(sapply(object, is.matrix))) {
137+
abort("All list elements should be matrices.")
138+
}
141139

142140
dd <- lapply(object, dim)
143-
if (length(unique(dd)) != 1)
144-
stop("All matrices in the list must have the same dimensions.")
141+
if (length(unique(dd)) != 1) {
142+
abort("All matrices in the list must have the same dimensions.")
143+
}
145144

146145
nms <- lapply(object, colnames)
147-
if (length(unique(nms)) != 1)
148-
stop("All matrices in the list must have the same column names.")
146+
if (length(unique(nms)) != 1) {
147+
abort("All matrices in the list must have the same column names.")
148+
}
149149

150-
if (length(pars))
151-
object <- lapply(object, function(x)
152-
x[, pars, drop = FALSE])
150+
if (length(pars)) {
151+
object <- lapply(object, function(x) x[, pars, drop = FALSE])
152+
}
153153

154-
object <- setNames(reshape2::melt(object),
155-
c("Iteration", "Parameter", "Value", "Chain"))
156-
validate_df_classes(object, c("integer", "factor", "numeric", "integer"))
154+
out <- reshape2::melt(object)
155+
out <- set_names(out, c("Iteration", "Parameter", "Value", "Chain"))
156+
validate_df_classes(out, c("integer", "factor", "numeric", "integer"))
157157
}
158158

159159

@@ -220,7 +220,6 @@ neff_ratio.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
220220
if (!is.null(pars) || !is.null(regex_pars)) {
221221
return(ratio)
222222
}
223-
224223
ratio[!names(ratio) %in% c("mean_PPD", "log-posterior")]
225224
}
226225

@@ -240,9 +239,9 @@ validate_df_classes <- function(x, classes = character()) {
240239
ncol(x) == length(classes)
241240
)
242241
for (j in 1:ncol(x)) {
243-
if (!inherits(x[, j], classes[j]))
244-
stop(colnames(x)[j], " does not have class ", classes[j],
245-
call. = FALSE)
242+
if (!inherits(x[, j], classes[j])) {
243+
abort(paste0(colnames(x)[j], " does not have class ", classes[j]))
244+
}
246245
}
247-
return(x)
246+
x
248247
}

R/bayesplot-ggplot-themes.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ bayesplot_theme_get <- function() {
145145
bayesplot_theme_set <- function(new = theme_default()) {
146146
missing <- setdiff(names(ggplot2::theme_gray()), names(new))
147147
if (length(missing)) {
148-
warning("New theme missing the following elements: ",
149-
paste(missing, collapse = ", "), call. = FALSE)
148+
warn(paste(
149+
"New theme missing the following elements:",
150+
paste(missing, collapse = ", ")
151+
))
150152
}
151153

152154
old <- .bayesplot_theme_env$current

R/bayesplot-helpers.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ lbub <- function(p, med = TRUE) {
302302
# internal
303303
calc_v <- function(v, fun, fun_args, ...) {
304304
if (missing(v))
305-
stop("'v' can't be missing.", call. = FALSE)
305+
abort("'v' can't be missing.")
306306
if (missing(fun))
307307
return(v)
308308
f <- match.fun(fun)

R/bayesplot_grid.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ bayesplot_grid <-
8080
suggested_package("gridExtra")
8181
dots <- list(...)
8282
if (length(dots) && length(plots)) {
83-
stop("Arguments '...' and 'plots' can't both be specified.")
83+
abort("Arguments '...' and 'plots' can't both be specified.")
8484
} else if (length(plots)) {
8585
if (!is.list(plots) || !all_ggplot(plots))
86-
stop("'plots' must be a list of ggplot objects.")
86+
abort("'plots' must be a list of ggplot objects.")
8787
} else if (length(dots)) {
8888
if (!all_ggplot(dots))
89-
stop("All objects in '...' must be ggplot objects.")
89+
abort("All objects in '...' must be ggplot objects.")
9090
plots <- dots
9191
} else {
92-
stop("No plots specified.")
92+
abort("No plots specified.")
9393
}
9494

9595
if (length(titles)) {

0 commit comments

Comments
 (0)