Skip to content

Commit c36cb1b

Browse files
committed
Merge pull request #409 from ropensci/fix/notebook
Fix embed_notebook. Fixes #398
2 parents 72b6636 + 1480719 commit c36cb1b

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
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.2.3
3+
Version: 2.2.4
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.2.4 -- 18 Jan 2015
2+
3+
Fix for error in embed_notebook(). See #409.
4+
15
2.2.3 -- 18 Jan 2015
26

37
Fix for geom_vline(). See #402.

R/print.R

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,39 @@ knit_print.figure <- function(x, options, ...) {
8585

8686
#' Embed a plotly figure as an iframe into a IPython Notebook
8787
#' @param x a plotly object
88-
#' @param width attribute of the iframe
89-
#' @param height attribute of the iframe
88+
#' @param width attribute of the iframe. If \code{NULL}, the width in
89+
#' \code{plot_ly} is used. If that is also \code{NULL}, '100\%' is the default.
90+
#' @param height attribute of the iframe. If \code{NULL}, the height in
91+
#' \code{plot_ly} is used. If that is also \code{NULL}, '400px' is the default.
92+
#' @param dir a directory for placing
93+
#' @param file a filename for saving the standalone HTML
94+
#' (only used if x is a non-figure object)
9095
#' @export
91-
embed_notebook <- function(x, width = "100%", height = "525") {
92-
if (!"figure" %in% class(x)) stop("x must be a plotly figure")
96+
embed_notebook <- function(x, width = NULL, height = NULL,
97+
file = paste0("plotlyJupyterHTML/", digest::digest(Sys.time()), ".html")) {
9398
if (system.file(package = "IRdisplay") == "") {
9499
warning("You need the IRdisplay package to use this function: \n",
95100
"devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))")
96101
return(x)
97102
}
98-
iframe <- plotly_iframe(x$url, width, height)
103+
l <- plotly_build(x)
104+
src <- if (is.null(l$url)) {
105+
dir <- dirname(file)
106+
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
107+
owd <- setwd(dir)
108+
on.exit(setwd(owd), add = TRUE)
109+
htmlwidgets::saveWidget(as.widget(l), file = basename(file))
110+
file
111+
} else {
112+
paste0(l$url, ".embed")
113+
}
114+
iframe <- plotly_iframe(src, width %||% l$width, height %||% l$height)
99115
get("display_html", envir = asNamespace("IRdisplay"))(iframe)
100116
}
101117

102-
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="")
118+
plotly_iframe <- function(url = "", width = NULL, height = NULL) {
119+
sprintf(
120+
'<iframe src="%s" width="%s" height="%s" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>',
121+
url, width %||% "100%", height %||% "400"
122+
)
105123
}

man/embed_notebook.Rd

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

0 commit comments

Comments
 (0)