forked from ramnathv/htmlwidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows $width, $height, and $sizingPolicy elements of a widget object to work together to determine the size a widget should be rendered at for any given situation. Still TBD: How to opt out of the RStudio Viewer pane
- Loading branch information
Showing
9 changed files
with
301 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,9 @@ Author: Ramnath Vaidyanathan, Joe Cheng, and JJ Allaire | |
Maintainer: Ramnath Vaidyanathan <[email protected]> | ||
Description: HTML Widgets for R | ||
License: MIT | ||
Imports: | ||
htmltools, | ||
RJSONIO, | ||
yaml | ||
Enhances: shiny, | ||
knitr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
#' @import htmltools | ||
#' @importFrom knitr knit_print | ||
#' @import htmltools RJSONIO | ||
NULL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Reusable function for registering a set of methods with S3 manually. The | ||
# methods argument is a list of character vectors, each of which has the form | ||
# c(package, genname, class). | ||
registerMethods <- function(methods) { | ||
lapply(methods, function(method) { | ||
pkg <- method[[1]] | ||
generic <- method[[2]] | ||
class <- method[[3]] | ||
func <- get(paste(generic, class, sep=".")) | ||
if (pkg %in% loadedNamespaces()) { | ||
registerS3method(generic, class, func, envir = asNamespace(pkg)) | ||
} | ||
setHook( | ||
packageEvent(pkg, "onLoad"), | ||
function(...) { | ||
registerS3method(generic, class, func, envir = asNamespace(pkg)) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
.onLoad <- function(...) { | ||
# htmlwidgets provides methods for knitr::knit_print, but knitr isn't a Depends or | ||
# Imports of htmltools, only an Enhances. Therefore, the NAMESPACE file has to | ||
# declare it as an export, not an S3method. That means that R will only know to | ||
# use our methods if htmlwidgets is actually attached, i.e., you have to use | ||
# library(htmlwidgets) in a knitr document or else you'll get escaped HTML in your | ||
# document. This code snippet manually registers our method(s) with S3 once both | ||
# htmlwidgets and knitr are loaded. | ||
registerMethods(list( | ||
# c(package, genname, class) | ||
c("knitr", "knit_print", "htmlwidget") | ||
)) | ||
} | ||
|
||
#' @export | ||
knit_print.htmlwidget <- function(x, ..., options) { | ||
knitr::knit_print(toHTML(x, standalone = FALSE, knitrOptions = options), options = options, ...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
DEFAULT_WIDTH <- 450 | ||
DEFAULT_HEIGHT <- 350 | ||
DEFAULT_PADDING <- 15 | ||
|
||
#' Resolve widget sizing policy | ||
#' | ||
#' Take a widget object and sizing policy, and some other contextual details, | ||
#' and figure out what width/height to use, if possible. Some decisions may need | ||
#' to be deferred until runtime; include any metadata that's needed for that | ||
#' decision in the result as well. | ||
#' | ||
#' @param x The widget object whose size is to be determined. It may have $width | ||
#' and $height directly on it, which means we should obey those. | ||
#' @param sp The sizing policy to use. | ||
#' @param standalone Logical value indicating whether the widget is being | ||
#' rendered in a standalone context (where it's the only thing on the page; | ||
#' this is usually via `print.htmlwidget()`). | ||
#' @param knitrOptions Object representing the knitr options passed to us via | ||
#' `knit_print`. If we're not doing a `knit_print` right now, then the value | ||
#' should be `NULL`. | ||
#' @value A list that is guaranteed to have `width` and `height` values, each of | ||
#' which is either a number or CSS unit string. If `standalone=TRUE` then the | ||
#' list will also have a `runtime` value that is a list, that contains two | ||
#' nested lists `viewer` and `browser`. Each of those in turn has `width`, | ||
#' `height`, `padding` (between 1 and 4 numbers), and `fill` (`TRUE`/`FALSE`). | ||
#' @keywords internal | ||
resolveSizing <- function(x, sp, standalone, knitrOptions = NULL) { | ||
if (isTRUE(standalone)) { | ||
userSized <- !is.null(x$width) || !is.null(x$height) | ||
viewerScopes <- list(sp$viewer, sp) | ||
browserScopes <- list(sp$browser, sp) | ||
# Precompute the width, height, padding, and fill for each scenario. | ||
return(list( | ||
runtime = list( | ||
viewer = list( | ||
width = x$width %||% any_prop(viewerScopes, "defaultWidth") %||% DEFAULT_WIDTH, | ||
height = x$height %||% any_prop(viewerScopes, "defaultHeight") %||% DEFAULT_HEIGHT, | ||
padding = any_prop(viewerScopes, "padding") %||% DEFAULT_PADDING, | ||
fill = !userSized && any_prop(viewerScopes, "fill") %||% TRUE | ||
), | ||
browser = list( | ||
width = x$width %||% any_prop(browserScopes, "defaultWidth") %||% DEFAULT_WIDTH, | ||
height = x$height %||% any_prop(browserScopes, "defaultHeight") %||% DEFAULT_HEIGHT, | ||
padding = any_prop(browserScopes, "padding") %||% DEFAULT_PADDING, | ||
fill = !userSized && any_prop(browserScopes, "fill") %||% FALSE | ||
) | ||
), | ||
width = x$width %||% prop(sp, "defaultWidth"), | ||
height = x$height %||% prop(sp, "defaultHeight") | ||
)) | ||
} else if (!is.null(knitrOptions)) { | ||
knitrScopes <- list(sp$knitr, sp) | ||
isFigure <- any_prop(knitrScopes, "figure") | ||
figWidth <- if (isFigure) knitrOptions$out.width.px else NULL | ||
figHeight <- if (isFigure) knitrOptions$out.height.px else NULL | ||
# Compute the width and height | ||
return(list( | ||
width = x$width %||% figWidth %||% any_prop(knitrScopes, "defaultWidth"), | ||
height = x$height %||% figHeight %||% any_prop(knitrScopes, "defaultHeight") | ||
)) | ||
} else { | ||
# Some non-knitr, non-print scenario. | ||
# Just resolve the width/height vs. defaultWidth/defaultHeight | ||
return(list( | ||
width = x$width %||% prop(sp, "defaultWidth"), | ||
height = x$height %||% prop(sp, "defaultHeight") | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.