Skip to content

Commit 3d9651e

Browse files
authored
Merge f9fe22e into 373fc08
2 parents 373fc08 + f9fe22e commit 3d9651e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

R/data.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,19 @@ process_data <- function(data, model_variables = NULL) {
186186
# generating a decimal point in write_stan_json
187187
if (data_variables[[var_name]]$type == "int"
188188
&& !is.integer(data[[var_name]])) {
189-
if (!isTRUE(all(is_wholenumber(data[[var_name]])))) {
190-
# Don't warn for NULL/NA, as different warnings are used for those
191-
if (!isTRUE(any(is.na(data[[var_name]])))) {
192-
warning("A non-integer value was supplied for '", var_name, "'!",
193-
" It will be truncated to an integer.", call. = FALSE)
189+
if (!is.factor(data[[var_name]])) {
190+
if (!isTRUE(all(is_wholenumber(data[[var_name]])))) {
191+
# Don't warn for NULL/NA, as different warnings are used for those
192+
if (!isTRUE(any(is.na(data[[var_name]])))) {
193+
warning("A non-integer value was supplied for '", var_name, "'!",
194+
" It will be truncated to an integer.", call. = FALSE)
195+
}
196+
} else {
197+
# Round before setting mode to integer to avoid floating point errors
198+
data[[var_name]] <- round(data[[var_name]])
194199
}
195-
mode(data[[var_name]]) <- "integer"
196-
} else {
197-
# Round before setting mode to integer to avoid floating point errors
198-
data[[var_name]] <- round(data[[var_name]])
199-
mode(data[[var_name]]) <- "integer"
200200
}
201+
mode(data[[var_name]]) <- "integer"
201202
}
202203
}
203204
}

tests/testthat/test-data.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ test_that("process_data warns on int coercion", {
383383
expect_no_warning(
384384
process_data(list(a = c(1, 2, 3)), model_variables = mod$variables())
385385
)
386+
expect_no_warning(
387+
process_data(list(a = factor(c("a", "b", "c"))), model_variables = mod$variables())
388+
)
386389
})
387390

388391
test_that("Floating-point differences do not cause truncation towards 0", {

0 commit comments

Comments
 (0)