Skip to content

Commit 143e964

Browse files
authored
Faster write_delim (#394)
Writing the table to a tempfile and then reading back is a lot faster than writing the table to a `textConnection()`.
1 parent 8cd1bfb commit 143e964

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

crates/ark/src/modules/positron/r_data_explorer.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,13 @@ export_selection <- function(x, format = c("csv", "tsv", "html"), include_header
294294
}
295295

296296
write_delim <- function(x, delim, include_header) {
297-
con <- textConnection("text_val", "w", encoding="UTF-8")
298-
defer(close(con))
297+
tmp <- tempfile()
298+
defer(unlink(tmp))
299299

300-
utils::write.table(x, con, sep = delim, row.names = FALSE, col.names = include_header, quote = FALSE, na = "")
301-
paste0(textConnectionValue(con), collapse = "\n")
300+
utils::write.table(x, tmp, sep = delim, row.names = FALSE, col.names = include_header, quote = FALSE, na = "")
301+
# We use size - 1 because we don't want to read the last newline character
302+
# that creates problems when pasting the content in spreadsheets
303+
readChar(tmp, file.info(tmp)$size - 1L)
302304
}
303305

304306
write_html <- function(x, include_header) {

0 commit comments

Comments
 (0)