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

Skip to content
Merged
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: readODS
Type: Package
Title: Read and Write ODS Files
Version: 1.8.0
Version: 1.8.1
Authors@R: c(person("Gerrit-Jan", "Schutten", role = c("aut"), email = "[email protected]"), person("Chung-hong", "Chan", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-6232-7530")), person("Thomas J.", "Leeper", role = c("aut"), email = "[email protected]", comment = c(ORCID = "0000-0003-4097-6326")), person("John", "Foster", role = c("ctb"), email = "[email protected]"), person("Sergio", "Oller", role = c("ctb")), person("Jim", "Hester", role = c("ctb"), email = "[email protected]", comment = c(ORCID = "0000-0002-2739-7082")), person("Stephen", "Watts", role = c("ctb")), person("Arthur", "Katossky", role = c("ctb")), person("Stas", "Malavin", role = c("ctb")), person("Duncan", "Garmonsway", role = c("ctb")), person("Mehrad", "Mahmoudian", role = c("ctb")), person("Matt", "Kerlogue", role = c("ctb")), person("Detlef", "Steuer", role = c("aut"), email = "[email protected]", comment = c(ORCID = "0000-0003-2676-5290")), person("Michal", "Lauer", role = c("ctb"), email = "[email protected]"))
Description: Read ODS (OpenDocument Spreadsheet) into R as data frame. Also support writing data frame into ODS file.
URL: https://github.com/ropensci/readODS
Expand All @@ -21,7 +21,7 @@ Suggests:
knitr,
rmarkdown
License: GPL-3
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
VignetteBuilder: knitr
Expand Down
26 changes: 17 additions & 9 deletions R/writeODS.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
sprintf('<table:table table:name="%s" table:style-name="ta1"><table:table-column table:style-name="co1" table:number-columns-repeated="16384" table:default-cell-style-name="ce1"/>', .escape_xml(sheet))
}

.write_sheet_con <- function(x, con, sheet = "Sheet1", row_names = FALSE, col_names = FALSE) {
.write_sheet_con <- function(x, con, sheet = "Sheet1", row_names = FALSE, col_names = FALSE, na_as_string = FALSE) {
cat(.gen_sheet_tag(sheet), file = con)
types <- unlist(lapply(x, class))
types <- ifelse(types %in% c("integer", "numeric"), "float", "string")
Expand All @@ -80,30 +80,36 @@
.cell_out(type = "string", value = rownames(x)[i], con = con)
}
for (j in colj) {
.cell_out(type = types[j], value = as.character(x[i, j, drop = TRUE]), con = con)
value <- as.character(x[i, j, drop = TRUE])
if (is.na(value) && na_as_string) {
type <- "string"
} else {
type <- types[j]
}
.cell_out(type = type, value = value, con = con)
}
cat("</table:table-row>", file = con)
}
cat("</table:table>", file = con)
return(invisible(con))
}

