export {rio}R Documentation

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. (An exception to this is that x can be a list of data frames if the output file format is an Excel .xlsx workbook, .Rdata file, or HTML file. See examples.)

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. See examples.

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 ...

When exporting a data set that contains label attributes (e.g., if imported from an SPSS or Stata file) to a plain text file, characterize can be a useful pre-processing step that records value labels into the resulting file (e.g., export(characterize(x), "file.csv")) rather than the numeric values.

Value

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

See Also

.export, characterize, import, convert

Examples

library("datasets")
# 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 multiple objects to Rdata
export(list(mtcars = mtcars, iris = iris), "mtcars.rdata")
export(c("mtcars", "iris"), "mtcars.rdata")

# 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 to an Excel workbook
## Not run: 
  ## export a single data frame
  export(mtcars, "mtcars.xlsx")
  
  ## export a list of data frames as worksheets
  export(list(a = mtcars, b = iris), "multisheet.xlsx")

  ## export, adding sheet to an existing workbook
  export(iris, "mtcars.xlsx", which = "iris", overwrite = FALSE)

## End(Not run)

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

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

[Package rio version 0.5.10 Index]