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

Widget list #110

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
S3method(as.tags,htmlwidget)
S3method(print,htmlwidget)
S3method(print,suppress_viewer)
S3method(print,widgetList)
export(JS)
export(appendContent)
export(as.widgetList)
export(createWidget)
export(onRender)
export(onStaticRenderComplete)
Expand All @@ -14,4 +16,5 @@ export(scaffoldWidget)
export(shinyRenderWidget)
export(shinyWidgetOutput)
export(sizingPolicy)
export(widgetList)
import(htmltools)
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ htmlwidgets 0.5.2 (unreleased)
e.g. larger embedded leaflet maps). Stack size can also be controlled
by the pandoc.stack.size option.

* Added new functions `widgetList()` and `as.widgetList()` to combine multiple
HTML widgets into a list, and render all of them when the list is printed in
the R console or R Markdown or Shiny apps. (#110)

htmlwidgets 0.5
-----------------------------------------------------------------------
Expand Down
66 changes: 46 additions & 20 deletions R/htmlwidgets.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
#' @export
print.htmlwidget <- function(x, ..., view = interactive()) {
# call html_print with the viewer
html_print(htmltools::as.tags(x, standalone=TRUE), viewer = if (view) viewerFunc(x))

# return value
invisible(x)
}

viewerFunc <- function(x) {
# if we have a viewer then forward viewer pane height (if any)
viewer <- getOption("viewer")
if (!is.null(viewer)) {
viewerFunc <- function(url) {

# get the requested pane height (it defaults to NULL)
paneHeight <- x$sizingPolicy$viewer$paneHeight
if (is.null(viewer)) utils::browseURL else function(url) {
# get the requested pane height (it defaults to NULL)
paneHeight <- x$sizingPolicy$viewer$paneHeight

# convert maximize to -1 for compatibility with older versions of rstudio
# (newer versions convert 'maximize' to -1 interally, older versions
# will simply ignore the height if it's less than zero)
if (identical(paneHeight, "maximize"))
paneHeight <- -1
# convert maximize to -1 for compatibility with older versions of rstudio
# (newer versions convert 'maximize' to -1 interally, older versions will
# simply ignore the height if it's less than zero)
if (identical(paneHeight, "maximize"))
paneHeight <- -1

# call the viewer
viewer(url, height = paneHeight)
}
} else {
viewerFunc <- utils::browseURL
# call the viewer
viewer(url, height = paneHeight)
}

# call html_print with the viewer
html_print(htmltools::as.tags(x, standalone=TRUE), viewer = if (view) viewerFunc)

# return value
invisible(x)
}

#' @export
Expand Down Expand Up @@ -119,6 +116,35 @@ addHook <- function(x, hookName, jsCode) {
}


#' Widget lists
#'
#' Combine multiple HTML widgets into a list, and all widgets will be rendered
#' on the same page when the widget list is printed.
#' @param ... HTML widget objects created by \code{createWidget()} or widget
#' packages, and (optionally) HTML elements created from \pkg{htmltools}
#' @export
widgetList <- function(...) {
as.widgetList(list(...))
}

#' @param x a list to be coerced to a widget list
#' @rdname widgetList
#' @export
as.widgetList <- function(x) {
if (!is.list(x)) stop("'x' must be a list")
for (i in seq_along(x)) {
if (!inherits(x[[i]], c('htmlwidget', 'html', 'shiny.tag', 'shiny.tag.list')))
stop("The element ", i, " in 'x' is not an HTML widget or tag")
}
structure(x, class = 'widgetList')
}

#' @export
print.widgetList <- function(x, ..., view = interactive()) {
html_print(lapply(x, as.tags), viewer = if (view) viewerFunc(list()))
invisible(x)
}

toHTML <- function(x, standalone = FALSE, knitrOptions = NULL) {

sizeInfo <- resolveSizing(x, x$sizingPolicy, standalone = standalone, knitrOptions = knitrOptions)
Expand Down
6 changes: 5 additions & 1 deletion R/knitr-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ registerMethods <- function(methods) {
# htmlwidgets and knitr are loaded.
registerMethods(list(
# c(package, genname, class)
c("knitr", "knit_print", "htmlwidget")
c("knitr", "knit_print", "htmlwidget"),
c("knitr", "knit_print", "widgetList")
))
}

Expand All @@ -42,3 +43,6 @@ knit_print.htmlwidget <- function(x, ..., options = NULL) {
knitr::knit_print(toHTML(x, standalone = FALSE, knitrOptions = options), options = options, ...)
}

knit_print.widgetList <- function(x, ..., options = NULL) {
structure(lapply(x, knitr::knit_print, ..., options = options), class = 'knit_asis_list')
}
22 changes: 22 additions & 0 deletions man/widgetList.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/htmlwidgets.R
\name{widgetList}
\alias{as.widgetList}
\alias{widgetList}
\title{Widget lists}
\usage{
widgetList(...)

as.widgetList(x)
}
\arguments{
\item{...}{HTML widget objects created by \code{createWidget()} or widget
packages, and (optionally) HTML elements created from \pkg{htmltools}}

\item{x}{a list to be coerced to a widget list}
}
\description{
Combine multiple HTML widgets into a list, and all widgets will be rendered
on the same page when the widget list is printed.
}