Skip to content

Commit be34adb

Browse files
authored
Merge pull request #453 from stan-dev/minor_fixes
Fix issue with reading in CSV for models with spaces in name
2 parents 3b2cfca + d2862ad commit be34adb

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Bug fixes
44

5+
* Fixed issue with retrieving draws with models with spaces in their names. (#453)
6+
57
### New features
68

79
* New function `as_cmdstan_fit()` that creates CmdStanMCMC/MLE/VB objects

R/csv.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ read_cmdstan_csv <- function(files,
223223
if (length(col_select) > 0) {
224224
if (os_is_windows()) {
225225
grep_path <- repair_path(Sys.which("grep.exe"))
226-
fread_cmd <- paste0(grep_path, " -v '^#' --color=never ", output_file)
226+
fread_cmd <- paste0(grep_path, " -v '^#' --color=never '", output_file, "'")
227227
} else {
228-
fread_cmd <- paste0("grep -v '^#' --color=never ", output_file)
228+
fread_cmd <- paste0("grep -v '^#' --color=never '", output_file, "'")
229229
}
230230
suppressWarnings(
231231
draws <- data.table::fread(
@@ -512,9 +512,9 @@ read_csv_metadata <- function(csv_file) {
512512
total_time <- 0
513513
if (os_is_windows()) {
514514
grep_path <- repair_path(Sys.which("grep.exe"))
515-
fread_cmd <- paste0(grep_path, " '^[#a-zA-Z]' --color=never ", csv_file)
515+
fread_cmd <- paste0(grep_path, " '^[#a-zA-Z]' --color=never '", csv_file, "'")
516516
} else {
517-
fread_cmd <- paste0("grep '^[#a-zA-Z]' --color=never ", csv_file)
517+
fread_cmd <- paste0("grep '^[#a-zA-Z]' --color=never '", csv_file, "'")
518518
}
519519
suppressWarnings(
520520
metadata <- data.table::fread(

tests/testthat/test-fit-shared.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,38 @@ test_that("sig_figs works with all methods", {
399399
c(0.12, 0.12345, 0.123456789)
400400
)
401401
})
402+
403+
test_that("draws are returned for model with spaces", {
404+
skip_on_cran()
405+
m <- "
406+
parameters {
407+
real y;
408+
}
409+
model {
410+
y ~ std_normal();
411+
}
412+
generated quantities {
413+
real two_y = 2 * y;
414+
}
415+
"
416+
f <- write_stan_file(m, basename = "file with spaces")
417+
expect_equal(basename(f), "file with spaces.stan")
418+
mod <- cmdstan_model(f)
419+
utils::capture.output(
420+
fit_sample <- mod$sample(seed = 123, iter_sampling = 1000, chains = 1)
421+
)
422+
expect_equal(dim(fit_sample$draws()), c(1000, 1, 3))
423+
utils::capture.output(
424+
fit <- mod$variational(seed = 123, output_samples = 1000)
425+
)
426+
expect_equal(dim(fit$draws()), c(1000, 4))
427+
utils::capture.output(
428+
fit <- mod$optimize(seed = 123)
429+
)
430+
expect_equal(length(fit$mle()), 2)
431+
432+
utils::capture.output(
433+
fit <- mod$generate_quantities(fitted_params = fit_sample, seed = 123)
434+
)
435+
expect_equal(dim(fit$draws()), c(1000, 1, 1))
436+
})

tests/testthat/test-model-compile.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ if (not_on_cran()) {
66
mod <- cmdstan_model(stan_file = stan_program, compile = FALSE)
77
}
88

9-
109
test_that("object initialized correctly", {
1110
skip_on_cran()
1211
expect_equal(mod$stan_file(), stan_program)
@@ -399,4 +398,25 @@ test_that("check_syntax() works with include_paths", {
399398

400399
})
401400

401+
test_that("check_syntax() works with pedantic=TRUE", {
402+
skip_on_cran()
403+
model_code <- "
404+
transformed data {
405+
real a;
406+
a <- 3;
407+
}
408+
"
409+
stan_file <- write_stan_file(model_code)
410+
mod_dep_warning <- cmdstan_model(stan_file, compile = FALSE)
411+
expect_message(
412+
mod_dep_warning$compile(),
413+
"Warning: deprecated language construct used in",
414+
fixed = TRUE
415+
)
416+
expect_message(
417+
mod_dep_warning$check_syntax(),
418+
"Warning: deprecated language construct used in",
419+
fixed = TRUE
420+
)
421+
})
402422

0 commit comments

Comments
 (0)