Learn R Programming

⚠️There's a newer version (1.2.3) of this package.Take me there.

rio: A Swiss-Army Knife for Data I/O

Overview

The aim of rio is to make data file I/O in R as easy as possible by implementing two main functions in Swiss-army knife style:

  • import() provides a painless data import experience by automatically choosing the appropriate import/read function based on file extension (or a specified format argument)
  • export() provides the same painless file recognition for data export/write functionality

Installation

The package is available on CRAN and can be installed directly in R using install.packages().

install.packages("rio")

The latest development version on GitHub can be installed using:

if (!require("remotes")){
    install.packages("remotes")
}
remotes::install_github("gesistsa/rio")

Optional: Installation of additional formats (see below: Supported file formats)

library(rio)
install_formats()

Usage

Because rio is meant to streamline data I/O, the package is extremely easy to use. Here are some examples of reading, writing, and converting data files.

Import

Importing data is handled with one function, import():

library("rio")
import("starwars.xlsx")
##                  Name homeworld species
## 1      Luke Skywalker  Tatooine   Human
## 2               C-3PO  Tatooine   Human
## 3               R2-D2  Alderaan   Human
## 4         Darth Vader  Tatooine   Human
## 5         Leia Organa  Tatooine   Human
## 6           Owen Lars  Tatooine   Human
## 7  Beru Whitesun lars   Stewjon   Human
## 8               R5-D4  Tatooine   Human
## 9   Biggs Darklighter  Kashyyyk Wookiee
## 10     Obi-Wan Kenobi  Corellia   Human
import("starwars.csv")
##                  Name homeworld species
## 1      Luke Skywalker  Tatooine   Human
## 2               C-3PO  Tatooine   Human
## 3               R2-D2  Alderaan   Human
## 4         Darth Vader  Tatooine   Human
## 5         Leia Organa  Tatooine   Human
## 6           Owen Lars  Tatooine   Human
## 7  Beru Whitesun lars   Stewjon   Human
## 8               R5-D4  Tatooine   Human
## 9   Biggs Darklighter  Kashyyyk Wookiee
## 10     Obi-Wan Kenobi  Corellia   Human

Export

Exporting data is handled with one function, export():

export(mtcars, "mtcars.csv") # comma-separated values
export(mtcars, "mtcars.rds") # R serialized
export(mtcars, "mtcars.sav") # SPSS

A particularly useful feature of rio is the ability to import from and export to compressed archives (e.g., zip), saving users the extra step of compressing a large exported file, e.g.:

export(mtcars, "mtcars.tsv.zip")

export() can also write multiple data frames to respective sheets of an Excel workbook or an HTML file:

export(list(mtcars = mtcars, iris = iris), file = "mtcars.xlsx")

Supported file formats

rio supports a wide range of file formats. To keep the package slim, several formats are supported via “Suggests” packages, which are not installed (or loaded) by default. To ensure rio is fully functional, install these packages the first time you use rio via:

install_formats()

The full list of supported formats is below:

