Skip to content

Commit 681509f

Browse files
committed
plotly_IMAGE() seems to work
1 parent e72a50f commit 681509f

File tree

6 files changed

+38
-19
lines changed

6 files changed

+38
-19
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ export(toRGB)
3232
import(ggplot2)
3333
import(httr)
3434
import(jsonlite)
35+
importFrom(base64enc,base64encode)
3536
importFrom(magrittr,"%>%")
3637
importFrom(viridis,viridis)

R/plotly_IMAGE.R

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,35 @@
88
#' @param height Image height in pixels
99
#' @param format The desired image format 'png', 'jpeg', 'svg', 'pdf', 'eps', or 'webp'
1010
#' @param scale Both png and jpeg formats will be scaled beyond the specified width and height by this number.
11-
#' @param encoded A boolean flag for base64 encoding of bytes
11+
#' @param out_file A filename for writing the image to a file.
12+
#' @param ... arguments passed onto \code{httr::POST}
1213
#' @export
1314
#' @examples
1415
#'
15-
#' plotly_IMAGE(plot_ly(x = 1:10))
16+
#' p <- plot_ly(x = 1:10)
1617
#'
18+
#' Png <- plotly_IMAGE(p, out_file = "plotly-test-image.png")
19+
#' Jpeg <- plotly_IMAGE(p, format = "jpeg", out_file = "plotly-test-image.jpeg")
20+
#' Svg <- plotly_IMAGE(p, format = "svg", out_file = "plotly-test-image.svg")
21+
#' Pdf <- plotly_IMAGE(p, format = "pdf", out_file = "plotly-test-image.pdf")
1722
#'
1823

1924
plotly_IMAGE <- function(x, width = 1000, height = 500, format = "png",
20-
scale = 4, encoded = FALSE) {
25+
scale = 4, out_file, ...) {
2126
x <- plotly_build(x)
2227

2328
bod <- list(
24-
figure = to_JSON(x[c("data", "layout")]),
29+
figure = x[c("data", "layout")],
2530
width = width,
2631
height = height,
2732
format = format,
2833
scale = scale,
29-
encoded = encoded
34+
encoded = FALSE
3035
)
31-
32-
base_url <- file.path(get_domain("v2"), "images")
33-
resp <- httr::POST(base_url, plotly_headers("v2"), body = bod)
36+
base_url <- paste0(get_domain("v2"), "images")
37+
resp <- httr::POST(base_url, plotly_headers("v2"), body = to_JSON(bod),
38+
if (!missing(out_file)) write_disk(out_file, overwrite = TRUE),
39+
...)
3440
con <- process(struct(resp, "image"))
35-
41+
invisible(con)
3642
}

R/plotly_POST.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ plotly_POST <- function(x) {
4141
base_url <- file.path(get_domain(), "clientresp")
4242
resp <- httr::POST(base_url, body = bod)
4343
con <- process(struct(resp, "clientresp"))
44-
msg <- switch(kwargs$fileopt,
44+
msg <- switch(x$fileopt %||% "new",
4545
new = "Success! Created a new plotly here -> ",
4646
overwrite = "Success! Modified your plotly here -> ")
4747
message(msg, con$url)

R/process.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ process.clientresp <- function(resp) {
1717

1818
process.image <- function(resp) {
1919
httr::stop_for_status(resp)
20-
browser()
21-
con <- from_JSON(httr::content(resp, as = "text"))
20+
# httr (should) know to call png::readPNG() which returns raster array
21+
tryCatch(httr::content(resp, as = "parsed"),
22+
error = function(e) httr::content(resp, as = "raw"))
2223
}
2324

2425
process.figure <- function(resp) {

R/utils.R

+7-4
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,20 @@ get_kwargs <- function() {
158158
plotly_headers <- function(type = "main") {
159159
usr <- verify("username")
160160
key <- verify("api_key")
161+
v <- as.character(packageVersion("plotly"))
161162
h <- if (type == "v2") {
163+
auth <- base64enc::base64encode(charToRaw(paste(usr, key, sep = ":")))
162164
c(
163-
"plotly-username" = base64enc::base64encode(charToRaw(usr)),
164-
"plotly-apikey" = base64enc::base64encode(charToRaw(key)),
165-
"Plotly-Client-Platform" = paste("R", as.character(packageVersion("plotly")))
165+
"authorization" = paste("Basic", auth),
166+
"plotly-client-platform" = paste("R", v),
167+
"plotly_version" = v,
168+
"content-type" = "application/json"
166169
)
167170
} else {
168171
c(
169172
"plotly-username" = usr,
170173
"plotly-apikey" = key,
171-
"plotly-version" = as.character(packageVersion("plotly")),
174+
"plotly-version" = v,
172175
"plotly-platform" = "R"
173176
)
174177
}

man/plotly_IMAGE.Rd

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
\title{Create/Modify plotly images}
66
\usage{
77
plotly_IMAGE(x, width = 1000, height = 500, format = "png", scale = 4,
8-
encoded = FALSE)
8+
out_file, ...)
99
}
1010
\arguments{
1111
\item{x}{either a plotly object or a list.}
@@ -18,13 +18,21 @@ plotly_IMAGE(x, width = 1000, height = 500, format = "png", scale = 4,
1818

1919
\item{scale}{Both png and jpeg formats will be scaled beyond the specified width and height by this number.}
2020

21-
\item{encoded}{A boolean flag for base64 encoding of bytes}
21+
\item{out_file}{A filename for writing the image to a file.}
22+
23+
\item{...}{arguments passed onto \code{httr::POST}}
2224
}
2325
\description{
2426
The images endpoint turn a plot (which may be given in multiple forms)
2527
into an image of the desired format.
2628
}
2729
\examples{
28-
plotly_IMAGE(qplot(1:10))
30+
p <- plot_ly(x = 1:10)
31+
32+
# returns raster array representing the image
33+
img <- plotly_IMAGE(p, out_file = "plotly-test-image.png", overwrite = TRUE)
34+
35+
img <- plotly_IMAGE(p, format = "pdf",
36+
out_file = "plotly-test-image.pdf")
2937
}
3038

0 commit comments

Comments
 (0)