dput2: Write an Object to console

Description Usage Arguments Details See Also Examples

View source: R/dput2.R

Description

Writes an ASCII text representation of an R object to the console for easy copy/paste sharing

Usage

1
2
dput2(x, width = 65, assign = c("front", "end", "none"),
  breakAtParen = FALSE, compact = TRUE, exdent = NULL)

Arguments

x

an object

width

integer; column width

assign

character; should assignment be included?

breakAtParen

logical; should lines break at parenthesis begins

compact

remove spaces around ' = ' assignments

exdent

a non-negative integer specifying the exdentation of lines after the first. default 2 if assign="front", else 0.

Details

This is similar to the way dput is used to print ASCII representations of objects to the console. The differences are that dput2 lets you specify the width of the resulting column, and assignment of the object to the name used in the call will by default be included. Line breaks are by default only done on whitespace, but can be set to happen at parenthesis begins as well. This should not break code and can make for a more compact representation, but it can also make the code harder to read.

See Also

dput, deparse, explode

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
xmpl <- faithful[sort(sample(1:nrow(faithful), 50)), ]
dput(xmpl)
cat(deparse(xmpl, width.cutoff=65), sep='\n')
dput2(xmpl, compact=FALSE)
dput2(xmpl)
dput2(xmpl, assign="end")
dput2(xmpl, assign="none")
dput2(xmpl, 80)

# no line breaks on whitespaces or parens within character strings
xmpl <- mtcars[1:5, ]
rownames(xmpl) <- c("bbbb (hhhhhhh\u00A0hhhhhhhh)", 
                    " rrrrrrrr ( bbbbbbb )",
                    "v v v v v v v v v v",
                    "(  g-god, d-god, _-___)",
                    "100*(part)/(total)")
dput2(xmpl, 15)
dput2(xmpl, 15, breakAtParen=TRUE)

AkselA/R-ymse documentation built on March 21, 2020, 9:52 a.m.