Skip to content

Commit 6407c51

Browse files
committed
first attempt at #768
1 parent f56dff8 commit 6407c51

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method(arrange_,plotly)
44
S3method(distinct_,plotly)
55
S3method(do_,plotly)
6+
S3method(embed_notebook,plotly)
67
S3method(filter_,plotly)
78
S3method(fortify,SharedData)
89
S3method(geom2trace,GeomBar)

R/print.R

+17-16
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,32 @@ knit_print.plotly_figure <- function(x, options, ...) {
3131
#' \code{plot_ly} is used. If that is also \code{NULL}, '100\%' is the default.
3232
#' @param height attribute of the iframe. If \code{NULL}, the height in
3333
#' \code{plot_ly} is used. If that is also \code{NULL}, '400px' is the default.
34-
#' @param file a filename for saving the standalone HTML
35-
#' (only used if x is a non-figure object)
3634
#' @export
37-
embed_notebook <- function(x, width = NULL, height = NULL,
38-
file = paste0("plotlyJupyterHTML/", digest::digest(Sys.time()), ".html")) {
35+
embed_notebook <- function(x, width = NULL, height = NULL) {
3936
if (system.file(package = "IRdisplay") == "") {
4037
warning("You need the IRdisplay package to use this function: \n",
4138
"devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))")
4239
return(x)
4340
}
44-
l <- plotly_build(x)
45-
src <- if (is.null(l$url)) {
46-
dir <- dirname(file)
47-
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
48-
owd <- setwd(dir)
49-
on.exit(setwd(owd), add = TRUE)
50-
htmlwidgets::saveWidget(l, file = basename(file))
51-
file
52-
} else {
53-
l$url
41+
UseMethod("embed_notebook")
42+
}
43+
44+
#' @export
45+
embed_notebook.plotly <- function(x, width = NULL, height = NULL) {
46+
p <- plotly_build(x)
47+
p$width <- width %||% p$width
48+
p$height <- height %||% p$height
49+
html <- getFromNamespace("toHTML", asNamespace("htmlwidgets"))(p)
50+
# if we've already printed a plotly object, remove it's dependencies
51+
if (!is.null(last_plot())) {
52+
htmltools::htmlDependencies(html) <- NULL
5453
}
55-
iframe <- plotly_iframe(src, width %||% l$width, height %||% l$height, url_ext = "")
56-
get("display_html", envir = asNamespace("IRdisplay"))(iframe)
54+
get("display_html", envir = asNamespace("IRdisplay"))(as.character(html))
5755
}
5856

57+
# TODO: provide method for api_figure objects
58+
59+
5960
plotly_iframe <- function(url = "", width = NULL, height = NULL, url_ext = ".embed") {
6061
url <- paste0(url, url_ext)
6162
sprintf(

man/embed_notebook.Rd

+1-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)