Learn R Programming

rio (version 0.4.16)

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 will be created. Must specify file and/or format. Shortcuts include: “,” (for comma-separated values), “;” (for semicolon-separated values), “|” (for pipe-separated values), and “dump” for dump.
...
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: “mtcars.csv.tar”, “mtcars.csv.zip”, or “mtcars.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), using write.table with row.names = FALSE.
  • Comma-separated data (.csv), using write.csv with row.names = FALSE.
  • CSVY (CSV with a YAML metadata header) using write_csvy. The YAML header lines are preceded by R comment symbols (\#) by default; this can be turned off by passing a comment_header = FALSE argument to export.
  • Pipe-separated data (.psv), using write.table with sep = '|' and row.names = FALSE.
  • Feather R/Python interchange format (.feather), using feather::write_feather
  • Fixed-width format data (.fwf), using write.table with row.names = FALSE, quote = FALSE, and col.names = FALSE
  • Serialized R objects (.rds), using saveRDS
  • Saved R objects (.RData), using save
  • JSON (.json), using toJSON
  • YAML (.yml), using as.yaml
  • Stata (.dta), using write_dta. Note that variable/column names containing dots (.) are not allowed and will produce an error.
  • SPSS (.sav), using write_sav
  • SAS (.sas7bdat), using write_sas.
  • "XBASE" database files (.dbf), using write.dbf
  • Weka Attribute-Relation File Format (.arff), using write.arff
  • R syntax object (.R), using dput (by default) or dump (if format = 'dump'
  • Excel (.xlsx), using write.xlsx
  • XML (.xml), using a custom method based on xml_add_child to create a simple XML tree and write_xml to write to disk.
  • HTML (.html), using a custom method based on xml_add_child to create a simple HTML table and write_xml to write to disk.
  • Clipboard export (on Windows and Mac OS), using write.table with row.names = FALSE

See Also

.export, import, convert

Examples

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

## Not run: 
# # Stata does not recognize variables names with '.'
# export(mtcars, "mtcars.dta")
# ## End(Not run)

# specify only `format` argument
"mtcars.dta" %in% dir()
export(mtcars, format = "stata")
"mtcars.dta" %in% dir()

# specify `file` and `format` to override default format
export(mtcars, file = "mtcars.txt", format = "csv")

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

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

# write data to .R syntax file and append additional data
export(mtcars, 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(mtcars, "mtcars.csv.zip")

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

Run the code above in your browser using DataLab