diff --git a/DESCRIPTION b/DESCRIPTION index 9f6e082c09..4809a22080 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/NEWS b/NEWS index 4ec1f758a8..f368c774c6 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/R/print.R b/R/print.R index ab324d0e95..ff4c507d63 100644 --- a/R/print.R +++ b/R/print.R @@ -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("", sep="") +plotly_iframe <- function(url = "", width = NULL, height = NULL) { + sprintf( + '', + url, width %||% "100%", height %||% "400" + ) } diff --git a/man/embed_notebook.Rd b/man/embed_notebook.Rd index 2597ed3db9..a1072aa525 100644 --- a/man/embed_notebook.Rd +++ b/man/embed_notebook.Rd @@ -4,14 +4,22 @@ \alias{embed_notebook} \title{Embed a plotly figure as an iframe into a IPython Notebook} \usage{ -embed_notebook(x, width = "100\%", height = "525") +embed_notebook(x, width = NULL, height = NULL, + file = paste0("plotlyJupyterHTML/", digest::digest(Sys.time()), ".html")) } \arguments{ \item{x}{a plotly object} -\item{width}{attribute of the iframe} +\item{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.} -\item{height}{attribute of the iframe} +\item{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.} + +\item{file}{a filename for saving the standalone HTML +(only used if x is a non-figure object)} + +\item{dir}{a directory for placing} } \description{ Embed a plotly figure as an iframe into a IPython Notebook