cleanput: dput with saner quotes and line breaks

Description Usage Arguments Details Author(s) See Also Examples

View source: R/cleanput.R

Description

Write a representation of an object to a temp file and print it to the screen. Really handy for easy conversion of objects to a representation for StackOverflow for making your reproducable examples.

Usage

1

Arguments

dat

data to be put

Details

Requires that your system has cat, which is easy on Windows if you have mysys32 installed, which comes with git. Install git for Windows from https://git-scm.com. You probably need to let it put the Linux commands onto your path, but come on let's be real. Unless your a Windows batch file commando this won't affect you, and if you are that person, you're not reading this. Actually, I'd be happy if anyone ever reads this.

Author(s)

Gene Leynes

See Also

cat, .deparseOpts, tempfile

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require(geneorama)

## Let's say you need an example, and you choose a modified verion of mtcars
#dt <- data.table(carname = rownames(mtcars[1:4,]),
#				 carbrand = as.factor(sapply(strsplit(rownames(mtcars[1:4,]), " "), `[[`, 1)),
#				 mtcars[1:4,])
#dt
#           carname carbrand  mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1:      Mazda RX4    Mazda 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2:  Mazda RX4 Wag    Mazda 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3:     Datsun 710   Datsun 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 4: Hornet 4 Drive   Hornet 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1

## This is hard to read, so maybe you want to represent it directly
dput(dt)
# structure(list(carname = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710",
# "Hornet 4 Drive"), carbrand = structure(c(3L, 3L, 1L, 2L), .Label = c("Datsun",
# "Hornet", "Mazda"), class = "factor"), mpg = c(21, 21, 22.8,
# 21.4), cyl = c(6, 6, 4, 6), disp = c(160, 160, 108, 258), hp = c(110,
# 110, 93, 110), drat = c(3.9, 3.9, 3.85, 3.08), wt = c(2.62, 2.875,
# 2.32, 3.215), qsec = c(16.46, 17.02, 18.61, 19.44), vs = c(0,
# 0, 1, 1), am = c(1, 1, 1, 0), gear = c(4, 4, 4, 3), carb = c(4,
# 4, 1, 1)), .Names = c("carname", "carbrand", "mpg", "cyl", "disp",
# "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"), row.names = c(NA,
# -4L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x074324a0>)

## That looks nice, buy when you paste it into R Studio, it becomes 547 columns wide!!
## output not shown

## cleanput attempts to fix the line breaks
cleanput(dt)
# structure(list(carname = c('Mazda RX4', 'Mazda RX4 Wag', 'Datsun 710', 'Hornet 4 Drive'),
#  carbrand = structure(c(3L, 3L, 1L, 2L),
#  .Label = c('Datsun', 'Hornet', 'Mazda'),
#  class = 'factor'),
#  mpg = c(21, 21, 22.8, 21.4),
#  cyl = c(6, 6, 4, 6),
#  disp = c(160, 160, 108, 258),
#  hp = c(110, 110, 93, 110),
#  drat = c(3.9, 3.9, 3.85, 3.08),
#  wt = c(2.62, 2.875, 2.32, 3.215),
#  qsec = c(16.46, 17.02, 18.61, 19.44),
#  vs = c(0, 0, 1, 1),
#  am = c(1, 1, 1, 0),
#  gear = c(4, 4, 4, 3),
#  carb = c(4, 4, 1, 1)),
#  .Names = c('carname', 'carbrand', 'mpg', 'cyl', 'disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb'),
#  row.names = c(NA, -4L),
#  class = c('data.table', 'data.frame'),
#  .internal.selfref = <pointer: 0x074324a0>)

## When you paste into R Studio this becomes:

# structure(list(carname = c('Mazda RX4', 'Mazda RX4 Wag', 'Datsun 710', 'Hornet 4 Drive'),
# 			   carbrand = structure(c(3L, 3L, 1L, 2L),
# 			   					 .Label = c('Datsun', 'Hornet', 'Mazda'),
# 			   					 class = 'factor'),
# 			   mpg = c(21, 21, 22.8, 21.4),
# 			   cyl = c(6, 6, 4, 6),
# 			   disp = c(160, 160, 108, 258),
# 			   hp = c(110, 110, 93, 110),
# 			   drat = c(3.9, 3.9, 3.85, 3.08),
# 			   wt = c(2.62, 2.875, 2.32, 3.215),
# 			   qsec = c(16.46, 17.02, 18.61, 19.44),
# 			   vs = c(0, 0, 1, 1),
# 			   am = c(1, 1, 1, 0),
# 			   gear = c(4, 4, 4, 3),
# 			   carb = c(4, 4, 1, 1)),
# 		  .Names = c('carname', 'carbrand', 'mpg', 'cyl', 'disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb'),
# 		  row.names = c(NA, -4L),
# 		  class = c('data.table', 'data.frame'),
# 		  .internal.selfref = <pointer: 0x074324a0>)

## Which can be easily converted to this:

data.table(carname = c('Mazda RX4', 'Mazda RX4 Wag', 'Datsun 710', 'Hornet 4 Drive'),
		   carbrand = structure(c(3L, 3L, 1L, 2L),
		   					 .Label = c('Datsun', 'Hornet', 'Mazda'),
		   					 class = 'factor'),
		   mpg = c(21, 21, 22.8, 21.4),
		   cyl = c(6, 6, 4, 6),
		   disp = c(160, 160, 108, 258),
		   hp = c(110, 110, 93, 110),
		   drat = c(3.9, 3.9, 3.85, 3.08),
		   wt = c(2.62, 2.875, 2.32, 3.215),
		   qsec = c(16.46, 17.02, 18.61, 19.44),
		   vs = c(0, 0, 1, 1),
		   am = c(1, 1, 1, 0),
		   gear = c(4, 4, 4, 3),
		   carb = c(4, 4, 1, 1))

geneorama/geneorama documentation built on Oct. 17, 2020, 12:35 a.m.