From 5b7f8139fc44e2df3adfb05535fd5872d9e37218 Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 14 Dec 2020 11:39:57 -0700 Subject: [PATCH 1/4] allow empty data list fixes #401 --- R/data.R | 3 +++ tests/testthat/test-model-data.R | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/R/data.R b/R/data.R index 3e38be3a6..6fb7852d5 100644 --- a/R/data.R +++ b/R/data.R @@ -84,6 +84,9 @@ list_to_array <- function(x, name = NULL) { #' CmdStan, or a named list of \R objects to pass to [write_stan_json()]. #' @return Path to data file. process_data <- function(data) { + if (length(data) == 0) { + data <- NULL + } if (is.null(data)) { path <- data } else if (is.character(data)) { diff --git a/tests/testthat/test-model-data.R b/tests/testthat/test-model-data.R index b591efe12..6f81c28e1 100644 --- a/tests/testthat/test-model-data.R +++ b/tests/testthat/test-model-data.R @@ -46,3 +46,21 @@ test_that("error if data contains NA elements", { expect_error(mod$sample(data = data_list2), "Data includes NA values") expect_error(mod$sample(data = data_list3), "Data includes NA values") }) + +test_that("empty data list doesn't error if no data block", { + mod <- cmdstan_model(write_stan_file(" + parameters { + real x; + } + model { + x ~ normal(0, 1); + } + ")) + + expect_sample_output( + fit <- mod$sample(data = list(), chains = 1) + ) + + # would error if fitting failed + expect_silent(fit$draws()) +}) From 2378182967a6a5496a4f6049823ca7c43f13210a Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 14 Dec 2020 12:03:08 -0700 Subject: [PATCH 2/4] add test --- tests/testthat/test-model-data.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test-model-data.R b/tests/testthat/test-model-data.R index 6f81c28e1..adba5fdd5 100644 --- a/tests/testthat/test-model-data.R +++ b/tests/testthat/test-model-data.R @@ -47,6 +47,10 @@ test_that("error if data contains NA elements", { expect_error(mod$sample(data = data_list3), "Data includes NA values") }) +test_that("empty data list converted to NULL", { + expect_null(process_data(list())) +}) + test_that("empty data list doesn't error if no data block", { mod <- cmdstan_model(write_stan_file(" parameters { From e0d6ae0c00a8422bd63f92a0e0f709476e94a7e1 Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 14 Dec 2020 12:04:48 -0700 Subject: [PATCH 3/4] move test --- tests/testthat/test-data.R | 4 ++++ tests/testthat/test-model-data.R | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index 8fa0d3a0b..248421615 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -7,6 +7,10 @@ if (not_on_cran()) { fit_optimize <- testing_fit("bernoulli", method = "optimize", seed = 123) } +test_that("empty data list converted to NULL", { + expect_null(process_data(list())) +}) + test_that("NAs detected in data list", { expect_false(any_na_elements(list(y = 1))) expect_true(any_na_elements(list(y = 1, N = NA))) diff --git a/tests/testthat/test-model-data.R b/tests/testthat/test-model-data.R index adba5fdd5..6f81c28e1 100644 --- a/tests/testthat/test-model-data.R +++ b/tests/testthat/test-model-data.R @@ -47,10 +47,6 @@ test_that("error if data contains NA elements", { expect_error(mod$sample(data = data_list3), "Data includes NA values") }) -test_that("empty data list converted to NULL", { - expect_null(process_data(list())) -}) - test_that("empty data list doesn't error if no data block", { mod <- cmdstan_model(write_stan_file(" parameters { From cd5a74b29b605b63a138969f322fff7238a72793 Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 14 Dec 2020 12:33:37 -0700 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 50808d8f0..74d1142a0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,10 @@ * Fixed reading inverse mass matrix with values written in scientific format in the CSV. (#394) +* Fixed error caused by an empty data list. Previously if a model didn't require +data then `data` had to either be NULL or be a non-empty list, but now `list()` +is allowed. (#403) + ### New features * Added `$sample_mpi()` for MCMC sampling with MPI. (#350)