write | R Documentation |
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 = "")
data |
An object to write in a file. The accepted class depends on what
the delegated function expects (in many cases, a |
file |
The path to the file to write to. If |
ncolumns |
The number of columns to write the data in when |
append |
If |
sep |
A string used to separate columns. Using |
type |
The type (format) of data to read. |
fun_list |
The table with correspondence of the types, read, and write functions. |
x |
Same as |
... |
Further arguments passed to the write function, when |
pattern |
A regular expression to list matching names. |
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.
data
is returned invisibly (on the contrary to base::write()
which returns NULL
).
Philippe Grosjean phgrosjean@sciviews.org
data_types()
, read()
, write_csv()
, base::write()
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.