How to Export R Output to a File
Learn how to export R output into a file.
We'll cover the following
There are many ways to export R output. First, the simplest way to export all output from an R program into a text file is to employ the sink()
function. The sink()
function simply redirects output from the console to a file. We may insert the following two sink()
function lines of code at the beginning and the end of the segment of an R program whose output we plan to export. We use v1
as an example below.
# change working directory for program to project folder
setwd("C:/Project")
# redirect and export console output to a file named output.txt
sink("output.txt")
# Simple Example: Create, Describe, and Graph a Variable # use c() function to create a variable or vector object c(1, 2, 0, 2, 4, 5, 10, 1)
# assign the c function output to an object named v1
v1 <- c(1, 2, 0, 2, 4, 5, 10, 1)
# call object v1 to see what is in it
v1
# restore output to the screen
sink()
Running this program and then opening output.txt, we will find the following output:
[1] 1 2 0 2 4 510 1
[1] 1 2 0 2 4 510 1
Two caveats are worth noting. The output between the two sink()
functions is redirected to output.txt
and thus, no longer shows up in the console. The file output.txt
contains only the output from R functions but not the R code.
A second way to export R output is primarily to convert the select output into formatted statistical tables. As noted earlier, the stargazer package provides one way to export statistical output into a formatted table in the text or LaTeX format. Alternatively, we may send the computed statistics to a data object, use the write.table()
function to export the data object to a tab-
or comma-delimited file, and then format in Excel or Word. Furthermore, we use the xtable()
function to create the LaTeX code for any statistical output object so that a nicely formatted table can be created in LaTeX.
A third way to export R output is to integrate the paper or report text, the R program code, and the R output into one file, via the knitr
package plus pandoc
for creating a Word file for the final paper or report, or via the knitr
package plus RStudio for creating a .Rnw
or .Rmd
file that compiles into a pdf file of the final paper or report. This approach requires much greater investments in time and effort on the part of a user, but the final paper or report is typeset and professionally formatted.
How to save a graph into file of pdf or other formats
To save a graph into a pdf file, we employ the pdf()
function to first generate a graph called “graph1.pdf,” and then shut down the graphing device with dev.off ()
function so that we may open the graph file to view it in another application such
as Acrobat Reader.
# pdf() function creates a pdf file for subsequent graph # function output
pdf("graph1.pdf")
# hist() function creates a histogram for variable v1
hist(v1)
# dev.off() returns back to screen
dev.off()
Note that because we did not tell R where to save the graph file, R will then save it in the working directory specified in the setwd()
function earlier. Alternatively, we may specify the path to another folder in the pdf()
function.
Sometimes we need to save a graph into other formats. To do so, we simply replace the pdf line of code with any one of the following:
# create image files of alternative formats
bmp("graph1.bmp") jpeg("graph1.jpeg") png("graph1.png") postscript("graph1.ps")
Alternatively, one may simply copy and paste an image from R graph output into a Microsoft Word or PowerPoint file.
Why do we see =
also used as an assignment symbol rather than < −
?
The composite symbol of less than and minus, <-
, works as the assignment symbol in R; that is, the function output on the right of the composite symbol is assigned to an R object on the left of the symbol. Starting with version 1.4.0
in 2001, however, R also allows the equal sign, =
, to serve as an assignment operator, but in this course, to be consistent, we will use <-
throughout.
Other than vector and data frame, what are some other data objects often used in R?
Two other data objects often used are array and list. We will discuss them in detail when we come across them in future sessions. An array is a three-dimensional object with rows by columns by height, such as several matrices stacked on top of each other. Like in a matrix, all elements in an array must be of the same data type.
A list in R is a set of objects, which could be vectors, matrices, arrays, data frames, and other lists. A list allows us to gather these objects under one name.
How to Create a Box Plot of a Variable With Respect to Each Value of Another Variable
Often we would like to compare the distribution of a continuous variable across different groups (i.e., with respect to another variable). The R code is as follows:
# compare distribution of x1 across different groups of x2
boxplot(x1 ̃ x2, data = filename)
Note that since we explicitly specify the data frame name with the data=
option, we no longer need to use the $
sign to link the dataset to each variable.
Get hands-on with 1400+ tech skills courses.