2222# ' * `table` -> `vector`, `matrix`, or `array` (depending on dimensions of table)
2323# '
2424# ' ### Factor conversion
25- # ' Factors are written as their level indices: the position of each value in
26- # ' `levels(x)` rather than the value itself. The default levels are the sorted
27- # ' unique values, so `factor(c(10, 9, 8))` has levels `8`, `9`, `10` and is
28- # ' written as `[3, 2, 1]`, and an unused level shifts the indices of the levels
29- # ' after it. If the original values are what you want, convert them first, e.g.
30- # ' with `as.numeric(as.character(x))`. The fitting methods of a model compiled
31- # ' from a Stan file error if a factor is supplied for a variable that is not
32- # ' declared as `int`, but `write_stan_json()` has no declarations to check
33- # ' against and so always converts.
34- # '
25+ # ' Factors are written as their level indices, i.e., the position of each value
26+ # ' in `levels(x)` rather than the value itself. The default levels are the
27+ # ' sorted unique values, e.g., `factor(c(10, 9, 8))` has levels `8`, `9`, `10`
28+ # ' and is written as `[3, 2, 1]`. An unused level shifts the indices of the
29+ # ' levels after it. The fitting methods of a model compiled from a Stan file
30+ # ' will error if a factor is supplied for a variable that is not declared as
31+ # ' `int`, but if `write_stan_json()` is called directly by the user it has no
32+ # ' declarations to check and so it always does the conversion.
3533# '
3634# ' ### List to array conversion
3735# ' The `list` to `array` conversion is intended to make it easier to prepare
@@ -118,7 +116,6 @@ write_stan_json <- function(data, file, always_decimal = FALSE) {
118116 }
119117 validate_data_type(var , var_name )
120118 var <- convert_to_array(var , var_name )
121- # after the conversion, so that NAs nested inside a list are also found
122119 if (anyNA(var )) {
123120 stop(" Variable '" , var_name , " ' has NA values." , call. = FALSE )
124121 }
@@ -167,9 +164,8 @@ convert_to_array <- function(var, var_name = NULL) {
167164 if (is.table(var )) {
168165 var <- unclass(var )
169166 } else if (is.data.frame(var )) {
170- # data.matrix() silently coerces character columns to factor codes and
171- # date/time columns to their numeric representation, so apply the same
172- # type check used for the variables themselves (#817)
167+ # first check all columns are valid types, so data.matrix() doesn't silently
168+ # coerce character columns to factor codes and date/time columns to numeric
173169 invalid <- ! vapply(var , is_valid_data_type , logical (1 ))
174170 if (any(invalid )) {
175171 stop(" Variable '" , var_name , " ' has columns of invalid type: " ,
@@ -179,7 +175,7 @@ convert_to_array <- function(var, var_name = NULL) {
179175 } else if (is.list(var )) {
180176 var <- list_to_array(var , var_name )
181177 }
182- # after the conversions above so that lists of logicals are also converted
178+ # after the conversions above so we also convert lists of logicals
183179 if (is.logical(var )) {
184180 mode(var ) <- " integer"
185181 }
0 commit comments