Write a tasks <config>
class object to a tasks.json
JSON file.
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 tohub-config/tasks.json
withinhub_path
. If specified, overrideshub_path
.- overwrite
Logical. Whether to overwrite the file if it already exists.
- silent
Logical. Whether to suppress informational messages.
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
instancePath
schemaPath
keyword
message
schema
data
For more information, please consult the
hubDocs
documentation.
# Clean up
unlink(temp_hub)