Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged

F136 #139

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions R/writeODS.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
}

.cell_out <- function(type, value, con) {
escaped_value <- .escape_xml(value)
.write_as_utf8(stringi::stri_join("<table:table-cell office:value-type=\"", type, sep = ""), con)
if (type != "string") {
.write_as_utf8(stringi::stri_join("\" office:value=\"", escaped_value, sep = ""), con)
.write_as_utf8(stringi::stri_join("\" office:value=\"", value, sep = ""), con)
}
.write_as_utf8(stringi::stri_join("\" table:style-name=\"ce1\"><text:p>", escaped_value,
.write_as_utf8(stringi::stri_join("\" table:style-name=\"ce1\"><text:p>", value,
"</text:p></table:table-cell>",
sep = ""), con)
}
Expand All @@ -56,19 +55,26 @@
sprintf('<table:table table:name="%s" table:style-name="ta1"><table:table-column table:style-name="co1" table:number-columns-repeated="%d" table:default-cell-style-name="ce1"/>', .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]))
types <- ifelse(types %in% c("integer", "numeric"), "float", "string")
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) {
Expand All @@ -90,7 +96,7 @@
}
.write_as_utf8("</table:table-row>", 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("<table:table-row table:style-name=\"ro1\">", con)
Expand Down
8 changes: 4 additions & 4 deletions benchmark/write_ods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand 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

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down