@@ -4,7 +4,9 @@ skip_if(os_is_wsl())
44set_cmdstan_path()
55mod <- cmdstan_model(testing_stan_file(" bernoulli_log_lik" ), force_recompile = TRUE )
66data_list <- testing_data(" bernoulli" )
7- fit <- mod $ sample(data = data_list , chains = 1 , refresh = 0 )
7+ utils :: capture.output(
8+ fit <- mod $ sample(data = data_list , chains = 1 , refresh = 0 )
9+ )
810
911test_that(" Model methods automatically initialise when needed" , {
1012 expect_no_error(fit $ log_prob(unconstrained_variables = c(0.1 )))
@@ -59,7 +61,9 @@ test_that("Model methods environments are independent", {
5961 data_list_2 <- data_list
6062 data_list_2 $ N <- 20
6163 data_list_2 $ y <- c(data_list $ y , data_list $ y )
62- fit_2 <- mod $ sample(data = data_list_2 , chains = 1 )
64+ utils :: capture.output(
65+ fit_2 <- mod $ sample(data = data_list_2 , chains = 1 )
66+ )
6367 fit_2 $ init_model_methods()
6468
6569 expect_equal(fit $ log_prob(unconstrained_variables = c(0.1 )), - 8.6327599208828509347 )
@@ -90,8 +94,10 @@ test_that("methods error for incorrect inputs", {
9094
9195 logistic_mod <- cmdstan_model(testing_stan_file(" logistic" ), force_recompile = TRUE )
9296 logistic_data_list <- testing_data(" logistic" )
93- logistic_fit <- logistic_mod $ sample(data = logistic_data_list , chains = 1 )
94- logistic_fit $ init_model_methods(verbose = TRUE )
97+ utils :: capture.output(
98+ logistic_fit <- logistic_mod $ sample(data = logistic_data_list , chains = 1 )
99+ )
100+ logistic_fit $ init_model_methods()
95101
96102 expect_error(
97103 logistic_fit $ unconstrain_variables(list (alpha = 0.5 )),
@@ -104,7 +110,9 @@ test_that("Methods error with already-compiled model", {
104110 precompile_mod <- testing_model(" bernoulli" )
105111 mod <- testing_model(" bernoulli" )
106112 data_list <- testing_data(" bernoulli" )
107- fit <- mod $ sample(data = data_list , chains = 1 )
113+ utils :: capture.output(
114+ fit <- mod $ sample(data = data_list , chains = 1 )
115+ )
108116 expect_error(
109117 fit $ init_model_methods(),
110118 " Model methods cannot be used with a pre-compiled Stan executable, the model must be compiled again" ,
@@ -116,7 +124,9 @@ test_that("Methods can be compiled with model", {
116124 mod <- cmdstan_model(testing_stan_file(" bernoulli" ),
117125 force_recompile = TRUE ,
118126 compile_model_methods = TRUE )
119- fit <- mod $ sample(data = data_list , chains = 1 )
127+ utils :: capture.output(
128+ fit <- mod $ sample(data = data_list , chains = 1 )
129+ )
120130
121131 lp <- fit $ log_prob(unconstrained_variables = c(0.6 ))
122132 expect_equal(lp , - 10.649855405830624733 )
@@ -156,7 +166,9 @@ test_that("unconstrain_variables correctly handles zero-length containers", {
156166 mod <- cmdstan_model(write_stan_file(model_code ),
157167 force_recompile = TRUE ,
158168 compile_model_methods = TRUE )
159- fit <- mod $ sample(data = list (N = 0 ), chains = 1 )
169+ utils :: capture.output(
170+ fit <- mod $ sample(data = list (N = 0 ), chains = 1 )
171+ )
160172 unconstrained <- fit $ unconstrain_variables(variables = list (x = 5 ))
161173 expect_equal(unconstrained , 5 )
162174})
@@ -179,21 +191,23 @@ test_that("unconstrain_draws returns correct values", {
179191 mod <- cmdstan_model(write_stan_file(model_code ),
180192 compile_model_methods = TRUE ,
181193 force_recompile = TRUE )
182- fit <- mod $ sample(data = list (N = 0 ), chains = 2 , save_warmup = TRUE )
183- fit_no_warmup <- mod $ sample(data = list (N = 0 ), chains = 2 )
194+ utils :: capture.output({
195+ fit <- mod $ sample(data = list (N = 0 ), chains = 2 , save_warmup = TRUE )
196+ fit_no_warmup <- mod $ sample(data = list (N = 0 ), chains = 2 )
197+ })
184198
185199 x_draws <- fit $ draws(format = " draws_df" )$ x
186200 x_draws_warmup <- fit $ draws(format = " draws_df" , inc_warmup = TRUE )$ x
187-
201+
188202 # Unconstrain all internal draws
189203 unconstrained_internal_draws <- fit $ unconstrain_draws()
190204 unconstrained_internal_draws_warmup <- fit $ unconstrain_draws(inc_warmup = TRUE )
191205 expect_equal(as.numeric(x_draws ), as.numeric(unconstrained_internal_draws ))
192206 expect_equal(as.numeric(x_draws_warmup ), as.numeric(unconstrained_internal_draws_warmup ))
193-
207+
194208 expect_error({unconstrained_internal_draws <- fit_no_warmup $ unconstrain_draws(inc_warmup = TRUE )},
195209 " Warmup draws were requested from a fit object without them! Please rerun the model with save_warmup = TRUE." )
196-
210+
197211 # Unconstrain external CmdStan CSV files
198212 unconstrained_csv <- fit $ unconstrain_draws(files = fit $ output_files())
199213 unconstrained_csv_warmup <- fit $ unconstrain_draws(files = fit $ output_files(),
@@ -204,7 +218,7 @@ test_that("unconstrain_draws returns correct values", {
204218 # Unconstrain existing draws object
205219 unconstrained_draws <- fit $ unconstrain_draws(draws = fit $ draws())
206220 expect_equal(as.numeric(x_draws ), as.numeric(unconstrained_draws ))
207-
221+
208222 expect_message(fit $ unconstrain_draws(draws = fit $ draws(), inc_warmup = TRUE ),
209223 " 'inc_warmup' cannot be used with a draws object. Ignoring." )
210224
@@ -224,7 +238,9 @@ test_that("unconstrain_draws returns correct values", {
224238 mod <- cmdstan_model(write_stan_file(model_code ),
225239 compile_model_methods = TRUE ,
226240 force_recompile = TRUE )
227- fit <- mod $ sample(data = list (N = 0 ), chains = 2 )
241+ utils :: capture.output(
242+ fit <- mod $ sample(data = list (N = 0 ), chains = 2 )
243+ )
228244
229245 x_draws <- fit $ draws(format = " draws_df" )$ x
230246
@@ -241,10 +257,13 @@ test_that("unconstrain_draws returns correct values", {
241257})
242258
243259test_that(" Model methods can be initialised for models with no data" , {
244-
245260 stan_file <- write_stan_file(" parameters { real x; } model { x ~ std_normal(); }" )
246261 mod <- cmdstan_model(stan_file , compile_model_methods = TRUE , force_recompile = TRUE )
247- expect_no_error(fit <- mod $ sample())
262+ expect_no_error(
263+ utils :: capture.output(
264+ fit <- mod $ sample()
265+ )
266+ )
248267 expect_equal(fit $ log_prob(5 ), - 12.5 )
249268})
250269
@@ -268,8 +287,10 @@ test_that("Variable skeleton returns correct dimensions for matrices", {
268287 force_recompile = TRUE )
269288 N <- 4
270289 K <- 3
271- fit <- mod $ sample(data = list (N = N , K = K ), chains = 1 ,
272- iter_warmup = 1 , iter_sampling = 5 )
290+ utils :: capture.output(
291+ fit <- mod $ sample(data = list (N = N , K = K ), chains = 1 ,
292+ iter_warmup = 1 , iter_sampling = 5 )
293+ )
273294
274295 target_skeleton <- list (
275296 x_real = array (0 , dim = 1 ),
0 commit comments