Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ write_stan_json <- function(data, file, always_decimal = FALSE) {

for (var_name in data_names) {
var <- data[[var_name]]
if (is.null(var)) {
stop("Variable '", var_name, "' is NULL.", call. = FALSE)
}
if (!(is.numeric(var) || is.factor(var) || is.logical(var) ||
is.data.frame(var) || is.list(var))) {
stop("Variable '", var_name, "' is of invalid type.", call. = FALSE)
Expand Down Expand Up @@ -176,7 +179,10 @@ process_data <- function(data, model_variables = NULL) {
call. = FALSE
)
}
for(var_name in names(data_variables)) {
for (var_name in names(data_variables)) {
if (is.null(data[[var_name]])) {
stop("Variable '", var_name, "' is NULL.", call. = FALSE)
}
# distinguish between scalars and arrays/vectors of length 1
if (length(data[[var_name]]) == 1
&& data_variables[[var_name]]$dimensions == 1) {
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ test_that("process_data works for inputs of length one", {
expect_equal(jsonlite::read_json(process_data(data, model_variables = mod$variables())), list(val = list(5)))
})

test_that("process_data errors on NULL data variables", {
stan_file <- write_stan_file("
data {
int N;
}
")
mod <- cmdstan_model(stan_file, compile = FALSE)
expect_error(
process_data(list(N = NULL), model_variables = mod$variables()),
"Variable 'N' is NULL"
)
})

test_that("process_fitted_params() works with basic input types", {
temp_file <- tempfile()
temp_files <- c(tempfile(),
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-json.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ test_that("write_stan_json errors if NAs", {
)
})

test_that("write_stan_json errors if NULL variables", {
expect_error(
write_stan_json(list(N = NULL), tempfile()),
"Variable 'N' is NULL"
)
})

test_that("write_stan_json() errors if data is not a list", {
expect_error(
write_stan_json(1:10),
Expand Down