Skip to content

Commit

Permalink
tweak write.table() performance (PR#18500)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@84133 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
smeyer committed Mar 31, 2023
1 parent 7d4be67 commit bc9f8a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 5 additions & 0 deletions doc/NEWS.Rd
Expand Up @@ -271,6 +271,11 @@
an F test depending on whether the dispersion is fixed or
free. Test statistics can be suppressed by giving argument
\code{test} a false logical value.

\item The performance of \code{write.table()} has been improved
for data frames with a large number of unclassed columns.
(Thanks to Gabriel Becker's \PR{18500}, prompted by a report from
Toby Dylan Hocking on the R-devel mailing list.)
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/library/utils/R/write.table.R
@@ -1,7 +1,7 @@
# 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
# Copyright (C) 1995-2023 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
Expand Down Expand Up @@ -135,9 +135,8 @@ function (x, file = "", append = FALSE, quote = TRUE, sep = " ",
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
})
needconv <- vapply(x, function(z) is.object(z) && !is.factor(z), TRUE)
x[needconv] <- lapply(x[needconv], as.character)
}

invisible(.External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,
Expand Down

0 comments on commit bc9f8a0

Please sign in to comment.