Skip to content

Commit 90e3413

Browse files
committed
Add a class and print method for built objects
1 parent 190cfc9 commit 90e3413

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ S3method(geom2trace,GeomText)
1212
S3method(geom2trace,GeomTile)
1313
S3method(geom2trace,default)
1414
S3method(print,figure)
15+
S3method(print,plotly_built)
1516
S3method(print,plotly_hash)
1617
S3method(to_basic,GeomAbline)
1718
S3method(to_basic,GeomArea)
@@ -49,6 +50,7 @@ export(get_figure)
4950
export(gg2list)
5051
export(ggplotly)
5152
export(knit_print.figure)
53+
export(knit_print.plotly_built)
5254
export(knit_print.plotly_hash)
5355
export(last_plot)
5456
export(layout)

R/imports.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
#' @importFrom viridis viridis
88
#' @importFrom jsonlite toJSON fromJSON
99
#' @importFrom httr GET POST PATCH content config add_headers stop_for_status
10+
#' @importFrom htmlwidgets createWidget sizingPolicy
1011
NULL

R/plotly.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ plotly_build <- function(l = last_plot()) {
331331
x$layout$hovermode <- x$layout$hovermode %||% "closest"
332332
}
333333
# add plotly class mainly for printing method
334-
structure(x, class = unique("plotly", class(x)))
334+
structure(x, class = unique("plotly_built", class(x)))
335335
}
336336

337337
# returns a _list of traces_.

R/print.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ print.plotly_hash <- function(x, ...) {
1010
print(x, ...)
1111
}
1212

13+
#' Print a 'built' plotly object
14+
#'
15+
#' @param x a plotly object
16+
#' @param ... additional arguments
17+
#' @export
18+
print.plotly_built <- print.plotly_hash
19+
1320
#' Print a plotly object in a knitr doc
1421
#'
1522
#' @param x a plotly object
@@ -21,6 +28,13 @@ knit_print.plotly_hash <- function(x, options, ...) {
2128
knitr::knit_print(x, options, ...)
2229
}
2330

31+
#' Print a 'built' plotly object in a knitr doc
32+
#'
33+
#' @param x a plotly object
34+
#' @param ... additional arguments
35+
#' @export
36+
knit_print.plotly_built <- knit_print.plotly_hash
37+
2438
#' Convert a plotly object to an htmlwidget object
2539
#'
2640
#' @param x a plotly object.
@@ -34,22 +48,22 @@ knit_print.plotly_hash <- function(x, options, ...) {
3448

3549
as.widget <- function(x, ...) {
3650
if (inherits(x, "htmlwidget")) return(x)
37-
p <- plotly_build(x)
51+
if (!inherits(x, "plotly_built")) x <- plotly_build(x)
3852
# set some margin defaults if none are provided
39-
p$layout$margin <- modifyList(
53+
x$layout$margin <- modifyList(
4054
list(b = 40, l = 60, t = 25, r = 10),
41-
p$layout$margin %||% list()
55+
x$layout$margin %||% list()
4256
)
43-
p$config$modeBarButtonsToRemove <-
44-
i(p$config$modeBarButtonsToRemove %||% "sendDataToCloud")
45-
p$base_url <- get_domain()
57+
x$config$modeBarButtonsToRemove <-
58+
i(x$config$modeBarButtonsToRemove %||% "sendDataToCloud")
59+
x$base_url <- get_domain()
4660
# customize the JSON serializer (for htmlwidgets)
47-
attr(p, 'TOJSON_FUNC') <- to_JSON
61+
attr(x, 'TOJSON_FUNC') <- to_JSON
4862
htmlwidgets::createWidget(
4963
name = "plotly",
50-
x = p,
51-
width = p$width,
52-
height = p$height,
64+
x = x,
65+
width = x$width,
66+
height = x$height,
5367
sizingPolicy = htmlwidgets::sizingPolicy(
5468
padding = 5,
5569
browser.fill = TRUE

man/knit_print.plotly_built.Rd

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

man/print.plotly_built.Rd

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

0 commit comments

Comments
 (0)