7

How to make the output of dput be displayed in one line in R?

How to copy to the clipboard the string obtained with dput?

0

3 Answers 3

6

Take dat <- head(iris) for example:

  1. Make one-line output for dput() displayed in the console:
cat(capture.output(dput(dat)), "\n", sep = "")

Output:

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")
  1. Copy to the clipboard (Windows only):
writeClipboard(paste(capture.output(dput(dat)), collapse = ""))
3
  • 2
    I like both the question and both answers. But I would like to know in which scenario this could be relevant? Many thanks!
    – TarJae
    Aug 17, 2022 at 14:01
  • 4
    When providing the data on a Stack Overflow question, it takes less vertical space to put everything in one line
    – Julien
    Aug 17, 2022 at 14:39
  • 2
    Note that it only works when dat is not too big
    – Julien
    Aug 26, 2022 at 13:18
4

To copy paste directly the output of dput, you can use write.so with write_clip = T from the read.so package:

#devtools::install_github("alistaire47/read.so")
library(read.so)
write.so(head(iris), write_clip = TRUE)

output

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")
)
1
  • 2
    Hi Maël! It seems like your answer only answers the second part of OP's question (the clipboard part), but doesn't answer the first bit (the dput displayed in one line part)
    – Mark
    Sep 24 at 5:50
-1

Take dat <- head(iris) for example:

dat <- head(iris)

Install the packages--> "datapasta"

#install.packages(c("datapasta"), dependencies = TRUE)

Use dpasta (note: only works if you're using RStudio)

datapasta::dpasta(dat)

output-->

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

Copy to the clipboard ( using Rstudio )-->

datapasta::dmdclip(dat)

2
  • 1
    It does not put everything in one line
    – Julien
    Sep 24 at 8:19
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 4 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.