Skip to content

Commit 350b582

Browse files
committed
better printing of figures
1 parent 0f81022 commit 350b582

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(print,figure)
34
S3method(print,plotly)
45
export("%>%")
56
export(add_trace)
@@ -10,6 +11,7 @@ export(gg2list)
1011
export(ggplot_build2)
1112
export(ggplotly)
1213
export(group2NA)
14+
export(knit_print.figure)
1315
export(knit_print.plotly)
1416
export(last_plot)
1517
export(layer2traces)

R/plotly.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,6 @@ plotly_build <- function(l = last_plot()) {
381381
kwargs <- Reduce(modifyList, c(kwargs, list(x[get_kwargs()])))
382382
# empty keyword arguments can cause problems
383383
kwargs <- kwargs[sapply(kwargs, length) > 0]
384-
# filename & fileopt are keyword arguments required by the API
385-
if (!is.null(x$url) || !is.null(kwargs$filename))
386-
kwargs$fileopt <- "overwrite"
387-
kwargs$fileopt <- kwargs$fileopt %||% "new"
388384
# try our damndest to assign a sensible filename
389385
if (is.null(kwargs$filename)) {
390386
kwargs$filename <-

R/plotly_POST.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
5252
new = "Success! Created a new plotly here -> ",
5353
overwrite = "Success! Modified your plotly here -> ")
5454
message(msg, con$url)
55-
con
55+
structure(con, class = "figure")
5656
}

R/print.R

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ toWidget <- function(x) {
4747
)
4848
}
4949

50-
#' Embed a plotly iframe into a IPython Notebook
50+
#' Print a plotly figure object
51+
#'
52+
#' @param x a plotly figure object
53+
#' @param ... additional arguments (currently ignored)
54+
#' @export
55+
print.figure <- function(x, ...) {
56+
browseURL(x$url)
57+
}
58+
59+
#' Embed a plotly figure as an iframe in a knitr doc
60+
#'
61+
#' @param x a plotly figure object
62+
#' @param options knitr options.
63+
#' @param ... placeholder.
64+
#' @export
65+
#' @references https://github.com/yihui/knitr/blob/master/vignettes/knit_print.Rmd
66+
knit_print.figure <- function(x, options, ...) {
67+
if (!requireNamespace("knitr")) {
68+
warning("Please install.packages('knitr')")
69+
return(x)
70+
}
71+
w <- if (is.null(options[["width"]])) "800" else options[["width"]]
72+
h <- if (is.null(options[["height"]])) "600" else options[["height"]]
73+
iframe <- plotly_iframe(x$url, w, h)
74+
knitr::asis_output(iframe)
75+
}
76+
77+
#' Embed a plotly figure as an iframe into a IPython Notebook
5178
#' @param x a plotly object
5279
#' @param width attribute of the iframe
5380
#' @param height attribute of the iframe
5481
#' @export
5582
embed_notebook <- function(x, width = "100%", height = "525") {
83+
if (!"figure" %in% class(x)) stop("x must be a plotly figure")
5684
if (system.file(package = "IRdisplay") == "") {
5785
warning("You need the IRdisplay package to use this function: \n",
5886
"devtools::install_github(c('IRkernel/repr', 'IRKernel/IRdisplay'))")
5987
return(x)
6088
}
61-
resp <- plotly_POST(x)
62-
iframe <- plotly_iframe(attr(resp, "url"), width, height)
89+
iframe <- plotly_iframe(x$url, width, height)
6390
get("display_html", envir = asNamespace("IRdisplay"))(iframe)
6491
}
6592

man/embed_notebook.Rd

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

man/knit_print.figure.Rd

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

man/print.figure.Rd

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

0 commit comments

Comments
 (0)