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

Get dependencies from an htmlwidget object #255

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(as.tags,htmlwidget)
S3method(getDependency,character)
S3method(getDependency,htmlwidget)
S3method(print,htmlwidget)
S3method(print,suppress_viewer)
export(JS)
Expand Down
11 changes: 3 additions & 8 deletions R/htmlwidgets.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ toHTML <- function(x, standalone = FALSE, knitrOptions = NULL) {
)
}
)
html <- htmltools::attachDependencies(html,
c(widget_dependencies(class(x)[1], attr(x, 'package')),
x$dependencies)
html <- htmltools::attachDependencies(
html, c(getDependency(x), x$dependencies)
)

htmltools::browsable(html)
Expand All @@ -234,10 +233,6 @@ widget_html <- function(name, package, id, style, class, inline = FALSE, ...){
}
}

widget_dependencies <- function(name, package){
getDependency(name, package)
}

# Generates a <script type="application/json"> tag with the JSON-encoded data,
# to be picked up by htmlwidgets.js for static rendering.
widget_data <- function(x, id, ...){
Expand Down Expand Up @@ -386,7 +381,7 @@ shinyWidgetOutput <- function(outputId, name, width, height, package = name,
)

# attach dependencies
dependencies = widget_dependencies(name, package)
dependencies = getDependency(name, package)
htmltools::attachDependencies(html, dependencies)
}

Expand Down
14 changes: 12 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ toJSON <- function(x) {
structure(res, class = 'json')
}

#' Get js and css dependencies for a htmlwidget
#' Get js and css dependencies for an htmlwidget
#'
#' @param name name of the widget.
#' @param name either an htmlwidget object or the name of a widget.
#' @param package name of the package, defaults to the widget name.
#' @export
getDependency <- function(name, package = name){
UseMethod("getDependency")
}

#' @export
getDependency.htmlwidget <- function(name, package = name){
getDependency.character(class(name)[1], package = attr(name, "package"))
}

#' @export
getDependency.character <- function(name, package = name){
config = sprintf("htmlwidgets/%s.yaml", name)
jsfile = sprintf("htmlwidgets/%s.js", name)

Expand Down
6 changes: 3 additions & 3 deletions man/getDependency.Rd

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