Skip to content

Commit a0f1265

Browse files
committed
Avoid unnecessary run-time checks of shiny version and use staticimports to import stuff
1 parent a3aa2b1 commit a0f1265

File tree

4 files changed

+91
-75
lines changed

4 files changed

+91
-75
lines changed

R/htmlwidgets.R

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,9 @@ createWidget <- function(name,
476476
shinyWidgetOutput <- function(outputId, name, width, height, package = name,
477477
inline = FALSE, reportSize = FALSE, reportTheme = FALSE) {
478478

479-
checkShinyVersion()
480-
481479
# Theme reporting requires this shiny feature
482480
# https://github.com/rstudio/shiny/pull/2740/files
483-
if (reportTheme &&
484-
nzchar(system.file(package = "shiny")) &&
485-
packageVersion("shiny") < "1.4.0.9003") {
481+
if (reportTheme && is_installed("shiny", "1.4.0.9003")) {
486482
message("`reportTheme = TRUE` requires shiny v.1.4.0.9003 or higher. Consider upgrading shiny.")
487483
}
488484

@@ -512,7 +508,6 @@ shinyWidgetOutput <- function(outputId, name, width, height, package = name,
512508
#' @rdname htmlwidgets-shiny
513509
#' @export
514510
shinyRenderWidget <- function(expr, outputFunction, env, quoted, cacheHint = "auto") {
515-
checkShinyVersion()
516511
# generate a function for the expression
517512
shiny::installExprFunction(expr, "func", env, quoted)
518513

@@ -588,17 +583,6 @@ shinyRenderWidget <- function(expr, outputFunction, env, quoted, cacheHint = "au
588583
# For the magic behind shiny::installExprFunction()
589584
utils::globalVariables("func")
590585

591-
checkShinyVersion <- function(error = TRUE) {
592-
x <- utils::packageDescription('htmlwidgets', fields = 'Enhances')
593-
r <- '^.*?shiny \\(>= ([0-9.]+)\\).*$'
594-
if (is.na(x) || length(grep(r, x)) == 0 || system.file(package = 'shiny') == '')
595-
return()
596-
v <- gsub(r, '\\1', x)
597-
f <- if (error) stop else packageStartupMessage
598-
if (utils::packageVersion('shiny') < v)
599-
f("Please upgrade the 'shiny' package to (at least) version ", v)
600-
}
601-
602586
# Helper function to create payload
603587
createPayload <- function(instance){
604588
if (!is.null(instance$preRenderHook)){

R/knitr-methods.R

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
1-
# Reusable function for registering a set of methods with S3 manually. The
2-
# methods argument is a list of character vectors, each of which has the form
3-
# c(package, genname, class).
4-
registerMethods <- function(methods) {
5-
lapply(methods, function(method) {
6-
pkg <- method[[1]]
7-
generic <- method[[2]]
8-
class <- method[[3]]
9-
func <- get(paste(generic, class, sep="."))
10-
if (pkg %in% loadedNamespaces()) {
11-
registerS3method(generic, class, func, envir = asNamespace(pkg))
12-
}
13-
setHook(
14-
packageEvent(pkg, "onLoad"),
15-
function(...) {
16-
registerS3method(generic, class, func, envir = asNamespace(pkg))
17-
}
18-
)
19-
})
20-
}
21-
221
.onLoad <- function(...) {
23-
# htmlwidgets provides methods for knitr::knit_print, but knitr isn't a Depends or
24-
# Imports of htmltools, only an Enhances. Therefore, the NAMESPACE file has to
25-
# declare it as an export, not an S3method. That means that R will only know to
26-
# use our methods if htmlwidgets is actually attached, i.e., you have to use
27-
# library(htmlwidgets) in a knitr document or else you'll get escaped HTML in your
28-
# document. This code snippet manually registers our method(s) with S3 once both
29-
# htmlwidgets and knitr are loaded.
30-
registerMethods(list(
31-
# c(package, genname, class)
32-
c("knitr", "knit_print", "htmlwidget")
33-
))
34-
}
35-
36-
.onAttach <- function(...) {
37-
# warn if the version of shiny is lower than what was specified in DESCRIPTION
38-
checkShinyVersion(error = FALSE)
2+
register_s3_method("knitr", "knit_print", "htmlwidget")
3+
register_upgrade_message("shiny", "1.1", error = TRUE)
394
}
405

416
knit_print.htmlwidget <- function(x, ..., options = NULL) {
427
knitr::knit_print(toHTML(x, standalone = FALSE, knitrOptions = options), options = options, ...)
438
}
44-

R/staticimports.R

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Generated by staticimports; do not edit by hand.
2+
# ======================================================================
3+
# Imported from pkg:staticimports
4+
# ======================================================================
5+
6+
`%||%` <- function(a, b) {
7+
if (is.null(a)) b else a
8+
}
9+
10+
get_package_version <- function(pkg) {
11+
ns <- .getNamespace(pkg)
12+
if (is.null(ns)) {
13+
utils::packageVersion(pkg)
14+
} else {
15+
as.package_version(ns$.__NAMESPACE__.$spec[["version"]])
16+
}
17+
}
18+
19+
is_installed <- function(pkg, version = NULL) {
20+
installed <- isNamespaceLoaded(pkg) || nzchar(system.file(package = pkg))
21+
if (is.null(version)) {
22+
return(installed)
23+
}
24+
installed && isTRUE(get_package_version(pkg) >= version)
25+
}
26+
27+
register_s3_method <- function(pkg, generic, class, fun = NULL) {
28+
stopifnot(is.character(pkg), length(pkg) == 1)
29+
stopifnot(is.character(generic), length(generic) == 1)
30+
stopifnot(is.character(class), length(class) == 1)
31+
32+
if (is.null(fun)) {
33+
fun <- get(paste0(generic, ".", class), envir = parent.frame())
34+
} else {
35+
stopifnot(is.function(fun))
36+
}
37+
38+
if (pkg %in% loadedNamespaces()) {
39+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
40+
}
41+
42+
# Always register hook in case pkg is loaded at some
43+
# point the future (or, potentially, but less commonly,
44+
# unloaded & reloaded)
45+
setHook(
46+
packageEvent(pkg, "onLoad"),
47+
function(...) {
48+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
49+
}
50+
)
51+
}
52+
53+
register_upgrade_message <- function(pkg, version, error = FALSE) {
54+
55+
msg <- sprintf(
56+
"This version of '%s' is designed to work with '%s' >= %s.
57+
Please upgrade via install.packages('%s').",
58+
environmentName(environment(register_upgrade_message)),
59+
pkg, version, pkg
60+
)
61+
62+
cond <- if (error) stop else packageStartupMessage
63+
64+
if (pkg %in% loadedNamespaces() && !is_installed(pkg, version)) {
65+
cond(msg)
66+
}
67+
68+
# Always register hook in case pkg is loaded at some
69+
# point the future (or, potentially, but less commonly,
70+
# unloaded & reloaded)
71+
setHook(
72+
packageEvent(pkg, "onLoad"),
73+
function(...) {
74+
if (!is_installed(pkg, version)) cond(msg)
75+
}
76+
)
77+
}

R/utils.R

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# @staticimports pkg:staticimports
2+
# is_installed get_package_version
3+
# register_s3_method register_upgrade_message
4+
# %||%
5+
6+
17
# Copied from shiny 0.14.2
28
toJSON2 <- function(
39
x, ..., dataframe = "columns", null = "null", na = "null", auto_unbox = TRUE,
@@ -14,17 +20,6 @@ toJSON2 <- function(
1420
)
1521
}
1622

17-
if (requireNamespace('shiny') && packageVersion('shiny') >= '0.12.0') local({
18-
tryCatch({
19-
toJSON <- getFromNamespace('toJSON', 'shiny')
20-
args2 <- formals(toJSON2)
21-
args1 <- formals(toJSON)
22-
if (!identical(args1, args2)) {
23-
warning('Check shiny:::toJSON and make sure htmlwidgets:::toJSON is in sync')
24-
}
25-
})
26-
})
27-
2823
toJSON <- function(x) {
2924
if (!is.list(x) || !('x' %in% names(x))) return(toJSON2(x))
3025
func <- attr(x$x, 'TOJSON_FUNC', exact = TRUE)
@@ -49,10 +44,9 @@ getDependency <- function(name, package = name){
4944
# in this cases dependencies should be provided through the
5045
# dependencies argument of createWidget
5146
widgetDep <- list()
52-
if (file.exists(system.file(config, package = package))) {
53-
config = yaml::yaml.load_file(
54-
system.file(config, package = package)
55-
)
47+
yaml_file <- system.file(config, package = package)
48+
if (file.exists(yaml_file)) {
49+
config = yaml::yaml.load_file(yaml_file)
5650
widgetDep <- lapply(config$dependencies, function(l) {
5751
l$package = package
5852
do.call(htmlDependency, l)
@@ -65,7 +59,7 @@ getDependency <- function(name, package = name){
6559
bindingDep <- if (file.exists(system.file(jsfile, package = package))) {
6660
htmlDependency(
6761
name = paste0(name, "-binding"),
68-
version = packageVersion(package),
62+
version = get_package_version(package),
6963
src = "htmlwidgets",
7064
package = package,
7165
script = basename(jsfile),
@@ -76,7 +70,7 @@ getDependency <- function(name, package = name){
7670
c(
7771
list(htmlDependency(
7872
name = "htmlwidgets",
79-
version = packageVersion("htmlwidgets"),
73+
version = get_package_version("htmlwidgets"),
8074
src = "www",
8175
package = "htmlwidgets",
8276
script = "htmlwidgets.js"
@@ -86,9 +80,6 @@ getDependency <- function(name, package = name){
8680
)
8781
}
8882

89-
`%||%` <- function(x, y){
90-
if (is.null(x)) y else x
91-
}
9283

9384
prop <- function(x, path) {
9485
tryCatch({

0 commit comments

Comments
 (0)