R6 class for a rendered component, created by
mighty_component$render() or get_rendered_component().
A rendered component can be:
Streamed into an R script with
$stream().Evaluated in an environment with
$eval().
Super class
mighty_component -> mighty_component_rendered
Methods
Inherited methods
mighty_component_rendered$new()
Create a rendered component from a rendered template. The code is validated. See the Validation section in mighty_component.
Usage
mighty_component_rendered$new(template, id)mighty_component_rendered$stream()
Write the code to an R script. The code is appended if the file exists.
mighty_component_rendered$eval()
Evaluate the code.
Usage
mighty_component_rendered$eval(envir = parent.frame())Examples
path <- system.file("examples", "ady.mustache", package = "mighty.component")
x <- get_component(path)$render(
domain = "adae",
variable = "ASTDY",
date = "ASTDT"
)
#> → Found "ady.mustache" in "local::/home/runner/work/_temp/Library/mighty.component/examples"
x$code
#> [1] "adae <- adae |>"
#> [2] " dplyr::mutate("
#> [3] " ASTDY = admiral::compute_duration("
#> [4] " start_date = TRTSDT,"
#> [5] " end_date = ASTDT,"
#> [6] " in_unit = 'days',"
#> [7] " out_unit = 'days',"
#> [8] " add_one = TRUE"
#> [9] " )"
#> [10] " )"
# Write the code to an R script
script <- tempfile(fileext = ".R")
x$stream(script)
readLines(script)
#> [1] "adae <- adae |>"
#> [2] " dplyr::mutate("
#> [3] " ASTDY = admiral::compute_duration("
#> [4] " start_date = TRTSDT,"
#> [5] " end_date = ASTDT,"
#> [6] " in_unit = 'days',"
#> [7] " out_unit = 'days',"
#> [8] " add_one = TRUE"
#> [9] " )"
#> [10] " )"
unlink(script)
# Evaluate the code in a new environment
env <- new.env()
env$adae <- data.frame(
TRTSDT = as.Date("2024-01-01"),
ASTDT = as.Date(c("2024-01-01", "2024-01-10"))
)
x$eval(envir = env)
#> → Evaluating component Analysis relative day
#> ℹ Code:
#> `adae <- adae |>`, ` dplyr::mutate(`, ` ASTDY = admiral::compute_duration(`, `
#> start_date = TRTSDT,`, ` end_date = ASTDT,`, ` in_unit = 'days',`, ` out_unit =
#> 'days',`, ` add_one = TRUE`, ` )`, and ` )`
env$adae
#> TRTSDT ASTDT ASTDY
#> 1 2024-01-01 2024-01-01 1
#> 2 2024-01-01 2024-01-10 10
