Exporting Data From R


In the previous chapters we described the essentials of R programming as well as how to import data into R. Here, you’ll learn how to export data from R to txt, csv, Excel (xls, xlsx) and R data file formats. Additionally, we’ll describe how to create and format Word and PowerPoint documents from R.



Exporting data from R



  1. Writing data from R to a txt|csv file: R base functions
  • R base functions for writing data: write.table(), write.csv(), write.csv2()
  • Writing data to a file


Writing Data From R to txt|csv Files: R Base Functions

# Loading mtcars data
data("mtcars")
# Write data to txt file: tab separated values
# sep = "\t"
write.table(mtcars, file = "mtcars.txt", sep = "\t",
            row.names = TRUE, col.names = NA)
# Write data to csv files:  
# decimal point = "." and value separators = comma (",")
write.csv(mtcars, file = "mtcars.csv")
# Write data to csv files: 
# decimal point = comma (",") and value separators = semicolon (";")
write.csv2(mtcars, file = "mtcars.csv")

Read more: Writing data from R to a txt|csv file: R base functions

  1. Fast writing of Data From R to txt|csv Files: readr package
  • Installing and loading readr: install.packages(“readr”)
  • readr functions for writing data: write_tsv(), write_csv()
  • Writing data to a file


Fast Writing of Data From R to txt|csv Files: readr package

# Loading mtcars data
data("mtcars")
library("readr")
# Writing mtcars data to a tsv file
write_tsv(mtcars, path = "mtcars.txt")
# Writing mtcars data to a csv file
write_csv(mtcars, path = "mtcars.csv")

Read more: Fast writing of Data From R to txt|csv Files: readr package

  1. Writing data from R to Excel files (xls|xlsx)
  • Installing xlsx package: install.packages(“xlsx”)
  • Using xlsx package: write.xlsx()


Writing Data From R to Excel Files (xls|xlsx)

library("xlsx")
# Write the first data set in a new workbook
write.xlsx(USArrests, file = "myworkbook.xlsx",
      sheetName = "USA-ARRESTS", append = FALSE)
# Add a second data set in a new worksheet
write.xlsx(mtcars, file = "myworkbook.xlsx", 
           sheetName="MTCARS", append=TRUE)

Read more: Writing data from R to Excel files (xls|xlsx)

  1. Saving data into R data format: RDATA and RDS
  • Save one object to a file: saveRDS(object, file), readRDS(file)
  • Save multiple objects to a file: save(data1, data2, file), load(file)
  • Save your entire workspace: save.image(), load()


Save data into R data formats

  1. Saving and restoring one single R object:
# Save a single object to a file
saveRDS(mtcars, "mtcars.rds")
# Restore it under a different name
my_data <- readRDS("mtcars.rds")
  1. Saving and restoring one or more R objects:
# Save multiple objects
save(data1, data2, file = "data.RData")
# To load the data again
load("data.RData")
  1. Saving and restoring your entire workspace:
# Save your workspace
save.image(file = "my_work_space.RData")
# Load the workspace again
load("my_work_space.RData")

Read more: Saving data into R data format: RDATA and RDS

  1. Create and format Word documents with R and ReporteRs package

ReporteRs package, by David Gohel, provides easy to use functions to write and format Word documents. It can be also used to generate Word document from a template file with logos, fonts, etc. ReporteRs is Java-based solution, so it works on Windows, Linux and Mac OS systems.

  • Install and load the ReporteRs R package
  • Create a simple Word document
    • Add texts : title and paragraphs of texts
    • Format the text of a Word document using R software
    • Add plots and images
    • Add a table
    • Add lists : ordered and unordered lists
    • Add a footnote to a Word document
    • Add R scripts
  • Add a table of contents into a Word document


Write a Word document using R software and ReporteRs package

Read more: Create and format Word documents with R and ReporteRs package

  1. Create a Word document from a template file with R and ReporteRs package
  • Quick introduction to ReporteRs package
  • Create a Word document using a template file


Read and write a Word document from a template using R software and ReporteRs package

Read more: Create a Word document from a template file with R and ReporteRs package

  1. Add a table into a Word document with R and ReporteRs package
  • Add a simple table
  • Add a formatted table
    • Change the background colors of rows and columns
    • Change cell background and text colors
    • Insert content into a table : header and footer rows
  • Analyze, format and export a correlation matrix into a Word document
  • Powerpoint


Add a table into a Word document with R and ReporteRs package

Read more: Add a table into a Word document with R and ReporteRs package

  1. Create and format PowerPoint documents with R and ReporteRs
  • Why is it important to be able to generate a PowerPoint report from R ?
    • Reason I : Many collaborators works with Microsoft office tools
    • Reason II : keeping beautiful R graphs beautiful for publications
  • Install and load the ReporteRs package
  • Create a simple PowerPoint document
    • Slide layout
    • Generate a simple PowerPoint document from R software
    • Format the text of a PowerPoint document
    • Add plots and images
    • Add a table
    • Add ordered and unordered lists
  • Create a PowerPoint document from a template file


Write a PowerPoint document using R software and ReporteRs package

Write a PowerPoint document using R software and ReporteRs package

Read more: Create and format PowerPoint documents with R and ReporteRs

  1. Create an editable graph from R to PowerPoint
  • Case of base graphs
  • Case of graphs generated using ggplot2


Editable plot from R software using ReporteRs package

Read more: Create an editable graph from R to PowerPoint










Want to Learn More on R Programming and Data Science?

Follow us by Email

by FeedBurner

On Social Networks:


 Get involved :
  Click to follow us on and Google+ :   
  Comment this article by clicking on "Discussion" button (top-right position of this page)
  Sign up as a member and post news and articles on STHDA web site.


Articles contained by this category :  

Add a table into a Word document using R software and ReporteRs package
Create a Word document from a template file using R software and ReporteRs package
Create an editable graph from R software
Create and format PowerPoint documents from R software
Create and format Word documents using R software and Reporters package
Fast Writing of Data From R to txt|csv Files: readr package
R xlsx package : A quick start guide to manipulate Excel files in R
r2excel: Read, write and format easily Excel files using R software
Saving Data into R Data Format: RDS and RDATA
Writing Data From R to Excel Files (xls|xlsx)
Writing Data From R to txt|csv Files: R Base Functions
This page has been seen 7962 times