• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

R/H25-Nov-2018-1,9211,280

build/H03-May-2022-

inst/H25-Nov-2018-199150

man/H06-Aug-2018-550463

po/H09-Mar-2016-10171

tests/H03-Jan-2016-931712

vignettes/H25-Nov-2018-260191

DESCRIPTIONH A D26-Nov-20182.7 KiB5756

MD5H A D26-Nov-20185.2 KiB9190

NAMESPACEH A D25-Nov-20183.7 KiB136134

NEWS.mdH A D25-Nov-201816.8 KiB378234

README.mdH A D06-Aug-201815 KiB236171

README.md

1---
2title: 'rio: A Swiss-Army Knife for Data I/O'
3output: github_document
4---
5
6<img src="man/figures/logo.png" align="right" />
7
8The aim of **rio** is to make data file I/O in R as easy as possible by implementing four simple functions in Swiss-army knife style:
9
10 - `import()` provides a painless data import experience by automatically choosing the appropriate import/read function based on file extension (or a specified `format` argument)
11 - `import_list()` imports a list of data frames from a multi-object file (Excel workbook, .Rdata files, zip directory, or HTML file)
12 - `export()` provides the same painless file recognition for data export/write functionality
13 - `convert()` wraps `import()` and `export()` to allow the user to easily convert between file formats (thus providing a FOSS replacement for programs like [Stat/Transfer](https://www.stattransfer.com/) or [Sledgehammer](https://www.mtna.us/#/products/sledgehammer)). Relatedly, [Luca Braglia](https://lbraglia.github.io/) has created a Shiny app called [rioweb](https://github.com/lbraglia/rioweb) that provides access to the file conversion features of rio. [GREA](https://github.com/Stan125/GREA/) is an RStudio add-in that provides an interactive interface for reading in data using rio.
14
15## Examples
16
17Because **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.
18
19### Export
20
21Exporting data is handled with one function, `export()`:
22
23
24```r
25library("rio")
26
27export(mtcars, "mtcars.csv") # comma-separated values
28export(mtcars, "mtcars.rds") # R serialized
29export(mtcars, "mtcars.sav") # SPSS
30```
31
32A 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.:
33
34
35```r
36export(mtcars, "mtcars.tsv.zip")
37```
38
39As of rio v0.5.0, `export()` can also write multiple data farmes to respective sheets of an Excel workbook or an HTML file:
40
41
42```r
43export(list(mtcars = mtcars, iris = iris), file = "mtcars.xlsx")
44```
45
46### Import
47
48Importing data is handled with one function, `import()`:
49
50
51```r
52x <- import("mtcars.csv")
53y <- import("mtcars.rds")
54z <- import("mtcars.sav")
55
56# confirm data match
57all.equal(x, y, check.attributes = FALSE)
58```
59
60```
61## [1] TRUE
62```
63
64```r
65all.equal(x, z, check.attributes = FALSE)
66```
67
68```
69## [1] TRUE
70```
71
72Note: Because of inconsistencies across underlying packages, the data.frame returned by `import` might vary slightly (in variable classes and attributes) depending on file type.
73
74In rio v0.5.0, a new list-based import function was added. This 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):
75
76
77```r
78str(import_list("mtcars.xlsx"))
79```
80
81```
82## List of 2
83##  $ mtcars:'data.frame':	32 obs. of  11 variables:
84##   ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
85##   ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
86##   ..$ disp: num [1:32] 160 160 108 258 360 ...
87##   ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
88##   ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
89##   ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
90##   ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
91##   ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
92##   ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
93##   ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
94##   ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
95##  $ iris  :'data.frame':	150 obs. of  5 variables:
96##   ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
97##   ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
98##   ..$ 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 ...
99##   ..$ 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 ...
100##   ..$ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...
101```
102
103### Convert
104
105The `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.
106
107
108```r
109convert("mtcars.sav", "mtcars.dta")
110```
111
112It 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:
113
114```
115Rscript -e "rio::convert('iris.dta', 'iris.csv')"
116```
117
118
119
120## Supported file formats
121
122**rio** supports a wide range of file formats. To keep the package slim, all non-essential 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:
123
124```R
125install_formats()
126```
127
128The full list of supported formats is below:
129
130| Format | Typical Extension | Import Package | Export Package | Installed by Default |
131| ------ | --------- | -------------- | -------------- | -------------------- |
132| Comma-separated data | .csv | [**data.table**](https://cran.r-project.org/package=data.table) | [**data.table**](https://cran.r-project.org/package=data.table) | Yes |
133| Pipe-separated data | .psv | [**data.table**](https://cran.r-project.org/package=data.table) | [**data.table**](https://cran.r-project.org/package=data.table) | Yes |
134| Tab-separated data | .tsv | [**data.table**](https://cran.r-project.org/package=data.table) | [**data.table**](https://cran.r-project.org/package=data.table) | Yes |
135| SAS | .sas7bdat | [**haven**](https://cran.r-project.org/package=haven) | [**haven**](https://cran.r-project.org/package=haven) | Yes |
136| SPSS | .sav | [**haven**](https://cran.r-project.org/package=haven) | [**haven**](https://cran.r-project.org/package=haven) | Yes |
137| Stata | .dta | [**haven**](https://cran.r-project.org/package=haven) | [**haven**](https://cran.r-project.org/package=haven) | Yes |
138| SAS XPORT | .xpt | [**haven**](https://cran.r-project.org/package=haven) | [**haven**](https://cran.r-project.org/package=haven) | Yes |
139| SPSS Portable | .por | [**haven**](https://cran.r-project.org/package=haven) |  | Yes |
140| Excel | .xls | [**readxl**](https://cran.r-project.org/package=readxl) |  | Yes |
141| Excel | .xlsx | [**readxl**](https://cran.r-project.org/package=readxl) | [**openxlsx**](https://cran.r-project.org/package=openxlsx) | Yes |
142| R syntax | .R | **base** | **base** | Yes |
143| Saved R objects | .RData, .rda | **base** | **base** | Yes |
144| Serialized R objects | .rds | **base** | **base** | Yes |
145| Epiinfo | .rec | [**foreign**](https://cran.r-project.org/package=foreign) |  | Yes |
146| Minitab | .mtp | [**foreign**](https://cran.r-project.org/package=foreign) |  | Yes |
147| Systat | .syd | [**foreign**](https://cran.r-project.org/package=foreign) |  | Yes |
148| "XBASE" database files | .dbf | [**foreign**](https://cran.r-project.org/package=foreign) | [**foreign**](https://cran.r-project.org/package=foreign) | Yes |
149| Weka Attribute-Relation File Format | .arff | [**foreign**](https://cran.r-project.org/package=foreign) | [**foreign**](https://cran.r-project.org/package=foreign) | Yes |
150| Data Interchange Format | .dif | **utils** |  | Yes |
151| Fortran data | no recognized extension | **utils** |  | Yes |
152| Fixed-width format data | .fwf | **utils** | **utils** | Yes |
153| gzip comma-separated data | .csv.gz | **utils** | **utils** | Yes |
154| CSVY (CSV + YAML metadata header) | .csvy | [**csvy**](https://cran.r-project.org/package=csvy) | [**csvy**](https://cran.r-project.org/package=csvy) | No |
155| EViews | .wf1 | [**hexView**](https://cran.r-project.org/package=hexView) |  | No |
156| Feather R/Python interchange format | .feather | [**feather**](https://cran.r-project.org/package=feather) | [**feather**](https://cran.r-project.org/package=feather) | No |
157| Fast Storage | .fst | [**fst**](https://cran.r-project.org/package=fst) | [**fst**](https://cran.r-project.org/package=fst) | No |
158| JSON | .json | [**jsonlite**](https://cran.r-project.org/package=jsonlite) | [**jsonlite**](https://cran.r-project.org/package=jsonlite) | No |
159| Matlab | .mat | [**rmatio**](https://cran.r-project.org/package=rmatio) | [**rmatio**](https://cran.r-project.org/package=rmatio) | No |
160| OpenDocument Spreadsheet | .ods | [**readODS**](https://cran.r-project.org/package=readODS) | [**readODS**](https://cran.r-project.org/package=readODS) | No |
161| HTML Tables | .html | [**xml2**](https://cran.r-project.org/package=xml2) | [**xml2**](https://cran.r-project.org/package=xml2) | No |
162| Shallow XML documents | .xml | [**xml2**](https://cran.r-project.org/package=xml2) | [**xml2**](https://cran.r-project.org/package=xml2) | No |
163| YAML | .yml | [**yaml**](https://cran.r-project.org/package=yaml) | [**yaml**](https://cran.r-project.org/package=yaml) | No |
164| Clipboard | default is tsv | [**clipr**](https://cran.r-project.org/package=clipr) | [**clipr**](https://cran.r-project.org/package=clipr) | No |
165| [Google Sheets](https://www.google.com/sheets/about/) | as Comma-separated data |  |  |  |
166
167Additionally, 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.
168
169## Package Philosophy
170
171The core advantage of **rio** is that it makes assumptions that the user is probably willing to make. Eight of these are important:
172
173 1. **rio** uses the file extension of a file name to determine what kind of file it is. This is the same logic used by Windows OS, for example, in determining what application is associated with a given file type. By removing the need to manually match a file type (which a beginner may not recognize) to a particular import or export function, **rio** allows almost all common data formats to be read with the same function. And if a file extension is incorrect, users can force a particular import method by specifying the `format` argument. Other packages do this as well, but **rio** aims to be more complete and more consistent than each:
174
175   - [**reader**](https://cran.r-project.org/package=reader) handles certain text formats and R binary files
176   - [**io**](https://cran.r-project.org/package=io) offers a set of custom formats
177   - [**ImportExport**](https://cran.r-project.org/package=ImportExport) focuses on select binary formats (Excel, SPSS, and Access files) and provides a Shiny interface.
178   - [**SchemaOnRead**](https://cran.r-project.org/package=SchemaOnRead) iterates through a large number of possible import methods until one works successfully
179
180 2. **rio** uses `data.table::fread()` for text-delimited files to automatically determine the file format regardless of the extension. So, a CSV that is actually tab-separated will still be correctly imported. It's also crazy fast.
181
182 3. **rio**, wherever possible, does not import character strings as factors.
183
184 4. **rio** supports web-based imports natively, including from SSL (HTTPS) URLs, from shortened URLs, from URLs that lack proper extensions, and from (public) Google Documents Spreadsheets.
185
186 5. **rio** imports from from single-file .zip and .tar archives automatically, without the need to explicitly decompress them. Export to compressed directories is also supported.
187
188 6. **rio** wraps a variety of faster, more stream-lined I/O packages than those provided by base R or the **foreign** package. It uses [**data.table**](https://cran.r-project.org/package=data.table) for delimited formats, [**haven**](https://cran.r-project.org/package=haven) for SAS, Stata, and SPSS files, smarter and faster fixed-width file import and export routines, and [**readxl**](https://cran.r-project.org/package=readxl) and [**openxlsx**](https://cran.r-project.org/package=openxlsx) for reading and writing Excel workbooks.
189
190 7. **rio** stores metadata from rich file formats (SPSS, Stata, etc.) in variable-level attributes in a consistent form regardless of file type or underlying import function. These attributes are identified as:
191
192     - `label`: a description of variable
193     - `labels`: a vector mapping numeric values to character strings those values represent
194     - `format`: a character string describing the variable storage type in the original file
195
196      The `gather_attrs()` function makes it easy to move variable-level attributes to the data frame level (and `spread_attrs()` reverses that gathering process). These can be useful, especially, during file conversion to more easily modify attributes that are handled differently across file formats. As an example, the following idiom can be used to trim SPSS value labels to the 32-character maximum allowed by Stata:
197
198      ```R
199      dat <- gather_attrs(rio::import("data.sav"))
200      attr(dat, "labels") <- lapply(attributes(dat)$labels, function(x) {
201          if (!is.null(x)) {
202              names(x) <- substring(names(x), 1, 32)
203          }
204          x
205      })
206      export(spread_attrs(dat), "data.dta")
207      ```
208
209      In addition, two functions (added in v0.5.5) provide easy ways to create character and factor variables from these "labels" attributes. `characterize()` converts a single variable or all variables in a data frame that have "labels" attributes into character vectors based on the mapping of values to value labels. `factorize()` does the same but returns factor variables. This can be especially helpful for converting these rich file formats into open formats (e.g., `export(characterize(import("file.dta")), "file.csv")`.
210
211 8. **rio** imports and exports files based on an internal S3 class infrastructure. This means that other packages can contain extensions to **rio** by registering S3 methods. These methods should take the form `.import.rio_X()` and `.export.rio_X()`, where `X` is the file extension of a file type. An example is provided in the [rio.db package](https://github.com/leeper/rio.db).
212
213## Package Installation
214
215[![CRAN Version](https://www.r-pkg.org/badges/version/rio)](https://cran.r-project.org/package=rio)
216![Downloads](https://cranlogs.r-pkg.org/badges/rio)
217[![Travis-CI Build Status](https://travis-ci.org/leeper/rio.png?branch=master)](https://travis-ci.org/leeper/rio)
218[![Appveyor Build status](https://ci.appveyor.com/api/projects/status/40ua5l06jw0gjyjb?svg=true)](https://ci.appveyor.com/project/leeper/rio)
219[![codecov.io](https://codecov.io/github/leeper/rio/coverage.svg?branch=master)](https://codecov.io/github/leeper/rio?branch=master)
220
221The package is available on [CRAN](https://cran.r-project.org/package=rio) and can be installed directly in R using `install.packages()`. You may want to run `install_formats()` after the first installation.
222
223```R
224install.packages("rio")
225install_formats()
226```
227
228The latest development version on GitHub can be installed using:
229
230```R
231if (!require("remotes")){
232    install.packages("remotes")
233}
234remotes::install_github("leeper/rio")
235```
236