Skip to content

Fix embed_notebook. Fixes #398 #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: plotly
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
Version: 2.2.3
Version: 2.2.4
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
email = "cpsievert1@gmail.com"),
person("Chris", "Parmer", role = c("aut", "cph"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.2.4 -- 18 Jan 2015

Fix for error in embed_notebook(). See #409.

2.2.3 -- 18 Jan 2015

Fix for geom_vline(). See #402.
Expand Down
34 changes: 26 additions & 8 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,39 @@ knit_print.figure <- function(x, options, ...) {

#' Embed a plotly figure as an iframe into a IPython Notebook
#' @param x a plotly object
#' @param width attribute of the iframe
#' @param height attribute of the iframe
#' @param width attribute of the iframe. If \code{NULL}, the width in
#' \code{plot_ly} is used. If that is also \code{NULL}, '100\%' is the default.
#' @param height attribute of the iframe. If \code{NULL}, the height in
#' \code{plot_ly} is used. If that is also \code{NULL}, '400px' is the default.
#' @param dir a directory for placing
#' @param file a filename for saving the standalone HTML
#' (only used if x is a non-figure object)
#' @export
embed_notebook <- function(x, width = "100%", height = "525") {
if (!"figure" %in% class(x)) stop("x must be a plotly figure")
embed_notebook <- function(x, width = NULL, height = NULL,
file = paste0("plotlyJupyterHTML/", digest::digest(Sys.time()), ".html")) {
if (system.file(package = "IRdisplay") == "") {
warning("You need the IRdisplay package to use this function: \n",
"devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))")
return(x)
}
iframe <- plotly_iframe(x$url, width, height)
l <- plotly_build(x)
src <- if (is.null(l$url)) {
dir <- dirname(file)
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
owd <- setwd(dir)
on.exit(setwd(owd), add = TRUE)
htmlwidgets::saveWidget(as.widget(l), file = basename(file))
file
} else {
paste0(l$url, ".embed")
}
iframe <- plotly_iframe(src, width %||% l$width, height %||% l$height)
get("display_html", envir = asNamespace("IRdisplay"))(iframe)
}

plotly_iframe <- function(url, width, height) {
paste("<iframe height=\"", height, "\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"",
url, ".embed\" width=\"", width, "\" frameBorder=\"0\"></iframe>", sep="")
plotly_iframe <- function(url = "", width = NULL, height = NULL) {
sprintf(
'<iframe src="%s" width="%s" height="%s" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>',
url, width %||% "100%", height %||% "400"
)
}
14 changes: 11 additions & 3 deletions man/embed_notebook.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.