Skip to content

Commit f18dc13

Browse files
committed
Add sharing argument and deprecate world_readable. Fixes #332
1 parent c36cb1b commit f18dc13

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

R/plotly_POST.R

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44
#' \url{https://plot.ly/rest/}
55
#'
66
#' @param x either a ggplot object, a plotly object, or a list.
7-
#' @param filename character string describing the name of the plot in your plotly account.
8-
#' Use / to specify directories. If a directory path does not exist it will be created.
9-
#' If this argument is not specified and the title of the plot exists,
10-
#' that will be used for the filename.
11-
#' @param fileopt character string describing whether to create a "new" plotly, "overwrite" an existing plotly,
12-
#' "append" data to existing plotly, or "extend" it.
13-
#' @param world_readable logical. If \code{TRUE}, the graph is viewable
14-
#' by anyone who has the link and in the owner's plotly account.
15-
#' If \code{FALSE}, graph is only viewable in the owner's plotly account.
7+
#' @param filename character string describing the name of the plot in your
8+
#' plotly account. Use / to specify directories. If a directory path does not
9+
#' exist it will be created. If this argument is not specified and the title
10+
#' of the plot exists, that will be used for the filename.
11+
#' @param fileopt character string describing whether to create a "new" plotly,
12+
#' "overwrite" an existing plotly, "append" data to existing plotly,
13+
#' or "extend" it.
14+
#' @param sharing If 'public', anyone can view this graph. It will appear in
15+
#' your profile and can appear in search engines. You do not need to be
16+
#' logged in to Plotly to view this chart.
17+
#' If 'private', only you can view this plot. It will not appear in the
18+
#' Plotly feed, your profile, or search engines. You must be logged in to
19+
#' Plotly to view this graph. You can privately share this graph with other
20+
#' Plotly users in your online Plotly account and they will need to be logged
21+
#' in to view this plot.
22+
#' If 'secret', anyone with this secret link can view this chart. It will
23+
#' not appear in the Plotly feed, your profile, or search engines.
24+
#' If it is embedded inside a webpage or an IPython notebook, anybody who is
25+
#' viewing that page will be able to view the graph.
26+
#' You do not need to be logged in to view this plot.
1627
#' @export
1728
#' @seealso \link{plot_ly}, \link{signup}
1829
#' @return An R object created by mapping the JSON content of the plotly API
@@ -24,7 +35,8 @@
2435
#' plotly_POST(p, filename = "mtcars-bar-plot")
2536
#' }
2637

27-
plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
38+
plotly_POST <- function(x, filename, fileopt = "new",
39+
sharing = c("public", "private", "secret")) {
2840
x <- plotly_build(x)
2941
x$filename <- if (!missing(filename)) {
3042
filename
@@ -34,12 +46,17 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
3446
paste(c(x$layout$xaxis$title, x$layout$yaxis$title, x$layout$zaxis$title),
3547
collapse = " vs. ") %||% "plot from api"
3648
}
37-
if (!is.null(x$fileopt))
38-
warning("fileopt was specified in the wrong place. Please specify in plotly_POST()")
49+
if (!is.null(x$fileopt)) {
50+
warning("fileopt was specified in the wrong place.",
51+
"Please specify in plotly_POST()")
52+
}
3953
x$fileopt <- fileopt
40-
if (!is.null(x$world_readable))
41-
warning("world_readable was specified in the wrong place. Please specify in plotly_POST()")
42-
x$world_readable <- world_readable
54+
if (!is.null(x$world_readable)) {
55+
warning("world_readable was specified in the wrong place.",
56+
"Please use the sharing argument in plotly_POST()")
57+
}
58+
x$world_readable <- if (sharing[1] == "public") TRUE else FALSE
59+
4360
# plotly server has trouble with empty properties
4461
x <- x[sapply(x, length) > 0]
4562
# construct body of message to plotly server
@@ -55,6 +72,14 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
5572
base_url <- file.path(get_domain(), "clientresp")
5673
resp <- httr::POST(base_url, body = bod)
5774
con <- process(struct(resp, "clientresp"))
75+
if (sharing[1] == "secret") {
76+
bits <- strsplit(con$url, "/")[[1]]
77+
plot_id <- bits[length(bits)]
78+
url <- paste0(get_domain("v2"), "files/", verify("username"), ":", plot_id)
79+
bod <- list(share_key_enabled = TRUE)
80+
con2 <- httr::PATCH(url, plotly_headers("v2"), body = bod, encode = "json")
81+
con$url <- paste0(con$url, "?share_key=", content(con2)$share_key)
82+
}
5883
msg <- switch(x$fileopt %||% "new",
5984
new = "Success! Created a new plotly here -> ",
6085
overwrite = "Success! Modified your plotly here -> ")

0 commit comments

Comments
 (0)