如何将“dput”的输出放在R中的一行中?

Jul*_*ien 7 r dput

R中如何使输出dput显示在一行中?

如何将获得的字符串复制到剪贴板dput

Dar*_*sai 7

dat <- head(iris)个例子:

  1. 制作一行输出以dput()显示在控制台中:
cat(capture.output(dput(dat)), "\n", sep = "")
Run Code Online (Sandbox Code Playgroud)

输出:

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")
Run Code Online (Sandbox Code Playgroud)
  1. 复制到剪贴板(仅限 Windows):
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")
Run Code Online (Sandbox Code Playgroud)

  • 提供有关 Stack Overflow 问题的数据时,将所有内容放在一行中需要更少的垂直空间 (4认同)
  • 我喜欢这个问题和两个答案。但我想知道这在什么情况下可能相关?非常感谢! (2认同)
  • 请注意,它仅在“dat”不太大时才有效 (2认同)