Skip to content

Commit

Permalink
add a wrapper function system3() based on system2() to mark the c…
Browse files Browse the repository at this point in the history
…haracter output of `system2()` as UTF-8 if appropriate

to avoid problems like rstudio/blogdown@e4a3f38
  • Loading branch information
yihui committed Nov 17, 2021
1 parent 765b2c8 commit f8020f1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.28.5
Version: 0.28.6
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export(strict_list)
export(stringsAsStrings)
export(strings_please)
export(submit_cran)
export(system3)
export(tinify)
export(tojson)
export(tree)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Added functions `rest_api_raw()` and `rest_api()` to get data from a REST API; also added the function `github_api()` to get data from the Github API based on `rest_api_raw()`.

- Added a wrapper function `system3()` based on `system2()` to mark the character output of `system2()` as UTF-8 if appropriate.

# CHANGES IN xfun VERSION 0.28

- Added a new function `url_accessible()` to test if a URL can be downloaded.
Expand Down
25 changes: 25 additions & 0 deletions R/command.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
#' Run \code{system2()} and mark its character output as UTF-8 if appropriate
#'
#' This is a wrapper function based on \code{system2()}. If \code{system2()}
#' returns character output (e.g., with the argument \code{stdout = TRUE}),
#' check if the output is encoded in UTF-8. If it is, mark it with UTF-8
#' explicitly.
#' @param ... Passed to \code{\link{system2}()}.
#' @return The value returned by \code{system2()}.
#' @export
#' @examplesIf interactive()
#' a = shQuote(c('-e', 'print(intToUtf8(c(20320, 22909)))'))
#' x2 = system2('Rscript', a, stdout = TRUE)
#' Encoding(x2) # unknown
#'
#' x3 = xfun::system3('Rscript', a, stdout = TRUE)
#' # encoding of x3 should be UTF-8 if the current locale is UTF-8
#' !l10n_info()[['UTF-8']] || Encoding(x3) == 'UTF-8' # should be TRUE
system3 = function(...) {
res = system2(...)
if (is.character(res)) {
if (all(is_utf8(res))) Encoding(res) = 'UTF-8'
}
if (is.integer(res) && res == 0) invisible(res) else res
}

#' Run OptiPNG on all PNG files under a directory
#'
#' Call the command \command{optipng} via \code{system2()} to optimize all PNG
Expand Down
30 changes: 30 additions & 0 deletions man/system3.Rd

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

0 comments on commit f8020f1

Please sign in to comment.