Skip to content

Commit

Permalink
Fix #318 (#331)
Browse files Browse the repository at this point in the history
* Does this work 3.6 Windows

* Fix #318

* fix yaml?

JSON is working now, why not YAML?

* Use yaml::write_yaml

* Disable YAML test; add disclaimer

* Update NEWS.md [no ci]
  • Loading branch information
chainsawriot authored Sep 1, 2023
1 parent 0bb9d61 commit 14e3bb0
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* Update Examples to make them more realistic #327
* Bug fixes
- ... is correctly passed for exporting ODS and feather #318
- POTENTIALLY BREAKING: JSON are exported in UTF-8 by default; solved encoding issues on
Windows R < 4.2. This won't affect any modern R installation where UTF-8 is the default. #318
- POTENTIALLY BREAKING: YAML are exported using yaml::write_yaml(). But it can't pass the UTF-8 check on older systems.
Disclaimer added. #318
* Declutter
- remove the obsolete data.table option #323
- write all documentation blocks in markdown #311
Expand Down
2 changes: 1 addition & 1 deletion R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#' \item OpenDocument Spreadsheet (.ods), using [readODS::write_ods()]. (Currently only single-sheet exports are supported.)
#' \item HTML (.html), using a custom method based on [xml2::xml_add_child()] to create a simple HTML table and [xml2::write_xml()] to write to disk.
#' \item XML (.xml), using a custom method based on [xml2::xml_add_child()] to create a simple XML tree and [xml2::write_xml()] to write to disk.
#' \item YAML (.yml), using [yaml::as.yaml()]
#' \item YAML (.yml), using [yaml::write_yaml()], default to write the content with UTF-8. Might not work on some older systems, e.g. default Windows locale for R <= 4.2.
#' \item Clipboard export (on Windows and Mac OS), using [utils::write.table()] with `row.names = FALSE`
#' }
#'
Expand Down
6 changes: 3 additions & 3 deletions R/export_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export_delim <- function(file, x, fwrite = TRUE, sep = "\t", row.names = FALSE,
' colClasses = c("', paste0(col_classes, collapse = '","') ,'"))\n'), domain = NA)
}
}
cat(paste0("#", capture.output(write.csv(dict, row.names = FALSE, quote = FALSE))), file = file, sep = "\n")
.write_as_utf8(paste0("#", capture.output(write.csv(dict, row.names = FALSE, quote = FALSE))), file = file, sep = "\n")
utils::write.table(dat, file = file, append = TRUE, row.names = row.names, sep = sep, quote = quote,
col.names = col.names, ...)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ export_delim <- function(file, x, fwrite = TRUE, sep = "\t", row.names = FALSE,
#' @export
.export.rio_json <- function(file, x, ...) {
.check_pkg_availability("jsonlite")
cat(jsonlite::toJSON(x, ...), file = file)
.write_as_utf8(jsonlite::toJSON(x, ...), file = file)
}

#' @importFrom foreign write.arff
Expand Down Expand Up @@ -303,7 +303,7 @@ export_delim <- function(file, x, fwrite = TRUE, sep = "\t", row.names = FALSE,
#' @export
.export.rio_yml <- function(file, x, ...) {
.check_pkg_availability("yaml")
cat(yaml::as.yaml(x, ...), file = file)
yaml::write_yaml(x, file = file, ...)
}

#' @export
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ twrap <- function(value, tag) {
}
return(invisible(NULL))
}

.write_as_utf8 <- function(text, file, sep = "") {
writeLines(enc2utf8(text), con = file, sep = sep, useBytes = TRUE)
}
2 changes: 1 addition & 1 deletion man/export.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test_format_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ test_that("Export to JSON (non-data frame)", {
expect_true(length(import("list.json")) == 2L)
})

test_that("utf-8", {
content <- c("\"", "\u010d", "\u0161", "\u00c4", "\u5b57", "\u30a2", "\u30a2\u30e0\u30ed")
x <- data.frame(col = content)
tempjson <- tempfile(fileext = ".json")
y <- import(export(x, tempjson))
testthat::expect_equal(content, y$col)
})

unlink("iris.json")
unlink("list.json")
10 changes: 10 additions & 0 deletions tests/testthat/test_format_yml.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ test_that("Import from YAML", {
expect_identical(import("iris.yml")$Species, as.character(iris$Species))
})

test_that("utf-8", {
skip_if(getRversion() <= "4.2")
content <- c("\"", "\u010d", "\u0161", "\u00c4", "\u5b57", "\u30a2", "\u30a2\u30e0\u30ed")
x <- data.frame(col = content)
tempyaml <- tempfile(fileext = ".yaml")
y <- import(export(x, tempyaml))
testthat::expect_equal(content, y$col)
})


unlink("iris.yml")

0 comments on commit 14e3bb0

Please sign in to comment.