Skip to contents

Write a tasks <config> class object to a tasks.json JSON file.

Usage

write_config(
  config,
  hub_path = ".",
  config_path = NULL,
  overwrite = FALSE,
  silent = FALSE
)

Arguments

config

Object of class <config> to write to a JSON file.

hub_path

Path to the hub directory. Defaults to the current working directory. Ignored if config_path is specified.

config_path

Path to write the config object to. If NULL defaults to hub-config/tasks.json within hub_path. If specified, overrides hub_path.

overwrite

Logical. Whether to overwrite the file if it already exists.

silent

Logical. Whether to suppress informational messages.

Value

TRUE invisibly.

Details

! WARNING: Due to inconsistencies between R and JSON data types, in particular the fact that R has no concept of a scalar, some properties in the output file may not conform to schema expectations. They might be an <array> when a <scalar> is required or vice versa. validate_config() can be used to validate JSON config files and identify any deviations. Note also that these errors are introduced every time a JSON file is written from an R object. That includes when reading in a valid JSON config file and writing it back out. For more information, see the hubverse schema documentation

Examples

rounds <- create_rounds(
  create_round(
    round_id_from_variable = TRUE,
    round_id = "origin_date",
    model_tasks = create_model_tasks(
      create_model_task(
        task_ids = create_task_ids(
          create_task_id("origin_date",
            required = NULL,
            optional = c(
              "2023-01-02",
              "2023-01-09",
              "2023-01-16"
            )
          ),
          create_task_id("location",
            required = "US",
            optional = c("01", "02", "04", "05", "06")
          ),
          create_task_id("horizon",
            required = 1L,
            optional = 2:4
          )
        ),
        output_type = create_output_type(
          create_output_type_mean(
            is_required = TRUE,
            value_type = "double",
            value_minimum = 0L
          )
        ),
        target_metadata = create_target_metadata(
          create_target_metadata_item(
            target_id = "inc hosp",
            target_name = "Weekly incident influenza hospitalizations",
            target_units = "rate per 100,000 population",
            target_keys = NULL,
            target_type = "discrete",
            is_step_ahead = TRUE,
            time_unit = "week"
          )
        )
      )
    ),
    submissions_due = list(
      relative_to = "origin_date",
      start = -4L,
      end = 2L
    )
  )
)
# Create config object
config <- create_config(rounds)
# Create temporary hub
temp_hub <- tempdir()
dir.create(file.path(temp_hub, "hub-config"))
# Write config
write_config(config, hub_path = temp_hub)
#>  `config` written out successfully.
#> ! Due to inconsistencies between R and JSON data types, some properties in the
#>   output file may not conform to schema expectations. They might be an <array>
#>   when a <scalar> is required or vice versa.
#>  Please validate the file with `validate_config()` to identify any deviations.
cat(readLines(file.path(temp_hub, "hub-config/tasks.json")), sep = "\n")
#> {
#>   "schema_version": "https://raw.githubusercontent.com/hubverse-org/schemas/main/v3.0.1/tasks-schema.json",
#>   "rounds": [
#>     {
#>       "round_id_from_variable": true,
#>       "round_id": "origin_date",
#>       "model_tasks": [
#>         {
#>           "task_ids": {
#>             "origin_date": {
#>               "required": null,
#>               "optional": ["2023-01-02", "2023-01-09", "2023-01-16"]
#>             },
#>             "location": {
#>               "required": "US",
#>               "optional": ["01", "02", "04", "05", "06"]
#>             },
#>             "horizon": {
#>               "required": 1,
#>               "optional": [2, 3, 4]
#>             }
#>           },
#>           "output_type": {
#>             "mean": {
#>               "output_type_id": {
#>                 "required": "NA",
#>                 "optional": null
#>               },
#>               "value": {
#>                 "type": "double",
#>                 "minimum": 0
#>               }
#>             }
#>           },
#>           "target_metadata": [
#>             {
#>               "target_id": "inc hosp",
#>               "target_name": "Weekly incident influenza hospitalizations",
#>               "target_units": "rate per 100,000 population",
#>               "target_keys": null,
#>               "target_type": "discrete",
#>               "is_step_ahead": true,
#>               "time_unit": "week"
#>             }
#>           ]
#>         }
#>       ],
#>       "submissions_due": {
#>         "relative_to": "origin_date",
#>         "start": -4,
#>         "end": 2
#>       }
#>     }
#>   ]
#> }
# Validate config
if (curl::has_internet()) {
  v <- validate_config(hub_path = temp_hub)
  print(v)
  view_config_val_errors(v)
}
#> [1] FALSE
#> ! 6 schema errors: hub-config/tasks.json
#>   (<file:///tmp/RtmpH11xdr/hub-config/tasks.json>) (via tasks-schema v3.0.1
#>   (<https://raw.githubusercontent.com/hubverse-org/schemas/main/v3.0.1/tasks-schema.json>))
#>  use `view_config_val_errors()` to view table of error details.
hubUtils config validation error report
Report for file /tmp/RtmpH11xdr/hub-config/tasks.json using schema version v3.0.1
Error location Schema details Config
instancePath schemaPath keyword message schema data
rounds1 └─model_tasks └──1 └───task_ids └────location └─────required properties └rounds └─items └──properties └───model_tasks └────items └─────properties └──────task_ids └───────properties └────────location └─────────properties └──────────required └───────────type type ❌ must be array,null array, null US
rounds1 └─model_tasks └──1 └───task_ids └────horizon └─────required properties └rounds └─items └──properties └───model_tasks └────items └─────properties └──────task_ids └───────properties └────────horizon └─────────properties └──────────required └───────────type type ❌ must be array,null array, null 1
rounds1 └─model_tasks └──1 └───output_type └────mean └─────output_type_id properties └rounds └─items └──properties └───model_tasks └────items └─────properties └──────output_type └───────properties └────────mean └─────────properties └──────────output_type_id └───────────oneOf oneOf ❌ must match exactly one schema in oneOf 1 required-description: When mean is required, property set to single element ‘NA’ array required-type: array required-items-const:‘NA’ required-items-maxItems: 1 optional-description: When mean is required, property set to null optional-type: null

2 required-description: When mean is optional, property set to null required-type: null optional-description: When mean is optional, property set to single element ‘NA’ array optional-type: array optional-items-const:‘NA’ optional-items-maxItems: 1

required: NA
For more information, please consult the hubDocs documentation.
# Clean up unlink(temp_hub)