Skip to content

Commit

Permalink
export as.widget() for convenience sake. See #294
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Dec 23, 2015
1 parent 08bd614 commit 8193b54
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(print,figure)
S3method(print,plotly)
export("%>%")
export(add_trace)
export(as.widget)
export(config)
export(embed_notebook)
export(get_figure)
Expand Down
30 changes: 19 additions & 11 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
#' Print a plotly object
#'
#' @param x a plotly object
#' @param ... additional arguments (currently ignored)
#' @param ... additional arguments
#' @export
#' @importFrom htmlwidgets createWidget
#' @importFrom htmlwidgets sizingPolicy
print.plotly <- function(x, ...) {
w <- toWidget(x)
get("print.htmlwidget", envir = asNamespace("htmlwidgets"))(w)
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
get("print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, ...)
}

#' Print a plotly object in a knitr doc
#'
#' @param x a plotly object
#' @param options knitr options.
#' @param ... additional arguments (currently ignored)
#' @param ... additional arguments
#' @export
knit_print.plotly <- function(x, options, ...) {
w <- toWidget(x)
get("knit_print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, options = options)
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
get("knit_print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, options = options, ...)
}

#' Convert a plotly object to an htmlwidget object
#'
#' Users shouldn't need to use this function. It's exported for internal reasons.
#'
#' @param x a plotly object.
#' @param ... other options passed onto \code{htmlwidgets::createWidget}
#' @export
#' @examples \dontrun{
#' p <- plot_ly(mtcars, x = mpg, y = disp, mode = "markers")
#' htmlwidgets::saveWidget(as.widget(p), "index.html")
#' }
#'
toWidget <- function(x) {
as.widget <- function(x, ...) {
p <- plotly_build(x)
# set some margin defaults if none are provided
p$layout$margin <- modifyList(
Expand All @@ -41,13 +45,17 @@ toWidget <- function(x) {
x = p,
width = p$width,
height = p$height,
htmlwidgets::sizingPolicy(
sizingPolicy = htmlwidgets::sizingPolicy(
padding = 5,
browser.fill = TRUE
)
),
...
)
}

# for legacy reasons
toWidget <- as.widget

#' Print a plotly figure object
#'
#' @param x a plotly figure object
Expand Down
2 changes: 1 addition & 1 deletion R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ plotlyOutput <- function(outputId, width = "100%", height = "400px") {
renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
# https://github.com/ramnathv/htmlwidgets/issues/166#issuecomment-153000306
expr <- as.call(list(call(":::", quote("plotly"), quote("toWidget")), expr))
expr <- call("as.widget", expr)
shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
}
24 changes: 24 additions & 0 deletions man/as.widget.Rd

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

2 changes: 1 addition & 1 deletion man/knit_print.plotly.Rd

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

2 changes: 1 addition & 1 deletion man/print.plotly.Rd

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

15 changes: 0 additions & 15 deletions man/toWidget.Rd

This file was deleted.

0 comments on commit 8193b54

Please sign in to comment.