From 4845369cdf66cdfded08f38f669a75a40b1000e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rok=20=C4=8Ce=C5=A1novar?= Date: Thu, 16 Jan 2020 14:09:31 +0100 Subject: [PATCH 1/3] Revert "Switch back to using Rdata temporarily while issue with 0x0s gets resolved for JSON" --- DESCRIPTION | 2 +- R/model.R | 4 ++-- tests/testthat/test-fit-shared.R | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6299c0823..8092c55ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,12 +24,12 @@ Imports: checkmate, posterior (>= 0.0.1), processx, - rstan, R6 Suggests: jsonlite (>= 1.2.0), knitr, rmarkdown, + rstan, testthat (>= 2.1.0) VignetteBuilder: knitr Remotes: diff --git a/R/model.R b/R/model.R index 9d9f12005..6ba91ab3c 100644 --- a/R/model.R +++ b/R/model.R @@ -664,8 +664,8 @@ process_data <- function(data) { } else if (is.character(data)) { path <- absolute_path(data) } else if (is.list(data) && !is.data.frame(data)) { - path <- tempfile(pattern = "standata-", fileext = ".dat") - rstan::stan_rdump(names(data), file = path, env = list2env(data)) + path <- tempfile(pattern = "standata-", fileext = ".json") + write_stan_json(data = data, file = path) } else { stop("'data' should be a path or a named list.", call. = FALSE) } diff --git a/tests/testthat/test-fit-shared.R b/tests/testthat/test-fit-shared.R index 8d6ba591e..67c301696 100644 --- a/tests/testthat/test-fit-shared.R +++ b/tests/testthat/test-fit-shared.R @@ -96,16 +96,16 @@ test_that("saving data file works", { for (method in all_methods) { fit <- fits[[method]] old_path <- fit$data_file() - checkmate::expect_file_exists(old_path, extension = "dat") + checkmate::expect_file_exists(old_path, extension = "json") expect_message( path <- fit$save_data_file(tempdir(), basename = NULL, timestamp = FALSE, random = FALSE), "Moved data file and set internal path" ) - checkmate::expect_file_exists(path, extension = "dat") + checkmate::expect_file_exists(path, extension = "json") expect_true(file.size(path) > 0) - expect_equal(basename(path), "logistic.dat") + expect_equal(basename(path), "logistic.json") expect_false(file.exists(old_path)) expect_equal(fit$data_file(), path) From f2f4843ae9b91377a4cc2b876db534e9536afe83 Mon Sep 17 00:00:00 2001 From: rok-cesnovar Date: Mon, 3 Feb 2020 09:58:50 +0100 Subject: [PATCH 2/3] use json by default only if version of cmdstan is 2.22+ --- R/model.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/model.R b/R/model.R index 6ba91ab3c..e7859a1c6 100644 --- a/R/model.R +++ b/R/model.R @@ -664,8 +664,13 @@ process_data <- function(data) { } else if (is.character(data)) { path <- absolute_path(data) } else if (is.list(data) && !is.data.frame(data)) { - path <- tempfile(pattern = "standata-", fileext = ".json") - write_stan_json(data = data, file = path) + if (cmdstan_version() >= "2.22") { + path <- tempfile(pattern = "standata-", fileext = ".json") + write_stan_json(data = data, file = path) + } else { + path <- tempfile(pattern = "standata-", fileext = ".dat") + rstan::stan_rdump(names(data), file = path, env = list2env(data)) + } } else { stop("'data' should be a path or a named list.", call. = FALSE) } From 856285ebb370ecc37f25584dde805328a79a87e9 Mon Sep 17 00:00:00 2001 From: rok-cesnovar Date: Mon, 17 Feb 2020 20:06:07 +0100 Subject: [PATCH 3/3] add rstan check before using stan_rdump --- R/model.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/model.R b/R/model.R index e7859a1c6..4f9ca3f36 100644 --- a/R/model.R +++ b/R/model.R @@ -669,6 +669,9 @@ process_data <- function(data) { write_stan_json(data = data, file = path) } else { path <- tempfile(pattern = "standata-", fileext = ".dat") + if (!requireNamespace("rstan", quietly = TRUE)) { + stop("For CmdStan < 2.22 the rstan package is required for writing data.") + } rstan::stan_rdump(names(data), file = path, env = list2env(data)) } } else {