Skip to content

Commit 1530880

Browse files
committed
Merge pull request #410 from ropensci/feature/sharing
Add sharing argument and deprecate world_readable. Fixes #332
2 parents c36cb1b + 311d68b commit 1530880

File tree

6 files changed

+78
-28
lines changed

6 files changed

+78
-28
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ env:
1414
global:
1515
- R_LIBS_USER=~/R/library NOT_CRAN=true
1616
# plotly_api_key (for posting to plot.ly)
17-
- secure: "UAETDsmeXkXm9eUpZa3JTm8/cv+jyLmArXE0TQD0yQNICFNPkGeNergoQmkesRqG+ifLu6V1lPXu1XlqQPdXY60clUHThDN+AjsBmcv9F170k6RtBSV8pIKL9fsW8t0v/maEet86qOSf3cNa5+gK8GjBH1pmLrN2rF9r7RhwUdg="
17+
- secure: "jlIAFoBGaDWUQDh6oGNb2BgU31/80iioM4y4xwx35yWkGKkNIID1eMEuiIn+/ipLvOVHkPTDS6DYnOtD8EIBpROgAKHFY33gM1h7P6NRxVszzC6WLP2ASnq95DWq36DAC2Oevy05V7nwSRFilPwSt6JLppYUInG+WdaZ+QOSRi4="
1818
# GITHUB_PAT (for pushing to plotly-test-table)
1919
- secure: "LHJONgWOo+98vNeFLI7LSJU3RtbMVszlI79GB8CcXmc2mlgM/UtZ5b6RnkNlhmg3Gj1/uObfm/rIybVTwuS1yNpeKv73+gsZOYhobVXiUGVxdRFG/mg5mbqwyWkkuofjPGFlMZCEMgHim37eZzgjSibwVH1LClRDsCoFMCgvgV0="
2020

@@ -24,7 +24,7 @@ cache:
2424

2525
before_script:
2626
- mkdir -p "$R_LIBS_USER"
27-
- echo "Sys.setenv('plotly_username' = 'RTestBot3000')" > ~/.Rprofile
27+
- echo "Sys.setenv('plotly_username' = 'cpsievert')" > ~/.Rprofile
2828
- git clone https://github.com/cpsievert/plotly-test-table.git ../plotly-test-table
2929
- "wget -q -O - https://github.com/yihui/crandalf/raw/master/inst/scripts/install-pandoc | bash"
3030
- Rscript -e 'if (length(find.package("devtools", quiet = TRUE)) == 0L) { install.packages("devtools", repos = "http://cran.rstudio.com") }'

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 2.2.4
3+
Version: 2.3.0
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.3.0 -- 19 Jan 2015
2+
3+
Add sharing argument and deprecate world_readable. Fixes #332
4+
15
2.2.4 -- 18 Jan 2015
26

37
Fix for error in embed_notebook(). See #409.

R/plotly_POST.R

Lines changed: 42 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 'hidden', anyone with this hidden 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", "hidden")) {
2840
x <- plotly_build(x)
2941
x$filename <- if (!missing(filename)) {
3042
filename
@@ -34,12 +46,19 @@ 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 is no longer supported. Instead, set the sharing\n',
56+
'argument to "private" (you must be logged in to access),\n',
57+
'"hidden" (anybody with the obscured URL can access) or "public"\n',
58+
'(anybody can view).')
59+
}
60+
x$world_readable <- if (sharing[1] == "public") TRUE else FALSE
61+
4362
# plotly server has trouble with empty properties
4463
x <- x[sapply(x, length) > 0]
4564
# construct body of message to plotly server
@@ -55,6 +74,14 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
5574
base_url <- file.path(get_domain(), "clientresp")
5675
resp <- httr::POST(base_url, body = bod)
5776
con <- process(struct(resp, "clientresp"))
77+
if (sharing[1] == "hidden") {
78+
bits <- strsplit(con$url, "/")[[1]]
79+
plot_id <- bits[length(bits)]
80+
url <- paste0(get_domain("v2"), "files/", verify("username"), ":", plot_id)
81+
bod <- list(share_key_enabled = TRUE)
82+
con2 <- httr::PATCH(url, plotly_headers("v2"), body = bod, encode = "json")
83+
con$url <- paste0(con$url, "?share_key=", content(con2)$share_key)
84+
}
5885
msg <- switch(x$fileopt %||% "new",
5986
new = "Success! Created a new plotly here -> ",
6087
overwrite = "Success! Modified your plotly here -> ")

man/plotly_POST.Rd

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

tests/testthat/test-plotly-getfigure.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ test_that("can add traces to a subplot figure", {
3737
expect_equivalent(length(plotly_build(fig)$data) + 1, length(l$data))
3838
})
3939

40+
test_that("posting a hidden plot returns a secret key", {
41+
skip_on_cran()
42+
res <- plotly_POST(plot_ly(), sharing = "hidden")
43+
key <- strsplit(res$url, "=")[[1]][2]
44+
expect_true(nchar(key) > 1)
45+
})
46+

0 commit comments

Comments
 (0)