.chapter9<-function(i=0){ "i Chapter 9: Data Output in R i Explanations - -------------------------------- -- -------------------------- 1 R output 21 binary: files 2 Table 9.1: different formats 22 : save() for an R data set 3 several pairs 23 : example 2 4 text: file 24 : example 3 5 : write.csv() function 25 : example 4 6 : : example 1 26 : writeRDS() 7 : : example 2 27 : example 2 8 : : quote=F,row.names=F 28 : save.image() function 9 : write.table() function 29 : example 10 : example 1 30 : example 2 11 : example 2 31 temp file: why? 12 : example 3 32 file:tempfile() 13 : row.names=F,quote=F 33 : example 2 14 : appending to an existing file 34 PDF file: plot 15 : write to a clipboard 35 : a scatter 16 : write() function 36 : 17 : cat() function 37 : 18 : sink() function 38 Writing to an Excel file 19 Videos 39 example 2 20 Links 40 example 3 Example #1:>.c9 # see the above list Example #2:>.c9() # the same as the above Example #3:>.c9(1) # see the first explanation ";.zchapter9(i)} .n9chapter<-40 .zchapter9<-function(i){ if(i==0){ print(.c9) }else{ .printEachQ(9,i,.n9chapter) } } .c9<-.chapter9 .C9EXPLAIN1<-"Output //////////////////////////////// In the context of R programming, the output generated during data analysis and statistical modeling plays a pivotal role in conveying insights and results to users. R produces a diverse array of output formats, ranging from simple console prints to sophisticated graphical visualizations and comprehensive statistical summaries. The versatility of R output allows users to communicate findings effectively, whether through descriptive statistics, regression analyses, or data visualizations created with packages like ggplot2. Moreover, R facilitates the integration of results into reports and presentations by enabling the export of output in various formats such as PDF, HTML, or image files. Understanding how to interpret and effectively utilize R output is essential for researchers, analysts, and data scientists, as it empowers them to articulate and share the outcomes of their analyses in a clear and compelling manner. //////////////////////////////// " .C9EXPLAIN2<-"different formats for output files //////////////////////////////// different formats for output files Type Extensions R functions ----------- ------------ ------------------ Text file CSV write.csv() write.table() write.delim() Text file txt write.csv() write.table() write.delim() write() scan() sink() Binary files RData save(),save.image() rds saveRDS() xls, xlsx See Chapter 12: R and Excel //////////////////////////////// " .C9EXPLAIN3<-"several pairs such as read.csv() vs. write.csv() //////////////////////////////// It is easy to remember several most used R functions to export data when they are paired with some functions that retrieve data. 1) read.csv() vs. write.csv() 2) read.table() vs. write.table() 3) read.delim() vs. write.table() 4) readLines() vs. writeLines() 5) load() vs. save() 6) readRDS() vs. writeRDS() //////////////////////////////// " .C9EXPLAIN3<-"write.csv() function //////////////////////////////// x<-1:100 y<-matrix(x,50,4) write.csv(y,file=\"c:/temp/test.csv\") //////////////////////////////// " .C9EXPLAIN4<-"text files //////////////////////////////// For a text file, we can open it with Notepad, Microsoft Word and the like. //////////////////////////////// " .C9EXPLAIN5<-"write.table() function //////////////////////////////// x<-1:100 y<-matrix(x,50,4) write.table(y,file=\"c:/temp/test.csv\") //////////////////////////////// " .C9EXPLAIN9<-"write.table() //////////////////////////////// x<-1:500 write.table(x,\"c:/temp/test.txt\") //////////////////////////////// " .C9EXPLAIN10<-"Writing to a clipboard //////////////////////////////// x<-1:10 y<-20:10 z<-cbind(x,y) write.csv(z,\"clipboard\") # launch Excel and paste it there //////////////////////////////// " .C9EXPLAIN8<-"quote=F, row.names=F commands //////////////////////////////// x<-1:100 y<-matrix(x,50,4) write.csv(y,file=\"c:/temp/test.csv\",quote=F,rownames=F) //////////////////////////////// " .C9EXPLAIN14<-"Appending to an existing file //////////////////////////////// write.table(x,file='c:/temp/test.txt') write.table(y,file='c:/temp/test.txt',append=TRUE) # Note that for some reason append=T is not working for write.csv //////////////////////////////// " .C9EXPLAIN15<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN16<-"write() function //////////////////////////////// x<-1:10 y<-20:10 z<-cbind(x,y) write(z,'c:/temp/test2.txt') //////////////////////////////// " .C9EXPLAIN17<-"cat() function //////////////////////////////// > cat(\"1 3 4 5\", \"7 17 6 1\",file=\"c:/test.txt\",seq=\"\n\") >cat(file=\"c:/test_R/test.txt\",\"123456\",\"987654\",sep=\"\n\") //////////////////////////////// " .C9EXPLAIN18<-"the sink() function //////////////////////////////// x<-1:100 sink(\"test.txt\") # save subsequent print to this file x # nothing shown on the screen print(x) # again nothing shown sink() # end of the sink operation unsink(\"test.txt\") # close the file # start to save all operations sink(\"c:/temp/all.txt\") # your code there # End unsink(\"c:/temp/all.txt\") //////////////////////////////// " .C9EXPLAIN19<-"Videos //////////////////////////////// Brown, Jonathan,2016, Interpreting R Output For Simple Linear Regression Part 1 (EPSY 5262) (v452,s428k,t13:00) https://www.youtube.com/watch?v=u7TxjUI4PRI Chan, Phil,2012, Statistics with R: How to redirect textual output to a file in R using sink() (v9k,s28k,t2:57) https://www.youtube.com/watch?v=4GzCtx5XMNE Gotwals, Bob,2015, Exporting R Results (v40k,s428k,t3:56) https://www.youtube.com/watch?v=iZLCWJg0A44 //////////////////////////////// " .C9EXPLAIN20<-"Links //////////////////////////////// DataCamp, 2015, Exporting Data https://www.statmethods.net/input/exportingdata.html Send R Output to a File https://astrostatistics.psu.edu/su07/R/html/base/html/sink.html Vries, Andrie, Joris Meys, How to Save Graphics to an Image File in R https://www.dummies.com/programming/r/how-to-save-graphics-to-an-image-file-in-r/ //////////////////////////////// " .C9EXPLAIN21<-"Binary files //////////////////////////////// Binary files RData save() save.image() rds saveRDS() xls, xlsx See Chapter 12: R and Excel //////////////////////////////// " .C9EXPLAIN22<-"save() for an R data set //////////////////////////////// x<-1:500 save(x,file=\"c:\temp\test.RData\") //////////////////////////////// " .C9EXPLAIN23<-"writeRDS() //////////////////////////////// The writeRDS() functio is used to write a single R object to a file. Example #1: # write to a c:/temp/test.rds a<-1:100 saveRDS(a, \"c:/temp/test.rds\") Example #2: # write to a temp file a<-1:100 myFile<- tempfile(\"test\", fileext = \".rds\") saveRDS(a, myFile) Example #3 > pv<-100 > r<-0.1 > n<-5 > saveRDS(pv,r,n,\"c:/temp/test2.rds\") Error in saveRDS(pv, r, n, \"c:/temp/test2.rds\") : bad 'file' argument # Compare save(pv,r,n,file=\"c:/temp/test2.rds\") //////////////////////////////// " .C9EXPLAIN30<-"readRDS //////////////////////////////// Two functions called readRDS and writeRDS for data sets with an extension of .rds. a<-1:100 myFile<- tempfile(\"test\", fileext = \".rds\") saveRDS(a, myFile) a2 <- readRDS(myFile) identical(a,a2) # [1] TRUE # Example #1 x<-readRDS(url('http://datayyy.com/data_R/SECforms.rds')) head(x) load((url('http://datayyy.com/data_R/SECforms.rda'))) head(.x) x<-load((url('http://datayyy.com/data_R/SECforms.rda'))) head(x) //////////////////////////////// " .C9EXPLAIN25<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN26<-"tempfile() //////////////////////////////// //////////////////////////////// " .C9EXPLAIN27<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN28<-"save.image() //////////////////////////////// we can use the save.image() to save everything to a RData data set. In addition, we can use the load() function to load it. //////////////////////////////// " .C9EXPLAIN29<-"save.image() function //////////////////////////////// rm(list=ls(all=T)) # remove everything source('http://datayyy.com/fmr/week1.txt') save.image('weeek1.RData') //////////////////////////////// " .C9EXPLAIN30<-"tempfile() //////////////////////////////// //////////////////////////////// " .C9EXPLAIN31<-"why a temporary file //////////////////////////////// Firstly, tempfile() generates a unique and temporary file name, ensuring no name collisions or overwrites. This is particularly useful when you need a temporary file for intermediate calculations or storage. Secondly, since tempfile() creates files with unique names, it reduces the risk of accidentally overwriting important files. Thirdly, using tempfile() simplifies the process of managing temporary files. Fourthly, we don't need to specify a location or handle the file creation process manually. Finally, after quitting R, the temporary file will vanish automatically. //////////////////////////////// " .C9EXPLAIN32<-"tempfile() //////////////////////////////// fileName<-tempfile() //////////////////////////////// " .C9EXPLAIN33<-" //////////////////////////////// > ff<-tempfile() > x<-1:10 > write.table(x,ff) //////////////////////////////// " .C9EXPLAIN34<-"save to a PDF graph //////////////////////////////// pdf(\"c:/test_R/t.pdf\") plot(sin,-pi,pi) dev.off() //////////////////////////////// " .C9EXPLAIN35<-" //////////////////////////////// data <- data.frame(x = rnorm(100), y = rnorm(100)) p <- ggplot(data, aes(x = x, y = y)) + geom_point() + ggtitle(\"Sample Scatter Plot\") pdf(\"output.pdf\") print(p) dev.off() //////////////////////////////// " .C9EXPLAIN36<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN37<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN38<-"Writing to an Excel file //////////////////////////////// library(xlsx) x<-1:100 y<-matrix(x,50,2) write.xlsx(y, \"c:/test.xlsx\") //////////////////////////////// " .C9EXPLAIN39<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN40<-" //////////////////////////////// //////////////////////////////// " .C9EXPLAIN41<-" //////////////////////////////// hms training school https://www.youtube.com/watch?v=vCWMBvxWKL0 https://www.youtube.com/watch?v=L3hYjDNbj4Y this one is good https://www.youtube.com/watch?v=Au2DGt3rKRw https://www.youtube.com/watch?v=1GzE7jcf0Rg //////////////////////////////// "