-
Notifications
You must be signed in to change notification settings - Fork 306
/
write.table.R
179 lines (166 loc) · 6.18 KB
/
write.table.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File src/library/utils/R/write.table.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2019 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
write.table <-
function (x, file = "", append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
col.names = TRUE, qmethod = c("escape", "double"),
fileEncoding = "")
{
qmethod <- match.arg(qmethod)
if(is.logical(quote) && (length(quote) != 1L || is.na(quote)))
stop("'quote' must be 'TRUE', 'FALSE' or numeric")
## quote column names unless quote == FALSE (see help).
quoteC <- if(is.logical(quote)) quote else TRUE
qset <- is.logical(quote) && quote
if(!is.data.frame(x) && !is.matrix(x)) x <- data.frame(x)
makeRownames <- isTRUE(row.names)
## need col names if col.names is TRUE or NA
makeColnames <- is.logical(col.names) && !identical(FALSE, col.names)
if(is.matrix(x)) {
## fix up dimnames as as.data.frame would
p <- ncol(x)
d <- dimnames(x)
if(is.null(d)) d <- list(NULL, NULL)
if(is.null(d[[1L]]) && makeRownames) d[[1L]] <- seq_len(nrow(x))
if(is.null(d[[2L]]) && makeColnames && p > 0L)
d[[2L]] <- paste0("V", 1L:p)
if(qset)
quote <- if(is.character(x)) seq_len(p) else numeric()
} else { ## data.frame
if(qset)
quote <- if(length(x))
which(unlist(lapply(x, function(x)
is.character(x) || is.factor(x))))
else numeric()
## fix up embedded matrix columns into separate cols:
if(any(vapply(x,
function(z)
length(dim(z)) == 2 && dim(z)[2L] > 1,
NA))) {
c1 <- names(x)
x <- as.matrix(x, rownames.force = makeRownames)
d <- dimnames(x)
if(qset) {
ord <- match(c1, d[[2L]], 0L)
quote <- ord[quote]; quote <- quote[quote > 0L]
}
}
else
d <- list(if(makeRownames) row.names(x),
if(makeColnames) names(x))
p <- ncol(x)
}
nocols <- p == 0L
if(is.logical(quote)) # must be false
quote <- NULL
else if(is.numeric(quote)) {
if(any(quote < 1L | quote > p))
stop("invalid numbers in 'quote'")
} else
stop("invalid 'quote' specification")
rn <- FALSE
rnames <- NULL
if(is.logical(row.names)) {
if(row.names) {rnames <- as.character(d[[1L]]); rn <- TRUE}
} else {
rnames <- as.character(row.names)
rn <- TRUE
if(length(rnames) != nrow(x))
stop("invalid 'row.names' specification")
}
if(!is.null(quote) && rn) # quote the row names
quote <- c(0, quote)
if(is.logical(col.names)) {
if(!rn && is.na(col.names))
stop("'col.names = NA' makes no sense when 'row.names = FALSE'")
col.names <- if(is.na(col.names) && rn) c("", d[[2L]])
else if(col.names) d[[2L]] else NULL
} else {
col.names <- as.character(col.names)
if(length(col.names) != p)
stop("invalid 'col.names' specification")
}
if(file == "") file <- stdout()
else if(is.character(file)) {
file <- if(nzchar(fileEncoding))
file(file, ifelse(append, "a", "w"), encoding = fileEncoding)
else file(file, ifelse(append, "a", "w"))
on.exit(close(file))
} else if(!isOpen(file, "w")) {
open(file, "w")
on.exit(close(file))
}
if(!inherits(file, "connection"))
stop("'file' must be a character string or connection")
qstring <- # quoted embedded quote string
switch(qmethod,
"escape" = '\\"',
"double" = '""')
if(!is.null(col.names)) {
if(append)
warning("appending column names to file")
if(quoteC)
col.names <- paste0("\"", gsub('"', qstring, col.names, fixed=TRUE),
"\"")
writeLines(paste(col.names, collapse = sep), file, sep = eol)
}
if (nrow(x) == 0L) return(invisible())
if (nocols && !rn) return(cat(rep.int(eol, NROW(x)), file=file, sep=""))
## convert list matrices to character - maybe not much use?
if(is.matrix(x) && !is.atomic(x)) mode(x) <- "character"
if(is.data.frame(x)) {
## convert columns we can't handle in C code
x[] <- lapply(x, function(z) {
if(is.object(z) && !is.factor(z)) as.character(z) else z
})
}
invisible(.External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,
na, dec, as.integer(quote), qmethod != "double"))
}
write.csv <- function(...)
{
Call <- match.call(expand.dots = TRUE)
for(argname in c("append", "col.names", "sep", "dec", "qmethod"))
if(!is.null(Call[[argname]]))
warning(gettextf("attempt to set '%s' ignored", argname),
domain = NA)
rn <- eval.parent(Call$row.names)
Call$append <- NULL
Call$col.names <- if(is.logical(rn) && !rn) TRUE else NA
Call$sep <- ","
Call$dec <- "."
Call$qmethod <- "double"
Call[[1L]] <- as.name("write.table")
eval.parent(Call)
}
write.csv2 <- function(...)
{
Call <- match.call(expand.dots = TRUE)
for(argname in c("append", "col.names", "sep", "dec", "qmethod"))
if(!is.null(Call[[argname]]))
warning(gettextf("attempt to set '%s' ignored", argname),
domain = NA)
rn <- eval.parent(Call$row.names)
Call$append <- NULL
Call$col.names <- if(is.logical(rn) && !rn) TRUE else NA
Call$sep <- ";"
Call$dec <- ","
Call$qmethod <- "double"
Call[[1L]] <- as.name("write.table")
eval.parent(Call)
}