This function takes data from a json source, optionally validates the contents against a JSON validation schema, perform checks, calculates the D-score, calculates Z-scores and stores the data in an list with elements psn and xyz.

read_bds(
  txt = NULL,
  auto_format = TRUE,
  format = "1.0",
  schema = NULL,
  validate = FALSE,
  append_ddi = FALSE,
  intermediate = FALSE,
  verbose = FALSE,
  ...
)

Arguments

txt

A JSON string, URL or file

auto_format

Logical. Should the format be read from the data? Default is TRUE.

format

String. JSON data schema version number. There are currently three schemas supported: "1.0", "1.1", "2.0" and "3.0". Formats "1.0" and "1.1" are included for backward compatibility only. Use format = "3.0" for new applications.

schema

A file name (optionally including the path) with the JSON validation schema. The schema argument overrides format. The function extracts the version number for the basename, and overwrites the format argument by version number.

validate

Logical. Should the JSON-input be validated against the JSON-schema? The default (FALSE) bypasses checking. Set validate = TRUE to obtain diagnostic information from the jsonvalidate::json_validate() function.

append_ddi

Should the DDI responses be appended? (only used for JSON schema V1.0 and V2.0)

intermediate

Logical. If TRUE the function writes JSON files with intermediate result to the working directory.

  1. input.json: the JSON input data;

  2. bds.json: a data frame with info per BDS;

  3. ddi.json: result of recoding BDS into GSED item names;

  4. psn.json: known fixed child covariates;

  5. xy.json: time-varying variables.

verbose

Show verbose output for centile::y2z()

...

Ignored

Value

A list with elements named "psn" and "xyz".

Details

If txt is unspecified or NULL, then the return component will "xyz" have zero rows.

The format and schema arguments specify the format of the JSON input data argument txt. The default format = "1.0" expects that the JSON input data conform to the schema specified in system.file("schemas/bds_v1.0.json", package = "bdsreader"). This is only supported for legacy. We recommend format "3.0", which expects data coded according to system.file("schemas/bds_v3.0.json", package = "bdsreader").

The format can be specified in the JSON data file with an entry named Format. For auto_format == TRUE, the data specification overrides any format and schema arguments to the read_bds() function. Schema bds_v3.0.json requires the Format field, so the correct format is automatically set by the data.

Legacy note: If you erroneously read a JSON file of format "1.0" using format "2.0" you may see an error: Error ...: incorrect number of dimensions. In that make sure that you are reading with the format = "1.0" argument. Reversely, if you erroneously read a JSON file of format "2.0" using format "1.0" you may see messages .ClientGegevens should be object and Missing 'ClientGegevens$Groepen'. In that case, specify format = "2.0".

Examples

fn <- system.file("examples/maria1.json", package = "bdsreader")
m <- read_bds(fn)
fn <- system.file("examples/test.json", package = "bdsreader")
m <- read_bds(fn)

# Assume that jamesdemodata is installed locally.
# If not use remotes::install_github("growthcharts/jamesdemodata")

# Read file with input data according to format "3.0"
data2 <- system.file("extdata/bds_v3.0/smocc/Laura_S.json",
  package = "jamesdemodata")
