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 class 'write_function_subset'
.DollarNames(x, pattern = "")
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.
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`.
The number of columns to write the data in when type
is
provided, this is by-passed.
If TRUE
and type
is not provided, the data
are appended
to the connection.
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.
The type (format) of data to read.
The table with correspondence of the types, read, and write functions.
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.
A regular expression to list matching names.
data
is returned invisibly (on the contrary to base::write()
which returns NULL
).
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.
# 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)