Learn R Programming

pointblank (version 0.6.0)

yaml_write: Write an agent and informant to a pointblank YAML file

Description

With yaml_write() we can take an existing agent and write that agent's validation plan to a YAML file. With pointblank YAML, we can modify the YAML markup if so desired, or, use as is to create a new agent with the yaml_read_agent() function. That agent will have a validation plan and is ready to interrogate() the data. We can go a step further and perform an interrogation directly from the YAML file with the yaml_agent_interrogate() function. That returns an agent with intel (having already interrogated the target data table). An informant object can also be written to YAML with yaml_write().

One requirement for writing the agent to YAML is that we need to have a table-reading function (read_fn) specified (it's a function that is used to read the target table when interrogate() is called). This option can be set when using create_agent() or with set_read_fn() (for use with an existing agent).

Usage

yaml_write(agent = NULL, informant = NULL, filename, path = NULL)

Arguments

agent

An agent object of class ptblank_agent.

informant

An informant object of class ptblank_informant.

filename

The name of the YAML file to create on disk. It is recommended that either the .yaml or .yml extension be used for this file.

path

An optional path to which the YAML file should be saved (combined with filename).

Function ID

9-1

See Also

Other pointblank YAML: yaml_agent_interrogate(), yaml_agent_show_exprs(), yaml_agent_string(), yaml_informant_incorporate(), yaml_read_agent(), yaml_read_informant()

Examples

Run this code
# NOT RUN {
# Let's go through the process of
# developing an agent with a validation
# plan (to be used for the data quality
# analysis of the `small_table` dataset),
# and then offloading that validation
# plan to a pointblank YAML file

# We ought to think about what's
# tolerable in terms of data quality so
# let's designate proportional failure
# thresholds to the `warn`, `stop`, and
# `notify` states using `action_levels()`
al <- 
  action_levels(
    warn_at = 0.10,
    stop_at = 0.25,
    notify_at = 0.35
  )

# Now create a pointblank `agent` object
# and give it the `al` object (which
# serves as a default for all validation
# steps which can be overridden); the
# data will be referenced in a `read_fn`
# (a requirement for writing to YAML)
agent <- 
  create_agent(
    read_fn = ~small_table,
    label = "A simple example with the `small_table`.",
    actions = al
  )

# Then, as with any `agent` object, we
# can add steps to the validation plan by
# using as many validation functions as we
# want
agent <-
  agent %>% 
  col_exists(vars(date, date_time)) %>%
  col_vals_regex(
    vars(b), "[0-9]-[a-z]{3}-[0-9]{3}"
  ) %>%
  rows_distinct() %>%
  col_vals_gt(vars(d), 100) %>%
  col_vals_lte(vars(c), 5)

# The agent can be written to a pointblank
# YAML file with `yaml_write()`
# yaml_write(
#   agent = agent,
#   filename = "agent-small_table.yml"
# )

# The 'agent-small_table.yml' file is
# available in the package through
# `system.file()`
yml_file <- 
  system.file(
    "agent-small_table.yml",
    package = "pointblank"
  )

# We can view the YAML file in the console
# with the `yaml_agent_string()` function
yaml_agent_string(path = yml_file)

# The YAML can also be printed in the console
# by supplying the agent as the input
yaml_agent_string(agent = agent)

# At a later time, the YAML file can
# be read into a new agent with the
# `yaml_read_agent()` function
agent <- 
  yaml_read_agent(path = yml_file)

class(agent)

# We can interrogate the data (which
# is accessible through the `read_fn`)
# with `interrogate()` and get an
# agent with intel, or, we can
# interrogate directly from the YAML
# file with `yaml_agent_interrogate()`
agent <- 
  yaml_agent_interrogate(path = yml_file)

class(agent)

# }

Run the code above in your browser using DataLab