Skip to content

Commit

Permalink
add a function format_bytes() borrowed from tinytex:::auto_size() and…
Browse files Browse the repository at this point in the history
… built on top of format.object_size()
  • Loading branch information
yihui committed May 13, 2021
1 parent c1a90e8 commit 28c4889
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -30,6 +30,7 @@ export(exit_call)
export(file_exists)
export(file_ext)
export(file_string)
export(format_bytes)
export(from_root)
export(github_releases)
export(grep_sub)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -4,6 +4,8 @@

- Added a `news2md()` function to convert package news to the Markdown format. This is mainly for converting the plain-text `NEWS` file and the `NEWS.Rd` file to `NEWS.md`.

- Added a `format_bytes()` function to format numbers of bytes using a specified unit, e.g., `1024` can be formatted as `1 Kb`.

- When using `pkg_load2()` in an **renv** project, it will use `renv::install()` to install missing packages by default to take advantage of **renv**'s caching feature (thanks, @chunyunma @cderv, #52).

- `upload_win_builder()` no longer requires the system command `curl` to be available; if `curl` is not available, the R package **curl** will be used instead, which means this R package must be installed. In addition to uploading to the `ftp` server of win-builder, it's also possible to upload to <https://win-builder.r-project.org/upload.aspx>: call `upload_win_builder(..., server = 'https')`. This change was made so that it would be possible to continue to upload to win-builder in case it should stop supporting `ftp` (CRAN has discouraged package authors from using `ftp://`).
Expand Down
16 changes: 16 additions & 0 deletions R/utils.R
Expand Up @@ -260,3 +260,19 @@ connect_pipes = function(x) {
}

pkg_file = function(...) system.file(..., package = 'xfun', mustWork = TRUE)

#' Format numbers of bytes using a specified unit
#'
#' Call the S3 method \code{format.object_size()} to format numbers of bytes.
#' @param x A numeric vector (each element represents a number of bytes).
#' @param units,... Passed to \code{\link[=format.object_size]{format}()}.
#' @return A character vector.
#' @export
#' @examples
#' xfun::format_bytes(c(1, 1024, 2000, 1e6, 2e8))
#' xfun::format_bytes(c(1, 1024, 2000, 1e6, 2e8), units = 'KB')
format_bytes = function(x, units = 'auto', ...) {
vapply(x, function(b) {
format(structure(b, class = 'object_size'), units = units, ...)
}, character(1))
}
23 changes: 23 additions & 0 deletions man/format_bytes.Rd

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

0 comments on commit 28c4889

Please sign in to comment.