Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Saving the Output of "dput" in a txt File

I am working with the R programming language.

Suppose I have the following dataset:

id = 1:10
var_1 = rnorm(10,10,10)
var_2 = rnorm(10,10,10)
data_1 = data.frame(id, var_1, var_2)

If I use the "dput" statement in R, I get the following output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 dput(data_1)

structure(list(id = 1:10, var_1 = c(8.82195120774659, -3.93182446227049, 
4.71778269084519, 1.75623141174446, 8.04018019946558, 24.512930378032, 
18.5285862803682, 14.0274634385899, 13.2841577275347, 15.2732721877118
), var_2 = c(29.6332731622598, 22.5632323327142, -8.9690858260254, 
-7.20867402757854, -0.040059695538643, 0.727780887727967, 27.3854470138422, 
5.04872507031284, 0.934424948207598, 17.6628276038318)), class = "data.frame", row.names = c(NA, 
-10L))

I can then manually copy/paste this output into a notepad/txt file:

enter image description here

But now, suppose my file is much bigger:

id = 1:100000
var_1 = rnorm(100000,10,10)
var_2 = rnorm(100000,10,10)
data_2 = data.frame(id, var_1, var_2)

When I write dput(data_2) the output is too big to manually copy/paste into notepad.

I tried the following idea:

attempt = capture.output(dput(final))
saveRDS(attempt,"attempt.RDS")

I then tried to open "attempt.RDS" in notepad, but I get the following output:

¤,f®îMáVá1·²-×H*Ç3·†§F#ÉÛx·FR?H@z
$÷^pk±Ö4’¶RÕè=H®ô; ¯OÒ»Iûi¤äV  ²ÙêÂÔÖ§@º  5’ÊñÌ­çdn=ç¶FRÃ:4’zr HoäjŠn…o®‘TޝW Ý–$w5’öÓ   H?"I$µˆ)©«¥:¯¹ÒJ<s~
j4’jsÜ‹äêi³½0™[ý~Dô:$4’ø"¦U_‡$š4’ܳíVM3¥;4’zré É­AoQ¼ÐHZÆÌÕ•hn];skÅs¤r|½   H?É•Ý
êJp·ŽžÍVÓ§¶uUIy¼nÒHjËw;wkôSHyL«é$ì”8¥
¤eL«Þ›Ä:4’zré‰;[¿éuH"h$‰EL«é~$YÔH*Çsç¦F#©6ǽH

Can someone please show me how to fix this problem?

Thanks!

>Solution :

You can save the output of dput in the text file directly without manually copy-pasting it. For example, with mtcars dataset you can do –

dput(mtcars, 'new_file.txt')

This will save the output of dput in the file called new_file.txt in your working directory.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading