read_bds: Reads selected BDS data of a person

View source: R/read_bds.R

read_bdsR Documentation

Reads selected BDS data of a person

Description

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.

Usage

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

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".

Value

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

See Also

jsonlite::fromJSON(), centile::y2z()

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

# 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)

# 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

# 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)

growthcharts/bdsreader documentation built on Oct. 21, 2024, 4:39 a.m.