Skip to content

Commit d16be22

Browse files
committed
Merge pull request #371 from ropensci/feature/asWidget
export as.widget() for convenience sake. See #294
2 parents 08bd614 + 6646fa0 commit d16be22

File tree

9 files changed

+52
-30
lines changed

9 files changed

+52
-30
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 2.0.18
3+
Version: 2.0.19
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ S3method(print,figure)
44
S3method(print,plotly)
55
export("%>%")
66
export(add_trace)
7+
export(as.widget)
78
export(config)
89
export(embed_notebook)
910
export(get_figure)

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.0.19 -- 23 Dec 2015
2+
3+
Added as.widget() function for conveniency in converting plotly object to htmlwidget objects. See #294.
4+
15
2.0.18 -- 22 Dec 2015
26

37
Fix #365

R/print.R

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
#' Print a plotly object
22
#'
33
#' @param x a plotly object
4-
#' @param ... additional arguments (currently ignored)
4+
#' @param ... additional arguments
55
#' @export
66
#' @importFrom htmlwidgets createWidget
77
#' @importFrom htmlwidgets sizingPolicy
88
print.plotly <- function(x, ...) {
9-
w <- toWidget(x)
10-
get("print.htmlwidget", envir = asNamespace("htmlwidgets"))(w)
9+
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
10+
get("print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, ...)
1111
}
1212

1313
#' Print a plotly object in a knitr doc
1414
#'
1515
#' @param x a plotly object
1616
#' @param options knitr options.
17-
#' @param ... additional arguments (currently ignored)
17+
#' @param ... additional arguments
1818
#' @export
1919
knit_print.plotly <- function(x, options, ...) {
20-
w <- toWidget(x)
21-
get("knit_print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, options = options)
20+
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
21+
get("knit_print.htmlwidget", envir = asNamespace("htmlwidgets"))(w, options = options, ...)
2222
}
2323

2424
#' Convert a plotly object to an htmlwidget object
2525
#'
26-
#' Users shouldn't need to use this function. It's exported for internal reasons.
27-
#'
2826
#' @param x a plotly object.
27+
#' @param ... other options passed onto \code{htmlwidgets::createWidget}
28+
#' @export
29+
#' @examples \dontrun{
30+
#' p <- plot_ly(mtcars, x = mpg, y = disp, mode = "markers")
31+
#' htmlwidgets::saveWidget(as.widget(p), "index.html")
32+
#' }
2933
#'
30-
toWidget <- function(x) {
34+
as.widget <- function(x, ...) {
3135
p <- plotly_build(x)
3236
# set some margin defaults if none are provided
3337
p$layout$margin <- modifyList(
@@ -41,13 +45,17 @@ toWidget <- function(x) {
4145
x = p,
4246
width = p$width,
4347
height = p$height,
44-
htmlwidgets::sizingPolicy(
48+
sizingPolicy = htmlwidgets::sizingPolicy(
4549
padding = 5,
4650
browser.fill = TRUE
47-
)
51+
),
52+
...
4853
)
4954
}
5055

56+
# for legacy reasons
57+
toWidget <- as.widget
58+
5159
#' Print a plotly figure object
5260
#'
5361
#' @param x a plotly figure object

R/shiny.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ plotlyOutput <- function(outputId, width = "100%", height = "400px") {
2626
renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
2727
if (!quoted) { expr <- substitute(expr) } # force quoted
2828
# https://github.com/ramnathv/htmlwidgets/issues/166#issuecomment-153000306
29-
expr <- as.call(list(call(":::", quote("plotly"), quote("toWidget")), expr))
29+
expr <- call("as.widget", expr)
3030
shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
3131
}

man/as.widget.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/knit_print.plotly.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/print.plotly.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/toWidget.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)