@@ -85,21 +85,39 @@ knit_print.figure <- function(x, options, ...) {
85
85
86
86
# ' Embed a plotly figure as an iframe into a IPython Notebook
87
87
# ' @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)
90
95
# ' @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 " )) {
93
98
if (system.file(package = " IRdisplay" ) == " " ) {
94
99
warning(" You need the IRdisplay package to use this function: \n " ,
95
100
" devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))" )
96
101
return (x )
97
102
}
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 )
99
115
get(" display_html" , envir = asNamespace(" IRdisplay" ))(iframe )
100
116
}
101
117
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
+ )
105
123
}
0 commit comments