Skip to content

Commit 396f99f

Browse files
committed
use try() in is_chain_list() to avoid potential errors
if there's an error just return FALSE instead of erroring out
1 parent e812ff8 commit 396f99f

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!-- Items for next release go here* -->
88

99
* MCMC plots now also accept objects with an `as.array` method as
10-
input. (#175, #184)
10+
input (e.g., stanfit objects). (#175, #184)
1111

1212
* [`mcmc_trace()`](https://mc-stan.org/bayesplot/reference/MCMC-traces.html)
1313
gains an argument `iter1` which can be used to label the traceplot starting

R/helpers-mcmc.R

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ prepare_mcmc_array <- function(x,
1515
} else if (is.data.frame(x)) {
1616
# data frame without Chain column
1717
x <- as.matrix(x)
18-
} else if (!is.array(x)) {
18+
} else {
1919
x <- as.array(x)
2020
}
2121

2222
stopifnot(is.matrix(x) || is.array(x))
2323
if (is.array(x) && !(length(dim(x)) %in% c(2,3))) {
24-
stop("Arrays should have 2 or 3 dimensions. See help('MCMC-overview').")
24+
stop("Arrays should have 2 or 3 dimensions. See help('MCMC-overview').",
25+
call. = FALSE)
2526
}
2627
if (anyNA(x)) {
27-
stop("NAs not allowed in 'x'.")
28+
stop("NAs not allowed in 'x'.", call. = FALSE)
2829
}
2930

3031
parnames <- parameter_names(x)
@@ -172,7 +173,10 @@ df_with_chain2array <- function(x) {
172173
# @return TRUE or FALSE
173174
is_chain_list <- function(x) {
174175
check1 <- !is.data.frame(x) && is.list(x)
175-
dims <- sapply(x, function(chain) length(dim(chain)))
176+
dims <- try(sapply(x, function(chain) length(dim(chain))), silent=TRUE)
177+
if (inherits(dims, "try-error")) {
178+
return(FALSE)
179+
}
176180
check2 <- isTRUE(all(dims == 2)) # all elements of list should be matrices/2-D arrays
177181
check1 && check2
178182
}
@@ -193,7 +197,8 @@ validate_chain_list <- function(x) {
193197
n_iter <- sapply(x, nrow)
194198
same_iters <- length(unique(n_iter)) == 1
195199
if (!same_iters) {
196-
stop("Each chain should have the same number of iterations.")
200+
stop("Each chain should have the same number of iterations.",
201+
call. = FALSE)
197202
}
198203

199204
cnames <- sapply(x, colnames)
@@ -204,7 +209,7 @@ validate_chain_list <- function(x) {
204209
}
205210
if (!same_params) {
206211
stop("The parameters for each chain should be in the same order ",
207-
"and have the same names.")
212+
"and have the same names.", call. = FALSE)
208213
}
209214
}
210215

@@ -240,10 +245,10 @@ chain_list2array <- function(x) {
240245
parameter_names <- function(x) UseMethod("parameter_names")
241246
parameter_names.array <- function(x) {
242247
stopifnot(is_3d_array(x))
243-
dimnames(x)[[3]] %||% stop("No parameter names found.")
248+
dimnames(x)[[3]] %||% stop("No parameter names found.", call. = FALSE)
244249
}
245250
parameter_names.default <- function(x) {
246-
colnames(x) %||% stop("No parameter names found.")
251+
colnames(x) %||% stop("No parameter names found.", call. = FALSE)
247252
}
248253

249254
# Check if an object is a 3-D array
@@ -292,17 +297,18 @@ validate_transformations <-
292297
function(transformations = list(),
293298
pars = character()) {
294299
if (is.null(names(transformations))) {
295-
stop("'transformations' must be a _named_ list.")
300+
stop("'transformations' must be a _named_ list.", call. = FALSE)
296301
} else if (any(!nzchar(names(transformations)))) {
297-
stop("Each element of 'transformations' must have a name.")
302+
stop("Each element of 'transformations' must have a name.", call. = FALSE)
298303
}
299304

300305
transformations <- lapply(transformations, match.fun)
301306
if (!all(names(transformations) %in% pars)) {
302307
not_found <- which(!names(transformations) %in% pars)
303308
stop(
304309
"Some names(transformations) don't match parameter names: ",
305-
paste(names(transformations)[not_found], collapse = ", ")
310+
paste(names(transformations)[not_found], collapse = ", "),
311+
call. = FALSE
306312
)
307313
}
308314

0 commit comments

Comments
 (0)