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,
unicode_representation = c("ascii", "latin", "character", "unicode"),
escape = FALSE,
compare = compare_options(),
one_liner = FALSE,
template = getOption("constructive_opts_template")
)
construct_multi(
x,
...,
data = NULL,
pipe = NULL,
check = NULL,
unicode_representation = c("ascii", "latin", "character", "unicode"),
escape = FALSE,
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()
.- unicode_representation
By default "ascii", which means only ASCII characters (code point < 128) will be used to construct strings and variable names. This makes sure that homoglyphs (different spaces and other identically displayed unicode characters) are printed differently, and avoid possible unfortunate copy and paste auto conversion issues. "latin" is more lax and uses all latin characters (code point < 256). "character" shows all characters, but not emojis. Finally "unicode" displays all characters and emojis, which is what
dput()
does.- escape
Whether to escape double quotes and backslashes. If
FALSE
we use single quotes to surround strings (including variable and element names) containing double quotes, and raw strings for strings that contain backslashes and/or a combination of single and double quotes. Depending onunicode_representation
escape = FALSE
cannot be applied on all strings.- compare
Parameters passed to
waldo::compare()
, built withcompare_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}
.
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("default", "ascii", "latin", "character", "unicode"), escape = NULL)
opts_classGeneratorFunction(constructor = c("setClass"), ...)
opts_classPrototypeDef(constructor = c("prototype"), ...)
opts_classRepresentation(constructor = c("getClassDef"), ...)
opts_constructive_options(constructor = c("opts", "next"), ...)
opts_CoordCartesian(constructor = c("coord_cartesian", "next", "environment"), ...)
opts_CoordFixed(constructor = c("coord_fixed", "next", "environment"), ...)
opts_CoordFlip(constructor = c("coord_flip", "next", "environment"), ...)
opts_CoordMap(constructor = c("coord_map", "next", "environment"), ...)
opts_CoordMunch(constructor = c("coord_munch", "next", "environment"), ...)
opts_CoordPolar(constructor = c("coord_polar", "next", "environment"), ...)
opts_CoordQuickmap(constructor = c("coord_quickmap", "next", "environment"), ...)
opts_CoordSf(constructor = c("coord_sf", "next", "environment"), ...)
opts_CoordTrans(constructor = c("coord_trans", "next", "environment"), ...)
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_element_blank(constructor = c("element_blank", "next", "list"), ...)
opts_element_grob(constructor = c("element_grob", "next", "list"), ...)
opts_element_line(constructor = c("element_line", "next", "list"), ...)
opts_element_rect(constructor = c("element_rect", "next", "list"), ...)
opts_element_render(constructor = c("element_render", "next", "list"), ...)
opts_element_text(constructor = c("element_text", "next", "list"), ...)
opts_environment(constructor = c(".env", "list2env", "as.environment", "new.env", "topenv", "new_environment"), ..., recurse = FALSE, predefine = FALSE)
opts_externalptr(constructor = c("default"), ...)
opts_FacetWrap(constructor = c("facet_wrap", "ggproto", "next", "environment"), ...)
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_ggplot(constructor = c("ggplot", "next", "list"), ...)
opts_ggproto(constructor = c("default", "next", "environment"), ...)
opts_grouped_df(constructor = c("default", "next", "list"), ...)
opts_labels(constructor = c("labs", "next", "list"), ...)
opts_language(constructor = c("default"), ...)
opts_Layer(constructor = c("default", "layer", "next", "environment"), ...)
opts_list(constructor = c("list", "list2"), ..., trim = NULL, fill = c("vector", "new_list", "+", "...", "none"))
opts_margin(constructor = c("margin", "next", "atomic"), ...)
opts_matrix(constructor = c("matrix", "array", "next", "atomic"), ...)
opts_mts(constructor = c("ts", "next", "atomic"), ...)
opts_numeric_version(constructor = c("numeric_version", "next", "list"), ...)
opts_ordered(constructor = c("ordered", "factor", "new_ordered", "next", "atomic"), ...)
opts_package_version(constructor = c("package_version", "next", "list"), ...)
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", "list"), ...)
opts_rel(constructor = c("rel", "next", "atomic"), ...)
opts_rowwise_df(constructor = c("default", "next", "list"), ...)
opts_S4(constructor = c("new"), ...)
opts_Scale(constructor = c("default", "next", "environment"), ...)
opts_ScalesList(constructor = c("ScalesList", "next", "list"), ...)
opts_simpleUnit(constructor = c("unit", "next", "atomic"), ...)
opts_tbl_df(constructor = c("tibble", "tribble", "next", "list"), ..., trailing_comma = TRUE)
opts_theme(constructor = c("theme", "next", "list"), ...)
opts_ts(constructor = c("ts", "next", "atomic"), ...)
opts_uneval(constructor = c("aes", "next", "list"), ...)
opts_vctrs_list_of(constructor = c("list_of", "next", "list"), ...)
opts_waiver(constructor = c("waiver", "next", "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")
#> )