4
4
# ' \url{https://plot.ly/rest/}
5
5
# '
6
6
# ' @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.
16
27
# ' @export
17
28
# ' @seealso \link{plot_ly}, \link{signup}
18
29
# ' @return An R object created by mapping the JSON content of the plotly API
24
35
# ' plotly_POST(p, filename = "mtcars-bar-plot")
25
36
# ' }
26
37
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" )) {
28
40
x <- plotly_build(x )
29
41
x $ filename <- if (! missing(filename )) {
30
42
filename
@@ -34,12 +46,17 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
34
46
paste(c(x $ layout $ xaxis $ title , x $ layout $ yaxis $ title , x $ layout $ zaxis $ title ),
35
47
collapse = " vs. " ) %|| % " plot from api"
36
48
}
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
+ }
39
53
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
+
43
60
# plotly server has trouble with empty properties
44
61
x <- x [sapply(x , length ) > 0 ]
45
62
# construct body of message to plotly server
@@ -55,6 +72,14 @@ plotly_POST <- function(x, filename, fileopt = "new", world_readable = TRUE) {
55
72
base_url <- file.path(get_domain(), " clientresp" )
56
73
resp <- httr :: POST(base_url , body = bod )
57
74
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
+ }
58
83
msg <- switch (x $ fileopt %|| % " new" ,
59
84
new = " Success! Created a new plotly here -> " ,
60
85
overwrite = " Success! Modified your plotly here -> " )
0 commit comments