Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force UTF8 encoding during writeLines #552

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 3 additions & 1 deletion R/gist.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions R/rChartsClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ 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_)
switch(mode_,
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_){
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand Down