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,
pedantic_encoding = FALSE,
compare = compare_options(),
one_liner = FALSE,
template = getOption("constructive_opts_template"),
classes = NULL
)
construct_multi(
x,
...,
data = NULL,
pipe = NULL,
check = NULL,
unicode_representation = c("ascii", "latin", "character", "unicode"),
escape = FALSE,
pedantic_encoding = FALSE,
compare = compare_options(),
one_liner = FALSE,
template = getOption("constructive_opts_template"),
classes = NULL,
include_dotted = TRUE
)
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
Boolean. 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.- pedantic_encoding
Boolean. Whether to mark strings with the "unknown" encoding rather than an explicit native encoding ("UTF-8" or "latin1") when it's necessary to reproduce the binary representation exactly. This detail is normally of very little significance. The reason why we're not pedantic by default is that the constructed code might be different in the console and in snapshot tests and reprexes due to the latter rounding some angles, and it would be confusing for users.
- 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}
.- classes
A character vector of classes for which to use idiomatic constructors when available, we can provide a package instead of all its classes, in the "{pkg}" form, and we can use a minus sign (inside the quotes) to exclude rather than include. By default we use idiomatic constructors whenever possible. The special values
"*none*"
and"*base*"
can be used to restrict the idiomatic construction to the objects. Seeconstruct_dput()
andconstruct_base()
for wrappers around this feature.- include_dotted
Whether to include names starting with dots, this includes
.Random.seed
in the global environment and objects like.Class
and.Generic
in the execution environments of S3 methods.
Details
construct_multi()
recognizes promises (also called lazy bindings),
this means that for instance construct_multi(environment())
can be called
when debugging a function and will construct unevaluated arguments using
delayedAssign()
.
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"), ...)
opts_atomic(..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE)
opts_bibentry(constructor = c("bibentry", "next"), ...)
opts_blob(constructor = c("blob", "next"), ...)
opts_character(constructor = c("default"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE, unicode_representation = c("ascii", "latin", "character", "unicode"), escape = FALSE)
opts_citationFooter(constructor = c("citFooter", "next"), ...)
opts_citationHeader(constructor = c("citHeader", "next"), ...)
opts_classGeneratorFunction(constructor = c("setClass"), ...)
opts_classPrototypeDef(constructor = c("prototype"), ...)
opts_classRepresentation(constructor = c("getClassDef"), ...)
opts_complex(constructor = c("default"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE)
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"), ..., recycle = TRUE)
opts_data.table(constructor = c("data.table", "next", "list"), ..., selfref = FALSE, recycle = TRUE)
opts_Date(constructor = c("as.Date", "as_date", "date", "new_date", "as.Date.numeric", "as_date.numeric", "next", "double"), ..., origin = "1970-01-01")
opts_difftime(constructor = c("as.difftime", "next"), ...)
opts_dm(constructor = c("dm", "next", "list"), ...)
opts_dots(constructor = c("default"), ...)
opts_double(constructor = c("default"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE)
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", "predefine"), ..., recurse = FALSE)
opts_error(constructor = c("errorCondition", "next"), ...)
opts_expression(constructor = c("default"), ...)
opts_externalptr(constructor = c("default"), ...)
opts_FacetWrap(constructor = c("facet_wrap", "ggproto", "next", "environment"), ...)
opts_factor(constructor = c("factor", "as_factor", "new_factor", "next", "integer"), ...)
opts_formula(constructor = c("default", "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_hexmode(constructor = c("as.hexmode", "next"), ..., integer = FALSE)
opts_integer(constructor = c("default"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE)
opts_integer64(constructor = c("as.integer64", "next", "double"), ...)
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_logical(constructor = c("default"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE)
opts_margin(constructor = c("margin", "next", "double"), ...)
opts_matrix(constructor = c("matrix", "array", "next"), ...)
opts_mts(constructor = c("ts", "next", "atomic"), ...)
opts_noquote(constructor = c("noquote", "next"), ...)
opts_NULL(constructor = "NULL", ...)
opts_numeric_version(constructor = c("numeric_version", "next", "list"), ...)
opts_octmode(constructor = c("as.octmode", "next"), ..., integer = FALSE)
opts_ordered(constructor = c("ordered", "factor", "new_ordered", "next", "integer"), ...)
opts_package_version(constructor = c("package_version", "next", "list"), ...)
opts_pairlist(constructor = c("pairlist", "pairlist2"), ...)
opts_person(constructor = c("person", "next"), ...)
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_R6(constructor = c("R6Class", "next"), ...)
opts_R6ClassGenerator(constructor = c("R6Class", "next"), ...)
opts_raw(constructor = c("as.raw", "charToRaw"), ..., trim = NULL, fill = c("default", "rlang", "+", "...", "none"), compress = TRUE, representation = c("hexadecimal", "decimal"))
opts_rel(constructor = c("rel", "next", "double"), ...)
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_simpleCondition(constructor = c("simpleCondition", "next"), ...)
opts_simpleError(constructor = c("simpleError", "next"), ...)
opts_simpleMessage(constructor = c("simpleMessage", "next"), ...)
opts_simpleUnit(constructor = c("unit", "next", "double"), ...)
opts_simpleWarning(constructor = c("simpleWarning", "next"), ...)
opts_tbl_df(constructor = c("tibble", "tribble", "next", "list"), ..., trailing_comma = TRUE, justify = c("left", "right", "centre", "none"), recycle = 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_warning(constructor = c("warningCondition", "next"), ...)
opts_weakref(constructor = c("new_weakref"), ...)
opts_xts(constructor = c("as.xts.matrix", "next"), ...)
opts_yearmon(constructor = c("as.yearmon", "yearmon", "next"), ...)
opts_yearqtr(constructor = c("as.yearqtr", "yearqtr", "next"), ...)
opts_zoo(constructor = c("zoo", "next"), ...)
opts_zooreg(constructor = c("zooreg", "next"), ...)
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 = c(NA, -6L), 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")
#> )
construct_multi(list(a = head(cars), b = iris$Species))
#> a <- data.frame(speed = c(4, 4, 7, 7, 8, 9), dist = c(2, 10, 4, 22, 16, 10))
#>
#> b <- factor(rep(c("setosa", "versicolor", "virginica"), each = 50L))
#>