Skip to content

Commit

Permalink
fix #63: check package availability in advance
Browse files Browse the repository at this point in the history
in the case of R CMD check, also send a reminder to add the package to DESCRIPTION
  • Loading branch information
yihui committed Mar 14, 2022
1 parent 385830b commit 5557263
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
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.30.2
Version: 0.30.3
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
9 changes: 7 additions & 2 deletions R/base64.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ base64_encode_r = function(x) {
#' can be used to embed data in HTML documents, e.g., in the \code{src}
#' attribute of the \verb{<img />} tag.
#' @param x A file path.
#' @param type The MIME type of the file, e.g., \code{"image/png"} for a PNG
#' image file.
#' @return A string of the form \verb{data:<media type>;base64,<data>}.
#' @note By default, this function requires the \pkg{mime} package to determine
#' the MIME type of the file.
#' @export
#' @examples
#' logo = xfun:::R_logo()
#' img = htmltools::img(src = xfun::base64_uri(logo), alt = 'R logo')
#' if (interactive()) htmltools::browsable(img)
base64_uri = function(x) {
paste0("data:", mime::guess_type(x), ";base64,", base64_encode(x))
base64_uri = function(x, type = mime::guess_type(x)) {
if (missing(type)) pkg_require('mime')
paste0("data:", type, ";base64,", base64_encode(x))
}
1 change: 1 addition & 0 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ escape_math = function(x) {
#' link
#' if (interactive()) htmltools::browsable(link)
embed_file = function(path, name = basename(path), text = paste('Download', name), ...) {
pkg_require(c('mime', 'htmltools'))
h = paste0("data:", mime::guess_type(path), ";base64,", base64_encode(path))
htmltools::a(text, href = h, download = name, ...)
}
Expand Down
11 changes: 11 additions & 0 deletions R/packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ pkg_attach2 = function(...) pkg_attach(..., install = TRUE)
#' @export
pkg_load2 = function(...) pkg_load(..., install = TRUE)

pkg_require = function(pkgs, which = length(sys.calls()) - 1) {
f = func_name(which)
for (p in pkgs) if (!loadable(p)) stop2(
"The '", p, "' package is required by the function '", f, "' but not available.",
if (is_R_CMD_check()) c(
" If you are developing an R package, you need to declare the dependency on '",
p, "' in the DESCRIPTION file (e.g., in 'Imports')."
)
)
}

pkg_update = function(...) {
update.packages(ask = FALSE, checkBuilt = TRUE, ...)
}
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,9 @@ format_bytes = function(x, units = 'auto', ...) {
format(structure(b, class = 'object_size'), units = units, ...)
}, character(1))
}

# get the function name of the parent call
func_name = function(which = 1) {
x = sys.call(which)[[1]]
deparse(x)[1]
}
9 changes: 8 additions & 1 deletion man/base64_uri.Rd

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

0 comments on commit 5557263

Please sign in to comment.