Skip to contents

construct() builds the code to reproduce one object, construct_multi() builds the code to reproduce objects stored in a named list or environment.

Usage

construct(
  x,
  ...,
  data = NULL,
  pipe = NULL,
  check = NULL,
  compare = compare_options(),
  one_liner = FALSE,
  template = getOption("constructive_opts_template")
)

construct_multi(
  x,
  ...,
  data = NULL,
  pipe = NULL,
  check = NULL,
  compare = compare_options(),
  one_liner = FALSE,
  template = getOption("constructive_opts_template")
)

Arguments

x

An object, for construct_multi() a named list or an environment.

...

Constructive options built with the opts_*() family of functions. See the "Constructive options" section below.

data

Named list or environment of objects we want to detect and mention by name (as opposed to deparsing them further). Can also contain unnamed nested lists, environments, or package names, in the latter case package exports and datasets will be considered. In case of conflict, the last provided name is considered.

pipe

Which pipe to use, either "base" or "magrittr". Defaults to "base" for R >= 4.2, otherwise to "magrittr".

check

Boolean. Whether to check if the created code reproduces the object using waldo::compare().

compare

Parameters passed to waldo::compare(), built with compare_options().

one_liner

Boolean. Whether to collapse the output to a single line of code.

template

A list of constructive options built with opts_*() functions, they will be overriden by .... Use it to set a default behavior for {constructive}.

Value

An object of class 'constructive'.

Details

construct_multi() recognizes promises, this means that for instance construct_multi(environment()) can be called in a function and will construct unevaluated arguments using delayedAssign(). Note however that construct_multi(environment()) is equivalent to construct_reprex() called without argument and the latter is preferred.

Constructive options

Constructive options provide a way to customize the output of `construct()`. We can provide calls to `opts_*()` functions to the `...` argument. Each of these functions targets a specific type or class and is documented on its own page.

  • opts_array(constructor = c("array", "next"), ...)

  • opts_AsIs(constructor = c("I", "next", "atomic"), ...)

  • opts_atomic(..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE, unicode_representation = c("ascii", "latin", "character", "unicode"), escape = FALSE)

  • opts_classGeneratorFunction(constructor = c("setClass"), ...)

  • opts_classPrototypeDef(constructor = c("prototype"), ...)

  • opts_classRepresentation(constructor = c("getClassDef"), ...)

  • opts_constructive_options(constructor = c("opts", "next"), ...)

  • opts_data.frame(constructor = c("data.frame", "read.table", "next", "list"), ...)

  • opts_data.table(constructor = c("data.table", "next", "list"), ..., selfref = FALSE)

  • opts_Date(constructor = c("as.Date", "as_date", "date", "new_date", "as.Date.numeric", "as_date.numeric", "next", "atomic"), ..., origin = "1970-01-01")

  • opts_dm(constructor = c("dm", "next", "list"), ...)

  • opts_dots(constructor = c("default"), ...)

  • opts_environment(constructor = c(".env", "list2env", "as.environment", "new.env", "topenv", "new_environment"), ..., recurse = FALSE, predefine = FALSE)

  • opts_externalptr(constructor = c("default"), ...)

  • opts_factor(constructor = c("factor", "as_factor", "new_factor", "next", "atomic"), ...)

  • opts_formula(constructor = c("~", "formula", "as.formula", "new_formula", "next"), ..., environment = TRUE)

  • opts_function(constructor = c("function", "as.function", "new_function"), ..., environment = TRUE, srcref = FALSE, trim = NULL)

  • opts_grouped_df(constructor = c("default", "next", "list"), ...)

  • opts_language(constructor = c("default"), ...)

  • opts_Layer(constructor = c("default", "layer", "environment"), ...)

  • opts_list(constructor = c("list", "list2"), ..., trim = NULL, fill = c("vector", "new_list", "+", "...", "none"))

  • opts_matrix(constructor = c("matrix", "array", "next", "atomic"), ...)

  • opts_mts(constructor = c("ts", "next", "atomic"), ...)

  • opts_numeric_version(constructor = c("numeric_version", "next", "atomic"), ...)

  • opts_ordered(constructor = c("ordered", "factor", "new_ordered", "next", "atomic"), ...)

  • opts_package_version(constructor = c("package_version", "next", "atomic"), ...)

  • opts_pairlist(constructor = c("pairlist", "pairlist2"), ...)

  • opts_POSIXct(constructor = c("as.POSIXct", ".POSIXct", "as_datetime", "as.POSIXct.numeric", "as_datetime.numeric", "next", "atomic"), ..., origin = "1970-01-01")

  • opts_POSIXlt(constructor = c("as.POSIXlt", "next", "list"), ...)

  • opts_quosure(constructor = c("new_quosure", "next", "language"), ...)

  • opts_quosures(constructor = c("new_quosures", "next", "list"), ...)

  • opts_R_system_version(constructor = c("R_system_version", "next", "atomic"), ...)

  • opts_rowwise_df(constructor = c("default", "next", "list"), ...)

  • opts_S4(constructor = c("new"), ...)

  • opts_tbl_df(constructor = c("tibble", "tribble", "next", "list"), ..., trailing_comma = TRUE)

  • opts_ts(constructor = c("ts", "next", "atomic"), ...)

  • opts_vctrs_list_of(constructor = c("list_of", "list"), ...)

  • opts_weakref(constructor = c("new_weakref"), ...)

Examples

construct(head(cars))
#> data.frame(speed = c(4, 4, 7, 7, 8, 9), dist = c(2, 10, 4, 22, 16, 10))
construct(head(cars), opts_data.frame("read.table"))
#> read.table(header = TRUE, text = "
#> speed dist
#>    4.   2.
#>    4.  10.
#>    7.   4.
#>    7.  22.
#>    8.  16.
#>    9.  10.
#> ")
construct(head(cars), opts_data.frame("next"))
#> list(speed = c(4, 4, 7, 7, 8, 9), dist = c(2, 10, 4, 22, 16, 10)) |>
#>   structure(row.names = 1:6, class = "data.frame")
construct(iris$Species)
#> factor(rep(c("setosa", "versicolor", "virginica"), each = 50L))
construct(iris$Species, opts_atomic(compress = FALSE), opts_factor("new_factor"))
#> vctrs::new_factor(
#>   c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
#>   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
#>   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
#>   1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
#>   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
#>   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
#>   2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
#>   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
#>   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
#>   3L, 3L, 3L, 3L, 3L, 3L, 3L),
#>   levels = c("setosa", "versicolor", "virginica")
#> )