.convert_df_to_sheet <- function(x, sheet = "Sheet1", row_names = FALSE, col_names = FALSE) {
.convert_df_to_sheet <- function(x, sheet = "Sheet1", row_names = FALSE, col_names = FALSE, na_as_string = FALSE) {
throwaway_xml_file <- tempfile(fileext = ".xml")
con <- file(file.path(throwaway_xml_file), open="w")
.write_sheet_con(x = x, con = con, sheet = sheet, row_names = row_names, col_names = col_names)
.write_sheet_con(x = x, con = con, sheet = sheet, row_names = row_names, col_names = col_names, na_as_string = na_as_string)
close(con)
return(file.path(throwaway_xml_file))
}

## https://github.com/ropensci/readODS/issues/88
.vfwrite_ods <- function(x, temp_ods_dir, sheet = "Sheet1", row_names = FALSE, col_names = FALSE) {
.vfwrite_ods <- function(x, temp_ods_dir, sheet = "Sheet1", row_names = FALSE, col_names = FALSE, na_as_string = FALSE) {
templatedir <- system.file("template", package = "readODS")
file.copy(dir(templatedir, full.names = TRUE), temp_ods_dir, recursive = TRUE)
con <- file(file.path(temp_ods_dir, "content.xml"), open="w")
cat(.CONTENT[1], file = con)
cat(.CONTENT[2], file = con)
.write_sheet_con(x = x, con = con, sheet = sheet, row_names = row_names, col_names = col_names)
.write_sheet_con(x = x, con = con, sheet = sheet, row_names = row_names, col_names = col_names, na_as_string = na_as_string)
cat(.FOOTER, file = con)
close(con)
}
Expand All @@ -120,6 +126,7 @@
#' @param row_names logical, TRUE indicates that row names of x are to be included in the sheet. Default is FALSE.
#' @param col_names logical, TRUE indicates that column names of x are to be included in the sheet. Default is FALSE.
#' @param verbose logical, if messages should be displayed. Default is FALSE.
#' @param na_as_string logical, TRUE indicates that NAs are written as string. Default is `option("write_ods_na")` (will change to TRUE in the next version).
#' @param overwrite logical, deprecated.
#' @return An ODS file written to the file path location specified by the user. The value of \code{path} is also returned invisibly.
#' @author Detlef Steuer <steuer@@hsu-hh.de>, Thomas J. Leeper <thosjleeper@@gmail.com>, John Foster <john.x.foster@@nab.com.au>, Chung-hong Chan <chainsawtiney@@gmail.com>
Expand All @@ -131,7 +138,7 @@
#' write_ods(PlantGrowth, "mtcars.ods", append = TRUE, sheet = "plant")
#' }
#' @export
write_ods <- function(x, path, sheet = "Sheet1", append = FALSE, update = FALSE, row_names = FALSE, col_names = TRUE, verbose = FALSE, overwrite = NULL) {
write_ods <- function(x, path, sheet = "Sheet1", append = FALSE, update = FALSE, row_names = FALSE, col_names = TRUE, verbose = FALSE, na_as_string = getOption("write_ods_na", default = FALSE), overwrite = NULL) {
if (!is.null(overwrite)) {
warning("overwrite is deprecated. Future versions will always set it to TRUE.")
} else {
Expand All @@ -146,7 +153,7 @@ write_ods <- function(x, path, sheet = "Sheet1", append = FALSE, update = FALSE,
dir.create(temp_ods_dir)
tryCatch({
if (!file.exists(path) | (!append & !update)) {
.vfwrite_ods(x = x, temp_ods_dir = temp_ods_dir, sheet = sheet, row_names = row_names, col_names = col_names)
.vfwrite_ods(x = x, temp_ods_dir = temp_ods_dir, sheet = sheet, row_names = row_names, col_names = col_names, na_as_string = na_as_string)
} else {
## The file must be there.
utils::unzip(path, exdir = temp_ods_dir)
Expand All @@ -169,7 +176,8 @@ write_ods <- function(x, path, sheet = "Sheet1", append = FALSE, update = FALSE,
## Add a new sheet
sheet_node <- xml2::xml_add_child(spreadsheet_node, .silent_add_sheet_node(sheet))
}
throwaway_xml_file <- .convert_df_to_sheet(x = x, sheet = sheet, row_names = row_names, col_names = col_names)
throwaway_xml_file <- .convert_df_to_sheet(x = x, sheet = sheet, row_names = row_names, col_names = col_names,
na_as_string = na_as_string)
xml2::xml_replace(sheet_node, .silent_read_xml(throwaway_xml_file))
## write xml to contentfile
xml2::write_xml(content, contentfile)
Expand Down
3 changes: 3 additions & 0 deletions man/write_ods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/test_write_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ test_that("overwrite, #54", {
## cleanup
file.remove("mydata.ods")
})

test_that("na_as_string, #79", {
temp_odsfile <- tempfile(fileext = ".ods")
temp_odsdir <- tempdir()
na_data <- data.frame(x = c(1.0, NA))
write_ods(na_data, path = temp_odsfile, na_as_string = TRUE, col_names = FALSE)
utils::unzip(temp_odsfile, exdir = temp_odsdir)
contentfile <- file.path(temp_odsdir, "content.xml")
expect_true(grepl("office:value-type=\"string\" office:value=\"NA\"", suppressWarnings(readLines(contentfile))))
temp_odsfile <- tempfile(fileext = ".ods")
temp_odsdir <- tempdir()
na_data <- data.frame(x = c(1.0, NA))
write_ods(na_data, path = temp_odsfile, na_as_string = FALSE, col_names = FALSE)
utils::unzip(temp_odsfile, exdir = temp_odsdir)
contentfile <- file.path(temp_odsdir, "content.xml")
expect_false(grepl("office:value-type=\"string\" office:value=\"NA\"", suppressWarnings(readLines(contentfile))))
})