6 Writing Data
R provides a number of commands for writing output to a file. You will generally use write() or write.table() for writing numeric values and cat() for writing text, or a combination of numeric and character values.
The command write() has the form:
# `x` is the dataframe to save / `file` is the location to save it
write(x, file = "data", ncolumns = if(is.character(x)) 1 else 5, append = FALSE)Example:
write_csv(students, "students.csv")Note that the variable type information is lost when you save to CSV. This makes CSVs a little unreliable for caching interim results, so you will need to recreate the column specification every time you load in.
Example:
write_rds(students, "students.rds")
read_rds("students.rds")
# An alternative method
library(arrow)
write_parquet(students, "students.parquet")
read_parquet("students.parquet")These store data in R’s custom binary format called RDS. This means that when you reload the object, you are loading the exact same R object that you stored. Parquet tends to be much faster than RDS and is usable outside of R, but does require the arrow package.
6.1 Writing to Excel
You can write data back to disk as an Excel file using the write_xlsx() from the writexl package:
write_xlsx(bake_sale, path = "data/bake-sale.xlsx") If you’re interested in additional features like writing to sheets within a spreadsheet and styling, you will want to use the openxlsx package.
6.2 Writing to Google Sheets
You can write from R to Google Sheets with write_sheet(). The first argument is the dataframe to write, and the second argument is the name (or other identifier) of the Google Sheet to write to:
write_sheet(bake_sale, ss = "bake-sale")If you’d like to write your data to a specific (work)sheet inside a Google Sheet, you can specify that with the sheet argument as well.
write_sheet(bake_sale, ss = "bake-sale", sheet = "Sales")6.3 Writing a Dataframe
To write a dataframe to a file we use this:
write.table(x, file = "", append = FALSE, sep = " ", row.names = TRUE, col.names = TRUE)Here x is the vector to be written. If x is a matrix or array then it is converted to a vector (column by column) before being written. The other parameters are optional.
We can identify the complete rows from a two-dimensional object such as a dataframe (that is, rows that have no missing values) via the complete.cases command. We can easily remove rows with missing values using the na.omit function.
6.4 Writing Matrices
Because write() converts matrices to vectors before writing them, using it to write a matrix to a file can cause unexpected results. Since R stores its matrices by column, you should pass the transpose of the matrix to write if you want the output to reflect the matrix structure.
x <- matrix(1:24, nrow = 4, ncol = 6)
write(t(x), file = "../results/out.txt", ncolumns = 6)6.5 cat()
A more flexible command for writing to a file is cat(), which has the form:
cat(..., file = "", sep = " ", append = FALSE)Note that cat does not automatically write a newline after the expressions …. If you want a newline you must explicitly include the string \n.
6.6 Tabular Data
For writing tabular data to text files (i.e. CSV) or connections you may use write.table(), which has this format:
Example:
write.table()6.7 dump()
There is also the very useful dump() function, which creates a text representation of almost any R object that can subsequently be read by source.
Example:
x <- matrix(rep(1:5, 1:5), nrow = 3, ncol = 5)
dump("x", file = "../results/x.txt")
rm(x)
source("../results/x.txt")
x