NameExtensions / “format”Import PackageExport PackageTypeNote
Archive files (handled by tar)tar / tar.gz / tgz / tar.bz2 / tbz2utilsutilsDefault
Bzip2bz2 / bzip2basebaseDefault
Gzipgz / gzipbasebaseDefault
Zip filesziputilsutilsDefault
Ambiguous file formatdatdata.tableDefaultAttempt as delimited text data
CSVY (CSV + YAML metadata header)csvydata.tabledata.tableDefault
Comma-separated datacsvdata.tabledata.tableDefault
Comma-separated data (European)csv2data.tabledata.tableDefault
Data Interchange FormatdifutilsDefault
Epiinfoepiinfo / recforeignDefault
Excelexcel / xlsxreadxlwritexlDefault
Excel (Legacy)xlsreadxlDefault
Fixed-width format datafwfreadrutilsDefault
Fortran datafortranutilsDefaultNo recognized extension
Google Sheetsgooglesheetsdata.tableDefaultAs comma-separated data
Minitabminitab / mtpforeignDefault
Pipe-separated datapsvdata.tabledata.tableDefault
R syntaxrbasebaseDefault
SASsas / sas7bdathavenhavenDefaultExport is deprecated
SAS XPORTxport / xpthavenhavenDefault
SPSSsav / spsshavenhavenDefault
SPSS (compressed)zsavhavenhavenDefault
SPSS PortableporhavenDefault
Saved R objectsrda / rdatabasebaseDefault
Serialized R objectsrdsbasebaseDefault
Statadta / statahavenhavenDefault
Systatsyd / systatforeignDefault
Tab-separated data/ tsv / txtdata.tabledata.tableDefault
Text Representations of R ObjectsdumpbasebaseDefault
Weka Attribute-Relation File Formatarff / wekaforeignforeignDefault
XBASE database filesdbfforeignforeignDefault
Apache Arrow (Parquet)parquetnanoparquetnanoparquetSuggest
ClipboardclipboardcliprcliprSuggestdefault is tsv
EViewseviews / wf1hexViewSuggest
Fast StoragefstfstfstSuggest
Feather R/Python interchange formatfeatherarrowarrowSuggest
Graphpad PrismpzfxpzfxpzfxSuggest
HTML Tableshtm / htmlxml2xml2Suggest
JSONjsonjsonlitejsonliteSuggest
Matlabmat / matlabrmatiormatioSuggest
OpenDocument SpreadsheetodsreadODSreadODSSuggest
OpenDocument Spreadsheet (Flat)fodsreadODSreadODSSuggest
Serialized R objects (Quick)qsqsqsSuggest
Shallow XML documentsxmlxml2xml2Suggest
YAMLyaml / ymlyamlyamlSuggest

Additionally, any format that is not supported by rio but that has a known R implementation will produce an informative error message pointing to a package and import or export function. Unrecognized formats will yield a simple “Unrecognized file format” error.

Other functions

Convert

The convert() function links import() and export() by constructing a dataframe from the imported file and immediately writing it back to disk. convert() invisibly returns the file name of the exported file, so that it can be used to programmatically access the new file.

convert("mtcars.sav", "mtcars.dta")

It is also possible to use rio on the command-line by calling Rscript with the -e (expression) argument. For example, to convert a file from Stata (.dta) to comma-separated values (.csv), simply do the following:

Rscript -e "rio::convert('iris.dta', 'iris.csv')"

*_list

import_list() allows users to import a list of data frames from a multi-object file (such as an Excel workbook, .Rdata file, zip directory, or HTML file):

str(m <- import_list("mtcars.xlsx"))
## List of 2
##  $ mtcars:'data.frame':  32 obs. of  11 variables:
##   ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##   ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
##   ..$ disp: num [1:32] 160 160 108 258 360 ...
##   ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
##   ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##   ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
##   ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
##   ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
##   ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
##   ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
##   ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
##  $ iris  :'data.frame':  150 obs. of  5 variables:
##   ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##   ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##   ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##   ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##   ..$ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...

export_list() makes it easy to export a list of (possibly named) data frames to multiple files:

export_list(m, "%s.tsv")
c("mtcars.tsv", "iris.tsv") %in% dir()
## [1] TRUE TRUE

Other projects

GUIs

  • datamods provides Shiny modules for importing data via rio.
  • rioweb that provides access to the file conversion features of rio.
  • GREA is an RStudio add-in that provides an interactive interface for reading in data using rio.

Similar packages

  • reader handles certain text formats and R binary files
  • io offers a set of custom formats
  • ImportExport focuses on select binary formats (Excel, SPSS, and Access files) and provides a Shiny interface.
  • SchemaOnRead iterates through a large number of possible import methods until one works successfully

Copy Link

Version

Install

install.packages('rio')

Monthly Downloads

59,518

Version

1.2.2

License

GPL-2

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

August 19th, 2024

Functions in rio (1.2.2)

install_formats

Install rio's ‘Suggests’ Dependencies
rio

A Swiss-Army Knife for Data I/O
import

Import
import_list

Import list of data frames
export

Export
export_list

Export list of data frames to files
gather_attrs

Gather attributes from data frame variables
get_info

Get File Info
characterize

Character conversion of labelled data
convert

Convert from one file format to another