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 functionalityThe 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()
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.
Importing data is handled with one function, import()
:
library("rio") import("starwars.xlsx")
import("starwars.csv")
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.
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")
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:
suppressPackageStartupMessages(library(data.table))
rf <- data.table(rio:::rio_formats)[!input %in% c(",", ";", "|", "\\t") & type %in% c("import", "suggest", "archive"),] short_rf <- rf[, paste(input, collapse = " / "), by = format_name] type_rf <- unique(rf[,c("format_name", "type", "import_function", "export_function", "note")]) feature_table <- short_rf[type_rf, on = .(format_name)] colnames(feature_table)[2] <- "signature" setorder(feature_table, "type", "format_name") feature_table$import_function <- stringi::stri_extract_first(feature_table$import_function, regex = "[a-zA-Z0-9\\.]+") feature_table$import_function[is.na(feature_table$import_function)] <- "" feature_table$export_function <- stringi::stri_extract_first(feature_table$export_function, regex = "[a-zA-Z0-9\\.]+") feature_table$export_function[is.na(feature_table$export_function)] <- "" feature_table$type <- ifelse(feature_table$type %in% c("suggest"), "Suggest", "Default") feature_table <- feature_table[,c("format_name", "signature", "import_function", "export_function", "type", "note")] colnames(feature_table) <- c("Name", "Extensions / \"format\"", "Import Package", "Export Package", "Type", "Note") knitr::kable(feature_table)
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.
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')"
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"))
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()
unlink("mtcars.csv") unlink("mtcars.dta") unlink("mtcars.sav") unlink("mtcars.rds") unlink("mtcars.xlsx") unlink("mtcars.tsv.zip") unlink("mtcars.tsv") unlink("iris.tsv")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.