diff --git a/R/writeODS.R b/R/writeODS.R index c4c3155..a88f8e6 100644 --- a/R/writeODS.R +++ b/R/writeODS.R @@ -37,12 +37,11 @@ } .cell_out <- function(type, value, con) { - escaped_value <- .escape_xml(value) .write_as_utf8(stringi::stri_join("", escaped_value, + .write_as_utf8(stringi::stri_join("\" table:style-name=\"ce1\">", value, "", sep = ""), con) } @@ -56,6 +55,13 @@ sprintf('', .escape_xml(sheet), cols) } +.flatten <- function(x, column_type) { + if (column_type == "string") { + return(.escape_xml(as.character(x))) + } + as.character(x) +} + .write_sheet_con <- function(x, con, sheet = "Sheet1", row_names = FALSE, col_names = FALSE, na_as_string = FALSE, padding = FALSE) { cmax <- force(if(ncol(x) > 1024) { 16384 } else { 1024 }) types <- unlist(lapply(x, function(x) class(x)[1])) @@ -63,12 +69,12 @@ colj <- seq_len(NCOL(x)) cols <- ncol(x) if (row_names) { - rownames_x <- rownames(x) + rownames_x <- .escape_xml(rownames(x)) cols <- cols + 1 } rows <- nrow(x) if (col_names) { - colnames_x <- colnames(x) + colnames_x <- .escape_xml(colnames(x)) rows <- rows + 1 } if (padding) { @@ -90,7 +96,7 @@ } .write_as_utf8("", con) } - x_list <- lapply(x, as.character) + x_list <- mapply(.flatten, x = x, column_type = types, SIMPLIFY = FALSE) for (i in seq_len(NROW(x))) { ## create a row .write_as_utf8("", con) diff --git a/benchmark/write_ods.md b/benchmark/write_ods.md index 2802a22..6be4f05 100644 --- a/benchmark/write_ods.md +++ b/benchmark/write_ods.md @@ -4,7 +4,7 @@ date() ``` - [1] "Wed Aug 9 23:32:12 2023" + [1] "Sat Aug 12 15:38:13 2023" ``` r devtools::load_all() @@ -27,7 +27,7 @@ system.time(write_ods(df1, path = tempfile(fileext = ".ods"))) ``` user system elapsed - 0.354 0.004 0.362 + 0.283 0.016 0.298 Large df @@ -36,7 +36,7 @@ system.time(write_ods(nycflights13::flights)) ``` user system elapsed - 94.700 0.592 95.384 + 77.828 0.621 78.560 ``` r sessionInfo() @@ -65,7 +65,7 @@ sessionInfo() [1] stats graphics grDevices utils datasets methods base other attached packages: - [1] readODS_2.0.0 testthat_3.1.10 + [1] readODS_2.0.1 testthat_3.1.10 loaded via a namespace (and not attached): [1] utf8_1.2.3 xml2_1.3.5 stringi_1.7.12 hms_1.1.3