Learn R Programming

rio (version 0.4.5)

export: Export

Description

Write data.frame to a file

Usage

export(x, file, format, ...)

Arguments

x
A data frame or matrix to be written into a file.
file
A character string naming a file. Must specify file and/or format.
format
An optional character string containing the file format, which can be used to override the format inferred from file or, in lieu of specifying file, a file with the symbol name of x and the specified file extension w
...
Additional arguments for the underlying export functions.

Value

  • The name of the output file as a character string (invisibly).

Details

This function exports a data frame or matrix into a file with file format based on the file extension (or the manually specified format, if format is specified). The output file can be to a compressed directory, simply by adding an appropriate additional extensiont to the file argument, such as: iris.csv.tar, iris.csv.zip, or iris.csv.gz. export supports many file formats. See the documentation for the underlying export functions for optional arguments that can be passed via ...
  • Tab-separated data (.tsv), usingwrite.tablewithrow.names = FALSE. %Setting \code{fwrite = TRUE} will rely on \code{\link[data.table]{fwrite}} for much faster export.
  • Comma-separated data (.csv), usingwrite.csvwithrow.names = FALSE. %Setting \code{fwrite = TRUE} will rely on \code{\link[data.table]{fwrite}} for much faster export.
  • http://csvy.org/{CSVY} (CSV with a YAML metadata header) usingwrite.csvwithrow.names = FALSEandstringsAsFactors = FALSE(or, iffread = TRUE,fread). The YAML header lines are preceded by R comment symbols (\#) by default; this can be turned off by passing acomment_header = FALSEargument toexport. %Setting \code{fwrite = TRUE} will rely on \code{\link[data.table]{fwrite}} for much faster export.
  • Pipe-separated data (.psv), usingwrite.tablewithsep = '|'androw.names = FALSE. %Setting \code{fwrite = TRUE} will rely on \code{\link[data.table]{fwrite}} for much faster export.
  • Feather R/Python interchange format (.feather), usingwrite_feather
  • Fixed-width format data (.fwf), usingwrite.tablewithrow.names = FALSE,quote = FALSE, andcol.names = FALSE
  • Serialized R objects (.rds), usingsaveRDS
  • Saved R objects (.RData), usingsave
  • JSON (.json), usingtoJSON
  • YAML (.yml), usingas.yaml
  • Stata (.dta), usingwrite_dta
  • SPSS (.sav), usingwrite_sav
  • "XBASE" database files (.dbf), usingwrite.dbf
  • Weka Attribute-Relation File Format (.arff), usingwrite.arff
  • R syntax object (.R), usingdput(by default) ordump(ifformat = 'dump'
  • Excel (.xlsx), usingwrite.xlsx
  • XML (.xml), using a custom method to create a simple XML tree
  • HTML (.html), using a custom method to create a single-table HTML file
  • Clipboard export (on Windows and Mac OS), usingwrite.tablewithrow.names = FALSE

Examples

Run this code
# specify only `file` argument
export(iris, "iris.csv")

# specify only `format` argument
"iris.dta"export(iris, format = "stata")
"iris.dta"
# specify `file` and `format` to override default format
export(iris, file = "iris.txt", format = "csv")

# export to JSON
export(iris, "iris.json")

# pass arguments to underlying export function
export(iris, "iris.csv", col.names = FALSE)

# write data to .R syntax file and append additional data
export(iris, file = "data.R", format = "dump")
export(mtcars, file = "data.R", format = "dump", append = TRUE)
source("data.R", echo = TRUE)

# write data to a zip-compressed CSV
export(iris, "iris.csv.zip")

# cleanup
unlink("iris.csv")
unlink("iris.dta")
unlink("iris.json")
unlink("data.R")
unlink("iris.csv.zip")

Run the code above in your browser using DataLab