Write data to a JSON file readable by CmdStan

`write_stan_json(data, file, always_decimal = FALSE)`

- data
(list) A named list of R objects.

- file
(string) The path to where the data file should be written.

- always_decimal
(logical) Force generate non-integers with decimal points to better distinguish between integers and floating point values. If

`TRUE`

all R objects in`data`

intended for integers must be of integer type.

`write_stan_json()`

performs several conversions before writing the JSON
file:

`logical`

->`integer`

(`TRUE`

->`1`

,`FALSE`

->`0`

)`data.frame`

->`matrix`

(via`data.matrix()`

)`list`

->`array`

`table`

->`vector`

,`matrix`

, or`array`

(depending on dimensions of table)

The `list`

to `array`

conversion is intended to make it easier to prepare
the data for certain Stan declarations involving arrays:

`vector[J] v[K]`

(or equivalently`array[K] vector[J] v`

as of Stan 2.27) can be constructed in R as a list with`K`

elements where each element a vector of length`J`

`matrix[I,J] v[K]`

(or equivalently`array[K] matrix[I,J] m`

as of Stan 2.27 ) can be constructed in R as a list with`K`

elements where each element an`IxJ`

matrix

These can also be passed in from R as arrays instead of lists but the list
option is provided for convenience. Unfortunately for arrays with more than
one dimension, e.g., `vector[J] v[K,L]`

(or equivalently
`array[K,L] vector[J] v `

as of Stan 2.27) it is not possible to use an R
list and an array must be used instead. For this example the array in R
should have dimensions `KxLxJ`

.

```
x <- matrix(rnorm(10), 5, 2)
y <- rpois(nrow(x), lambda = 10)
z <- c(TRUE, FALSE)
data <- list(N = nrow(x), K = ncol(x), x = x, y = y, z = z)
# write data to json file
file <- tempfile(fileext = ".json")
write_stan_json(data, file)
# check the contents of the file
cat(readLines(file), sep = "\n")
#> {
#> "N": 5,
#> "K": 2,
#> "x": [
#> [-1.3149626812135, -0.191761909283258],
#> [1.70603877388749, -0.282224274118901],
#> [0.177503683488102, -0.683561574477846],
#> [-0.393377531770713, 0.580194679530021],
#> [0.578306036031158, -1.06993557582488]
#> ],
#> "y": [10, 9, 8, 12, 11],
#> "z": [1, 0]
#> }
# demonstrating list to array conversion
# suppose x is declared as `vector[3] x[2]` (or equivalently `array[2] vector[3] x`)
# we can use a list of length 2 where each element is a vector of length 3
data <- list(x = list(1:3, 4:6))
file <- tempfile(fileext = ".json")
write_stan_json(data, file)
cat(readLines(file), sep = "\n")
#> {
#> "x": [
#> [1, 2, 3],
#> [4, 5, 6]
#> ]
#> }
```