loading...

Write R data into a file, in different formats.

write(
  data,
  file = "data",
  ncolumns = if (is.character(data)) 1 else 5,
  append = FALSE,
  sep = " ",
  type = NULL,
  fun_list = NULL,
  x,
  ...
)

# S3 method for write_function_subset
.DollarNames(x, pattern = "")

Arguments

data

An object to write in a file. The accepted class depends on what the delegated function expects (in many cases, a data.frame or tibble is just fine). If type is not provided, a data.frame is not suitable because only an atomic vector can be provided. Give a matrix instead, if you want to write tabular data, or provide type = "txt" for instance.

file

The path to the file to write to. If type is not provide, a connection, or a character string naming the file to write to. If ""``, print to the standard output connection. If it is "|cmd", the output is piped to the command given by cmd`.

ncolumns

The number of columns to write the data in when type is provided, this is by-passed.

append

If TRUE and type is not provided, the data are appended to the connection.

sep

A string used to separate columns. Using sep = "\t" gives tab delimited output; default is " " when type is not provide, or the default provided by the delegated function if this parameter is present there.

type

The type (format) of data to read.

fun_list

The table with correspondence of the types, read, and write functions.

x

Same as data=, for compatibility with base::write(). Please, do not use both data= and x= as the same time, or an error will be generated.

...

Further arguments passed to the write function, when type is explicitly provided.

pattern

A regular expression to list matching names.

Value

data is returned invisibly (on the contrary to base::write()

which returns NULL).

Details

This function is designed to be fully compatible with base::write(), while allowing to specify type also, and get a more interesting behavior in this case. Hence, when type is not provided, either with write(type = ...), or write$...(), the default code is used and a plain text file wit fields separated by spaces (be default) is written. When type is provided, then the exportation is delegated to specific functions (see data_types()) to write the data in different formats.

Author

Philippe Grosjean phgrosjean@sciviews.org

Examples

# Always specify type to delegate to more sophisticated functions
# (type = NULL explicitly indicated meaning: "guess from file extension")
urchin <- read("urchin_bio", package = "data.io")
write(urchin, "urchin_temporary.csv", type = NULL)
# To use a format more easily readable by Excel
write(urchin, "urchin_temporary.csv", type = "xlcsv")
# ... equivalently (and more compact)
write$xlcsv(urchin, "urchin_temporary.csv")
# Tidy up
unlink("urchin_temporary.csv")

# Write in Excel format
write$xlsx(urchin, "urchin_temporary.xlsx")
# Tidy up
unlink("urchin_temporary.xlsx")

# Use base::write() code to output atomic vectors (and matices) in text files
# when you don't specify type=
mat1 <- matrix(1:12, nrow = 4)
# To get a similar presentation in the file, you have to do:
write(t(mat1), "my_temporary_data.txt", ncolumns = 3)
file.show("my_temporary_data.txt")
# Tidy up
unlink("my_temporary_data.txt")
rm(mat1)