如何让R中dput
的输出在一行中显示?
如何将dput
获得的字符串复制到剪贴板中?
以 dat <- head(iris)
为例:
dput()
的输出结果压缩成一行并在控制台中显示:cat(capture.output(dput(dat)), "\n", sep = "")
输出:
structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("setosa", "versicolor", "virginica"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
writeClipboard(paste(capture.output(dput(dat)), collapse = ""))
要直接复制粘贴dput
的输出结果,您可以使用read.so
包中带有write_clip = T
参数的write.so
函数:
#devtools::install_github("alistaire47/read.so")
library(read.so)
write.so(head(iris), write_clip = TRUE)
输出
iris <- data.frame(
Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7),
Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4),
Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")
)
dat <- head(iris)
为例:
dat <- head(iris)
安装包-->"datapasta"install.packages(c("datapasta"), dependencies = TRUE)
使用dpasta(注意:仅适用于RStudio)
datapasta::dpasta(dat)
输出-->
named list()
> data.frame(
+ Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4),
+ Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9),
+ Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7),
+ Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 0.4),
+ Species = as.factor(c("setosa","setosa",
+ "setosa","setosa","setosa","setosa"))
+ )
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
datapasta::dmdclip(dat)