Skip to content

Commit 7d1b463

Browse files
committed
Fix embed_notebook. Fixes #398
1 parent 72b6636 commit 7d1b463

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

R/print.R

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,35 @@ knit_print.figure <- function(x, options, ...) {
8787
#' @param x a plotly object
8888
#' @param width attribute of the iframe
8989
#' @param height attribute of the iframe
90+
#' @param dir a directory for placing
91+
#' @param file a filename for saving the standalone HTML
92+
#' (only used if x is a non-figure object)
9093
#' @export
91-
embed_notebook <- function(x, width = "100%", height = "525") {
92-
if (!"figure" %in% class(x)) stop("x must be a plotly figure")
94+
embed_notebook <- function(x, width = "100%", height = "400",
95+
file = paste0("plotlyJupyterHTML/", digest::digest(Sys.time()), ".html")) {
9396
if (system.file(package = "IRdisplay") == "") {
9497
warning("You need the IRdisplay package to use this function: \n",
9598
"devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))")
9699
return(x)
97100
}
98-
iframe <- plotly_iframe(x$url, width, height)
101+
l <- plotly_build(x)
102+
src <- if (is.null(l$url)) {
103+
dir <- dirname(file)
104+
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
105+
owd <- setwd(dir)
106+
on.exit(setwd(owd), add = TRUE)
107+
htmlwidgets::saveWidget(as.widget(l), file = basename(file))
108+
file
109+
} else {
110+
paste0(l$url, ".embed")
111+
}
112+
iframe <- plotly_iframe(src, width, height)
99113
get("display_html", envir = asNamespace("IRdisplay"))(iframe)
100114
}
101115

102116
plotly_iframe <- function(url, width, height) {
103-
paste("<iframe height=\"", height, "\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"",
104-
url, ".embed\" width=\"", width, "\" frameBorder=\"0\"></iframe>", sep="")
117+
sprintf(
118+
'<iframe src="%s" width="%s" height="%s" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>',
119+
url, width, height
120+
)
105121
}

man/embed_notebook.Rd

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

0 commit comments

Comments
 (0)