Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🐟 A Swiss-Army Knife for Data I/O

NotificationsYou must be signed in to change notification settings

gesistsa/rio

Repository files navigation

CRAN VersionDownloads

Overview

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

  • import() provides a painless data import experience byautomatically choosing the appropriate import/read function based onfile extension (or a specifiedformat argument)
  • export() provides the same painless file recognition for dataexport/write functionality

Installation

The package is available onCRAN and can be installeddirectly in R usinginstall.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:Supportedfile formats)

library(rio)install_formats()

Usage

Becauserio is meant to streamline data I/O, the package isextremely easy to use. Here are some examples of reading, writing, andconverting 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 valuesexport(mtcars,"mtcars.rds")# R serializedexport(mtcars,"mtcars.sav")# SPSS

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

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

export() can also write multiple data frames to respective sheets ofan 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 notinstalled (or loaded) by default. You can check which formats arenot supported via:

show_unsupported_formats()

You can install the suggested packages individually, depending your ownneeds. If you want to install all suggested packages:

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 byrio but that has aknown R implementation will produce an informative error messagepointing to a package and import or export function. Unrecognizedformats will yield a simple “Unrecognized file format” error.

Other functions

Convert

Theconvert() function linksimport() andexport() by constructinga dataframe from the imported file and immediately writing it back todisk.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 userio on the command-line by callingRscript with the-e (expression) argument. For example, to convert afile from Stata (.dta) to comma-separated values (.csv), simply do thefollowing:

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

*_list

import_list() allows users to import a list of data frames from amulti-object file (such as an Excel workbook, .Rdata file, zipdirectory, 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) dataframes to multiple files:

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

Other projects

GUIs

  • datamods providesShiny modules for importing data viario.
  • rioweb that providesaccess to the file conversion features ofrio.
  • GREA is an RStudio add-inthat provides an interactive interface for reading in data usingrio.

Similar packages

  • reader handlescertain text formats and R binary files
  • io offers a set ofcustom formats
  • ImportExportfocuses on select binary formats (Excel, SPSS, and Access files) andprovides a Shiny interface.
  • SchemaOnReaditerates through a large number of possible import methods until oneworks successfully

[8]ページ先頭

©2009-2025 Movatter.jp