| 42333 |
ripley |
1 |
% File src/library/base/man/write.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 84801 |
maechler |
3 |
% Copyright 1995-2023 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{write}
|
| 7081 |
pd |
7 |
\title{Write Data to a File}
|
| 84801 |
maechler |
8 |
\alias{write}
|
| 2 |
r |
9 |
\usage{
|
| 9940 |
pd |
10 |
write(x, file = "data",
|
|
|
11 |
ncolumns = if(is.character(x)) 1 else 5,
|
| 34349 |
maechler |
12 |
append = FALSE, sep = " ")
|
| 2 |
r |
13 |
}
|
| 84801 |
maechler |
14 |
\description{
|
|
|
15 |
Write data \code{x} to a file or other \code{\link{connection}}.
|
|
|
16 |
\cr
|
| 84805 |
hornik |
17 |
As it simply calls \code{\link{cat}()}, less formatting happens than
|
| 85953 |
hornik |
18 |
with \I{\code{\link{print}()}ing}.
|
| 84801 |
maechler |
19 |
If \code{x} is a matrix you need to transpose it (and typically set
|
|
|
20 |
\code{ncolumns}) to get the columns in \code{file} the same as those in
|
|
|
21 |
the internal representation.
|
|
|
22 |
|
|
|
23 |
Whereas atomic vectors (\code{\link{numeric}}, \code{\link{character}},
|
|
|
24 |
etc, including matrices) are written plainly, i.e., without any names,
|
|
|
25 |
less simple vector-like objects such as \code{"\link{factor}"},
|
|
|
26 |
\code{"\link{Date}"}, or \code{"\link{POSIXt}"} may be
|
|
|
27 |
\code{\link{format}}ted to character before writing.
|
|
|
28 |
}
|
| 2 |
r |
29 |
\arguments{
|
| 84801 |
maechler |
30 |
\item{x}{the data to be written out. }
|
| 74519 |
maechler |
31 |
\item{file}{a \code{\link{connection}}, or a character string naming
|
|
|
32 |
the file to write to. If \code{""}, print to the standard output
|
| 87291 |
maechler |
33 |
connection, i.e., \code{""} is equivalent to \code{\link{stdout}()}
|
|
|
34 |
here.
|
| 74821 |
maechler |
35 |
|
|
|
36 |
When \code{\link{.Platform}$OS.type != "windows"}, and it
|
|
|
37 |
is \code{"|cmd"}, the output is piped to the command given
|
| 8701 |
ripley |
38 |
by \file{cmd}.
|
|
|
39 |
}
|
|
|
40 |
\item{ncolumns}{the number of columns to write the data in.}
|
| 40227 |
ripley |
41 |
\item{append}{if \code{TRUE} the data \code{x} are appended to the
|
|
|
42 |
connection.}
|
| 34355 |
maechler |
43 |
\item{sep}{a string used to separate columns. Using \code{sep = "\t"}
|
| 34349 |
maechler |
44 |
gives tab delimited output; default is \code{" "}.}
|
| 2 |
r |
45 |
}
|
| 24300 |
ripley |
46 |
\references{
|
| 88581 |
hornik |
47 |
\bibshow{R:Becker+Chambers+Wilks:1988}
|
| 24300 |
ripley |
48 |
}
|
| 2 |
r |
49 |
\seealso{
|
| 40227 |
ripley |
50 |
\code{write} is a wrapper for \code{\link{cat}}, which gives further
|
|
|
51 |
details on the format used.
|
| 61433 |
ripley |
52 |
|
| 84801 |
maechler |
53 |
\code{\link{write.table}} for matrix and data frame objects,
|
|
|
54 |
\code{\link{writeLines}} for lines of text,
|
| 9940 |
pd |
55 |
and \code{\link{scan}} for reading data.
|
| 84801 |
maechler |
56 |
|
|
|
57 |
\code{\link{saveRDS}} and \code{\link{save}} are often preferable (for
|
|
|
58 |
writing any \R objects).
|
| 2 |
r |
59 |
}
|
|
|
60 |
\examples{
|
| 84801 |
maechler |
61 |
# Demonstrate default ncolumns, writing to the console
|
|
|
62 |
write(month.abb, "") # 1 element per line for "character"
|
|
|
63 |
write(stack.loss, "") # 5 elements per line for "numeric"
|
| 2 |
r |
64 |
|
| 84801 |
maechler |
65 |
# Build a file with sequential calls
|
| 74546 |
maechler |
66 |
fil <- tempfile("data")
|
| 84801 |
maechler |
67 |
write("# Model settings", fil)
|
|
|
68 |
write(month.abb, fil, ncolumns = 6, append = TRUE)
|
|
|
69 |
write("\n# Initial parameter values", fil, append = TRUE)
|
|
|
70 |
write(sqrt(stack.loss), fil, append = TRUE)
|
| 74519 |
maechler |
71 |
if(interactive()) file.show(fil)
|
|
|
72 |
unlink(fil) # tidy up
|
| 2 |
r |
73 |
}
|
| 286 |
maechler |
74 |
\keyword{file}
|
| 11656 |
ripley |
75 |
\keyword{connection}
|