diff --git a/R/gist.R b/R/gist.R index 0e290d8..b836509 100644 --- a/R/gist.R +++ b/R/gist.R @@ -42,7 +42,9 @@ post_gist_oauth <- function(gist, create_gist <- function(filenames, description = "", extras = NULL, public = TRUE){ if (!is.null(extras)) filenames <- c(filenames, extras) files = lapply(filenames, function(file){ - x = list(content = paste(readLines(file, warn = F), collapse = "\n")) + con <- file(file, "r", encoding = "UTF-8") + on.exit(close(con)) + list(content = paste(readLines(con, warn = F), collapse = "\n")) }) names(files) = basename(filenames) body = list(description = description, public = public, files = files) diff --git a/R/rChartsClass.R b/R/rChartsClass.R index 822ebb3..4201e3a 100644 --- a/R/rChartsClass.R +++ b/R/rChartsClass.R @@ -107,7 +107,7 @@ rCharts = setRefClass('rCharts', list(params = 'list', lib = 'character', }, save = function(destfile = 'index.html', ...){ 'Save chart as a standalone html page' - writeLines(.self$render(...), destfile) + write_file(.self$render(...), destfile) }, show = function(mode_ = NULL, ..., extra_files = NULL){ mode_ = getMode(mode_) @@ -115,7 +115,7 @@ rCharts = setRefClass('rCharts', list(params = 'list', lib = 'character', static = { dir.create(temp_dir <- tempfile(pattern = 'rCharts')) static_ = grepl("^http", LIB$url) || is.null(viewer <- getOption('viewer')) - writeLines(.self$render(..., static = static_), + write_file(.self$render(..., static = static_), tf <- file.path(temp_dir, 'index.html') ) if (!static_){ @@ -189,7 +189,7 @@ rCharts = setRefClass('rCharts', list(params = 'list', lib = 'character', # take_screenshot(htmlFile, tools::file_path_sans_ext(imgFile)) if (!is.null(.self$srccode)){ codeFile = file.path(tempdir(), 'code.R'); on.exit(unlink(codeFile)) - writeLines(.self$srccode, con = codeFile) + write_file(.self$srccode, codeFile) files = c(htmlFile, codeFile) } else { files = htmlFile diff --git a/R/utils.R b/R/utils.R index e199539..c489226 100644 --- a/R/utils.R +++ b/R/utils.R @@ -3,7 +3,7 @@ #' @export make_chart <- function(text){ - writeLines(text, con="input.R") + write_file(text, "input.R") chart = source('input.R', local = TRUE)$value # chart$set(width = 700) # chart$setTemplate(page = 'rChart2.html') @@ -177,6 +177,20 @@ read_file <- function(file, warn = F, ...){ paste(readLines(file, warn = warn, ...), collapse = "\n") } +#' Write lines into a file using specific encoding +#' +#' @param text A character vector +#' @param output path to text file that needs to be written +#' @param encoding The name of the encoding to be used. See the Encoding section in \code{\link{connections}}. +#' @param ... other parameters to be passed to \code{\link{writeLines}} +#' @keywords internal +#' @noRd +write_file <- function(text, output, encoding = "UTF-8", ...){ + con <- file(output, "w", encoding = encoding) + writeLines(text, con, ...) + close(con) +} + #' Read contents of a system file into a character string #' #' @params ... character vectors, specifying subdirectory and file(s) within some package.