Skip to content

Commit

Permalink
Merge pull request #224 from Lchiffon/master
Browse files Browse the repository at this point in the history
update write function ( utf8 encoding)
  • Loading branch information
sckott authored Jun 21, 2018
2 parents fb5e03a + 1582026 commit 36a8702
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/docs_bulk_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ make_bulk_update <- function(df, index, type, counter, path = NULL) {

data <- lapply(tmp, jsonlite::toJSON, na = "null", auto_unbox = TRUE)
tmpf <- if (is.null(path)) tempfile("elastic__") else path
writeLines(paste(metadata, data, sep = "\n"), tmpf)
write_utf8(paste(metadata, data, sep = "\n"), tmpf)
invisible(tmpf)
}
2 changes: 1 addition & 1 deletion R/docs_bulk_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ make_bulk <- function(df, index, type, counter, es_ids, path = NULL) {
)
data <- jsonlite::toJSON(df, collapse = FALSE, na = "null", auto_unbox = TRUE)
tmpf <- if (is.null(path)) tempfile("elastic__") else path
writeLines(paste(metadata, data, sep = "\n"), tmpf)
write_utf8(paste(metadata, data, sep = "\n"), tmpf)
invisible(tmpf)
}

Expand Down
8 changes: 8 additions & 0 deletions R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ assert <- function(x, y) {
}
}
}


write_utf8 = function(text, con, ...) {
# prevent re-encoding the text in the file() connection in writeLines()
# https://kevinushey.github.io/blog/2018/02/21/string-encoding-and-r/
opts = options(encoding = 'native.enc'); on.exit(options(opts), add = TRUE)
writeLines(enc2utf8(text), con, ..., useBytes = TRUE)
}

0 comments on commit 36a8702

Please sign in to comment.