export: Export

Description Usage Arguments Details Value See Also Examples

View source: R/export.R

Description

Write data.frame to a file

Usage

1

Arguments

x

A data frame or matrix to be written into a file. Exceptions to this rule are that x can be a list of data frames if the output file format is an Excel .xlsx workbook, .Rdata file, or HTML file, or a variety of R objects if the output file format is RDS or JSON. See examples.) To export a list of data frames to multiple files, use export_list instead.

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. This can be used to specify non-standard arguments. 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.

Use export_list to export a list of dataframes to separate files.

Value

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

See Also

.export, characterize, import, convert, export_list

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 non-data frame R object to RDS or JSON
export(mtcars$cyl, "mtcars_cyl.rds")
export(list(iris, mtcars), "list.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 NAs to Excel as missing via args passed to `...`
  mtcars$drat <- NA_real_
  mtcars %>% export("tst2.xlsx", keepNA = TRUE)
  
  ## export a list of data frames as worksheets
  export(list(a = mtcars, b = iris), "multisheet.xlsx")

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

## End(Not run)

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

# cleanup
unlink("mtcars.csv")
unlink("mtcars.dta")
unlink("mtcars.txt")
unlink("mtcars_cyl.rds")
unlink("mtcars.rdata")
unlink("data.R")
unlink("mtcars.csv.zip")
unlink("list.json")

rio documentation built on June 21, 2021, 9:07 a.m.