• Stars
    star
    556
  • Rank 80,098 (Top 2 %)
  • Language
    R
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Swiss-Army Knife for Data I/O

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

CRAN Version Downloads

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

Note: Because of inconsistencies across underlying packages, the data.frame returned by import might vary slightly (in variable classes and attributes) depending on file type.

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 (e.g., zip) directories, 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:

Name Extensions / “format” Import Package Export Package Type Note
Archive files (handled by tar) bzip2 / xz / tar utils utils Default
Gzip files gz / gzip base base Default
Zip files zip utils utils Default
CSVY (CSV + YAML metadata header) csvy data.table data.table Default
Comma-separated data csv data.table data.table Default
Comma-separated data (European) csv2 data.table data.table Default
Data Interchange Format dif utils Default
Epiinfo epiinfo / rec foreign Default
Excel excel / xlsx readxl writexl Default
Excel (Legacy) xls readxl Default
Fixed-width format data fwf readr utils Default
Fortran data fortran utils Default No recognized extension
Google Sheets googlesheets data.table Default As comma-separated data
Minitab minitab / mtp foreign Default
Pipe-separated data psv data.table data.table Default
R syntax r base base Default
SAS sas / sas7bdat haven haven Default Export is deprecated
SAS XPORT xport / xpt haven haven Default
SPSS sav / spss haven haven Default
SPSS (compressed) zsav haven haven Default
SPSS Portable por haven Default
Saved R objects rda / rdata base base Default
Serialized R objects rds base base Default
Stata dta / stata haven haven Default
Systat syd / systat foreign Default
Tab-separated data / tsv / txt data.table data.table Default
Text Representations of R Objects dump base base Default
Weka Attribute-Relation File Format arff / weka foreign foreign Default
XBASE database files dbf foreign foreign Default
Apache Arrow (Parquet) parquet arrow arrow Suggest
Clipboard clipboard clipr clipr Suggest default is tsv
EViews eviews / wf1 hexView Suggest
Fast Storage fst fst fst Suggest
Feather R/Python interchange format feather arrow arrow Suggest
Graphpad Prism pzfx pzfx pzfx Suggest
HTML Tables htm / html xml2 xml2 Suggest
JSON json jsonlite jsonlite Suggest
Matlab mat / matlab rmatio rmatio Suggest
OpenDocument Spreadsheet ods readODS readODS Suggest
OpenDocument Spreadsheet (Flat) fods readODS readODS Suggest
Serialized R objects (Quick) qs qs qs Suggest
Shallow XML documents xml xml2 xml2 Suggest
YAML yaml / yml yaml yaml Suggest

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

  • 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

More Repositories

1

margins

An R Port of Stata's 'margins' Command
R
261
star
2

slopegraph

Edward Tufte-Inspired Slopegraphs
R
186
star
3

meme

Meme Generation in R
R
118
star
4

data-versioning

Collecting thoughts about data versioning
107
star
5

tttable

Defining a grammar of tables
92
star
6

prediction

Tidy, Type-Safe 'prediction()' Methods
R
87
star
7

csvy

Import and Export CSV Data With a YAML Metadata Header
R
53
star
8

conjoint-example

An Example Conjoint Experimental Design in Qualtrics
HTML
50
star
9

cregg

Simple Conjoint Analyses, Tidying, and Visualization
R
50
star
10

make-example

An example of using make for a data analysis project
R
32
star
11

pdfcount

An R Shiny App to Count Words in a PDF Document
R
28
star
12

htlatex-example

Demo of htlatex for LaTeX to Word (.docx) file conversion
TeX
26
star
13

reggie

Stata-like Regression Functionality for R
R
24
star
14

UNF

Tools for Creating Universal Numeric Fingerprints for Data
R
21
star
15

designcourse

Course materials for "Research Design in Political Science"
TeX
18
star
16

references

All of my bibliographic references
TeX
18
star
17

Depends

Quick demo of the risks of using 'Depends' in R packages
R
14
star
18

lumendb

Lumen Database (Chilling Effects) API Client
R
11
star
19

Rcourse

R Course Materials
HTML
11
star
20

surveycourse

Course materials for seminar on survey research methods
TeX
11
star
21

rite

rite: The Right Editor to Write R
R
8
star
22

lectures

Collection of files for miscellaneous talks and lectures
TeX
8
star
23

rrcourse

Course Materials for Reproducible Research Workshop
TeX
8
star
24

rpublons

Client for Publons.com
R
7
star
25

poliscitoys

Toy datasets for political science methods
7
star
26

lookfor

R port of Stata's lookfor command
R
7
star
27

rio.db

A Database Extension for 'rio'
R
6
star
28

webuse

Import Stata 'webuse' Datasets
R
6
star
29

surveyexpcourse

Materials for a short course on Survey Experiments
TeX
6
star
30

expcourse

Course materials for "Experimentation and Causal Inference"
TeX
6
star
31

responserates

AAPOR Survey Response Rates
R
6
star
32

arco

Select colors from the Tcl/tk `chooseColors` widget
R
5
star
33

textcolor

R
5
star
34

opinioncourse

Materials for "Public Opinion, Political Psychology, and Citizenship"
TeX
5
star
35

sparktex

Generate LaTeX sparklines in R
R
4
star
36

choco-r-devel

Chocolatey package for r-devel
PowerShell
4
star
37

apsa-leeper.bst

BibTeX style file for political science (adapted from apsa.bst)
4
star
38

interaction-plot

A simple shiny app for examining interaction effects
R
4
star
39

choco-rtools

Chocolatey package for Rtools
PowerShell
4
star
40

conjoint-subgroups

Reproduction Materials for "Measuring Subgroup Preferences in Conjoint Experiments"
TeX
4
star
41

statcompcourse

Course materials for Introduction to Statistical Computing
TeX
3
star
42

Impressive

A fork of Martin Fiedler's Impressive presentation application
Python
3
star
43

websurveycourse

Materials for a short course on online surveys and survey experiments
TeX
3
star
44

statapkg

A repo experimenting with installation of Stata packages from GitHub
HTML
2
star
45

regcourse

Some course materials about regression
TeX
2
star
46

GK2011

Gaines and Kuklinski (2011) Estimators for Hybrid Experiments
R
2
star
47

crandatapkgs

Find Data-Only Packages on CRAN
R
2
star
48

mcode

Functions to merge and recode across multiple variables
R
2
star
49

dual-axis

dual y-axis figures are usually terrible
R
2
star
50

excel-vba-userform-marking

Some VBA for an Excel UserForm used for essay marking at LSE
Visual Basic
2
star
51

mturkr-article

Materials for a short article about MTurkR
TeX
2
star
52

openfda

R
1
star
53

praiserror

A Sarcastic and Demoralizing Error Handler
R
1
star
54

ted-principle

Tidy Experimental Data: The TED Principle
HTML
1
star
55

ciplotm

Modified Version of Nick Cox's 'ciplot' ado for Stata
Stata
1
star
56

xyllabus

A Syllabus Markup Language
1
star
57

rcompiler

Automatically exported from code.google.com/p/rcompiler
C++
1
star
58

hints

Automatically exported from code.google.com/p/hints
TeX
1
star