q <- read_bds(data2)
q
#> $psn
#> # A tibble: 1 × 22
#>      id name    dob        dobm       dobf   src   dnr   sex      gad    ga
#>   <int> <chr>   <date>     <date>     <date> <chr> <chr> <chr>  <dbl> <dbl>
#> 1    -1 Laura S 1989-01-21 1961-07-22 NA     0     NA    female   276    39
#> # ℹ 12 more variables: smo <dbl>, bw <dbl>, hgtm <dbl>, hgtf <dbl>, agem <dbl>,
#> #   etn <chr>, pc4 <chr>, blbf <int>, blbm <int>, eduf <int>, edum <int>,
#> #   par <int>
#> 
#> $xyz
#> # A tibble: 58 × 8
#>      age xname yname zname zref                      x     y      z
#>    <dbl> <chr> <chr> <chr> <chr>                 <dbl> <dbl>  <dbl>
#>  1 0     age   hgt   hgt_z nl_1997_hgt_female_nl 0      48   -1.52 
#>  2 0.101 age   hgt   hgt_z nl_1997_hgt_female_nl 0.101  53.5 -0.499
#>  3 0.159 age   hgt   hgt_z nl_1997_hgt_female_nl 0.159  56   -0.261
#>  4 0.236 age   hgt   hgt_z nl_1997_hgt_female_nl 0.236  59.5  0.163
#>  5 0.485 age   hgt   hgt_z nl_1997_hgt_female_nl 0.485  65.5 -0.259
#>  6 0.753 age   hgt   hgt_z nl_1997_hgt_female_nl 0.753  71.5  0.131
#>  7 1.02  age   hgt   hgt_z nl_1997_hgt_female_nl 1.02   75   -0.18 
#>  8 1.25  age   hgt   hgt_z nl_1997_hgt_female_nl 1.25   80    0.421
#>  9 1.54  age   hgt   hgt_z nl_1997_hgt_female_nl 1.54   84    0.527
#> 10 2.04  age   hgt   hgt_z nl_1997_hgt_female_nl 2.04   90    0.67 
#> # ℹ 48 more rows
#> 

# Equivalent, but specifying the built-in schema file bds_v3.0.json
schema2 <- system.file("schemas/bds_v3.0.json", package = "bdsreader")
r <- read_bds(data2, schema = schema2)
identical(q, r)
#> [1] TRUE

# Automatic detection of format 3.0
# s <- read_bds(data2)
# identical(q, s)

# Reading data with older format (bds_v1.0)
data1 <- system.file("extdata/bds_v1.0/smocc/Laura_S.json",
  package = "jamesdemodata")
t <- read_bds(data1)
t
#> $psn
#> # A tibble: 1 × 16
#>      id name    dob        dobf   dobm       src   dnr   sex      gad    ga
#>   <int> <chr>   <date>     <date> <date>     <chr> <chr> <chr>  <dbl> <dbl>
#> 1    -1 Laura S 1989-01-21 NA     1961-07-22 0     NA    female   276    39
#> # ℹ 6 more variables: smo <int>, bw <dbl>, hgtm <dbl>, hgtf <dbl>, agem <dbl>,
#> #   etn <chr>
#> 
#> $xyz
#> # A tibble: 58 × 8
#>      age xname yname zname zref                      x     y      z
#>    <dbl> <chr> <chr> <chr> <chr>                 <dbl> <dbl>  <dbl>
#>  1 0     age   hgt   hgt_z nl_1997_hgt_female_nl 0      48   -1.52 
#>  2 0.101 age   hgt   hgt_z nl_1997_hgt_female_nl 0.101  53.5 -0.499
#>  3 0.159 age   hgt   hgt_z nl_1997_hgt_female_nl 0.159  56   -0.261
#>  4 0.236 age   hgt   hgt_z nl_1997_hgt_female_nl 0.236  59.5  0.163
#>  5 0.485 age   hgt   hgt_z nl_1997_hgt_female_nl 0.485  65.5 -0.259
#>  6 0.753 age   hgt   hgt_z nl_1997_hgt_female_nl 0.753  71.5  0.131
#>  7 1.02  age   hgt   hgt_z nl_1997_hgt_female_nl 1.02   75   -0.18 
#>  8 1.25  age   hgt   hgt_z nl_1997_hgt_female_nl 1.25   80    0.421
#>  9 1.54  age   hgt   hgt_z nl_1997_hgt_female_nl 1.54   84    0.527
#> 10 2.04  age   hgt   hgt_z nl_1997_hgt_female_nl 2.04   90    0.67 
#> # ℹ 48 more rows
#> 

# same, but using a built-in schema file
schema1 <- system.file("schemas/bds_v1.0.json", package = "bdsreader")
u <- read_bds(data1, schema = schema1)
identical(t, u)
#> [1] TRUE