Write Data to a File (original) (raw)
write {base} | R Documentation |
---|
Description
Write data x
to a file or other [connection](../../base/help/connection.html)
.
As it simply calls [cat](../../base/help/cat.html)()
, less formatting happens than with [print](../../base/help/print.html)()
ing. If x
is a matrix you need to transpose it (and typically setncolumns
) to get the columns in file
the same as those in the internal representation.
Whereas atomic vectors ([numeric](../../base/help/numeric.html)
, [character](../../base/help/character.html)
, etc, including matrices) are written plainly, i.e., without any names, less simple vector-like objects such as "[factor](../../base/help/factor.html)"
,"[Date](../../base/help/Date.html)"
, or "[POSIXt](../../base/help/POSIXt.html)"
may be[format](../../base/help/format.html)
ted to character before writing.
Usage
write(x, file = "data",
ncolumns = if(is.character(x)) 1 else 5,
append = FALSE, sep = " ")
Arguments
x | the data to be written out. |
---|---|
file | a connection, or a character string naming the file to write to. If "", print to the standard output connection, i.e., "" is equivalent to stdout()here. When .Platform$OS.type != "windows", and it is "|cmd", the output is piped to the command given by ‘cmd’. |
ncolumns | the number of columns to write the data in. |
append | if TRUE the data x are appended to the connection. |
sep | a string used to separate columns. Using sep = "\t"gives tab delimited output; default is " ". |
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.
See Also
write
is a wrapper for [cat](../../base/help/cat.html)
, which gives further details on the format used.
[write.table](../../utils/html/write.table.html)
for matrix and data frame objects,[writeLines](../../base/help/writeLines.html)
for lines of text, and [scan](../../base/help/scan.html)
for reading data.
[saveRDS](../../base/help/saveRDS.html)
and [save](../../base/help/save.html)
are often preferable (for writing any R objects).
Examples
# Demonstrate default ncolumns, writing to the console
write(month.abb, "") # 1 element per line for "character"
write(stack.loss, "") # 5 elements per line for "numeric"
# Build a file with sequential calls
fil <- tempfile("data")
write("# Model settings", fil)
write(month.abb, fil, ncolumns = 6, append = TRUE)
write("\n# Initial parameter values", fil, append = TRUE)
write(sqrt(stack.loss), fil, append = TRUE)
if(interactive()) file.show(fil)
unlink(fil) # tidy up
[Package _base_ version 4.6.0 Index]