1
1
# ' Tools for working with plotly's REST API (v2)
2
- # '
2
+ # '
3
3
# ' Convenience functions for working with version 2 of plotly's REST API.
4
4
# ' Upload R objects to a plotly account via `api_create()` and download
5
5
# ' plotly objects via `api_download_plot()`/`api_download_grid()`.
6
6
# ' For anything else, use `api()`.
7
- # '
8
- # ' @param id a filename id.
7
+ # '
8
+ # ' @param id a filename id.
9
9
# ' @param username a plotly username.
10
- # '
11
- # ' @param x An R object to hosted on plotly's web platform.
10
+ # '
11
+ # ' @param x An R object to hosted on plotly's web platform.
12
12
# ' Can be a plotly/ggplot2 object or a \link{data.frame}.
13
13
# ' @param filename character vector naming file(s). If `x` is a plot,
14
14
# ' can be a vector of length 2 naming both the plot AND the underlying grid.
15
- # ' @param fileopt character string describing whether to "overwrite" existing
15
+ # ' @param fileopt character string describing whether to "overwrite" existing
16
16
# ' files or ensure "new" file(s) are always created.
17
- # ' @param sharing If 'public', anyone can view this graph. It will appear in
17
+ # ' @param sharing If 'public', anyone can view this graph. It will appear in
18
18
# ' your profile and can appear in search engines. You do not need to be
19
19
# ' logged in to Plotly to view this chart.
20
20
# ' If 'private', only you can view this plot. It will not appear in the
21
- # ' Plotly feed, your profile, or search engines. You must be logged in to
22
- # ' Plotly to view this graph. You can privately share this graph with other
23
- # ' Plotly users in your online Plotly account and they will need to be logged
21
+ # ' Plotly feed, your profile, or search engines. You must be logged in to
22
+ # ' Plotly to view this graph. You can privately share this graph with other
23
+ # ' Plotly users in your online Plotly account and they will need to be logged
24
24
# ' in to view this plot.
25
25
# ' If 'secret', anyone with this secret link can view this chart. It will
26
- # ' not appear in the Plotly feed, your profile, or search engines.
27
- # ' If it is embedded inside a webpage or an IPython notebook, anybody who is
28
- # ' viewing that page will be able to view the graph.
26
+ # ' not appear in the Plotly feed, your profile, or search engines.
27
+ # ' If it is embedded inside a webpage or an IPython notebook, anybody who is
28
+ # ' viewing that page will be able to view the graph.
29
29
# ' You do not need to be logged in to view this plot.
30
- # '
31
- # ' @param endpoint the endpoint (i.e., location) for the request.
30
+ # '
31
+ # ' @param endpoint the endpoint (i.e., location) for the request.
32
32
# ' To see a list of all available endpoints, call `api()`.
33
33
# ' Any relevant query parameters should be included here (see examples).
34
34
# ' @param verb name of the HTTP verb to use (as in, [httr::RETRY()]).
35
35
# ' @param body body of the HTTP request(as in, [httr::RETRY()]).
36
- # ' If this value is not already converted to JSON
36
+ # ' If this value is not already converted to JSON
37
37
# ' (via [jsonlite::toJSON()]), it uses the internal `to_JSON()`
38
38
# ' to ensure values are "automatically unboxed" (i.e., vec.
39
39
# '
40
- # ' @param ... For `api()`, these arguments are passed onto
40
+ # ' @param ... For `api()`, these arguments are passed onto
41
41
# ' [httr::RETRY()]. For `api_create()`, these arguments are
42
42
# ' included in the body of the HTTP request.
43
- # '
43
+ # '
44
44
# ' @export
45
45
# ' @rdname api
46
46
# ' @author Carson Sievert
47
47
# ' @references \url{https://api.plot.ly/v2}
48
48
# ' @seealso [signup()]
49
- # ' @examples
50
- # '
49
+ # ' @examples
50
+ # '
51
51
# ' \dontrun{
52
- # '
52
+ # '
53
53
# ' # ------------------------------------------------------------
54
54
# ' # api_create() makes it easy to upload ggplot2/plotly objects
55
55
# ' # and/or data frames to your plotly account
56
56
# ' # ------------------------------------------------------------
57
- # '
58
- # ' # A data frame creates a plotly "grid". Printing one will take you
57
+ # '
58
+ # ' # A data frame creates a plotly "grid". Printing one will take you
59
59
# ' # to the it's web address so you can start creating!
60
60
# ' (m <- api_create(mtcars))
61
- # '
61
+ # '
62
62
# ' # A plotly/ggplot2 object create a plotly "plot".
63
63
# ' p <- plot_ly(mtcars, x = ~factor(vs))
64
64
# ' (r <- api_create(p))
65
- # '
65
+ # '
66
66
# ' # api_create() returns metadata about the remote "file". Here is
67
67
# ' # one way you could use that metadata to download a plot for local use:
68
68
# ' fileID <- strsplit(r$file$fid, ":")[[1]]
72
72
# ' )
73
73
# '
74
74
# ' ------------------------------------------------------------
75
- # ' # The api() function provides a low-level interface for performing
75
+ # ' # The api() function provides a low-level interface for performing
76
76
# ' # any action at any endpoint! It always returns a list.
77
77
# ' # ------------------------------------------------------------
78
- # '
78
+ # '
79
79
# ' # list all the endpoints
80
80
# ' api()
81
- # '
81
+ # '
82
82
# ' # search the entire platform!
83
83
# ' # see https://api.plot.ly/v2/search
84
84
# ' api("search?q=overdose")
85
85
# ' api("search?q=plottype:pie trump fake")
86
- # '
86
+ # '
87
87
# ' # these examples will require a user account
88
88
# ' usr <- Sys.getenv("plotly_username", NA)
89
89
# ' if (!is.na(usr)) {
92
92
# ' # your folders/files https://api.plot.ly/v2/folders#user
93
93
# ' api(sprintf("folders/home?user=%s", usr))
94
94
# ' }
95
- # '
95
+ # '
96
96
# ' # Retrieve a specific file https://api.plot.ly/v2/files#retrieve
97
97
# ' api("files/cpsievert:14681")
98
- # '
98
+ # '
99
99
# ' # change the filename https://api.plot.ly/v2/files#update
100
100
# ' # (note: this won't work unless you have proper credentials to the relevant account)
101
- # ' api("files/cpsievert:14681", "PATCH", list(filename = "toy file"))
102
- # '
101
+ # ' api("files/cpsievert:14681", "PATCH", list(filename = "toy file"))
102
+ # '
103
103
# ' # Copy a file https://api.plot.ly/v2/files#lookup
104
104
# ' api("files/cpsievert:14681/copy", "POST")
105
- # '
105
+ # '
106
106
# ' # Create a folder https://api.plot.ly/v2/folders#create
107
107
# ' api("folders", "POST", list(path = "/starts/at/root/and/ends/here"))
108
- # '
108
+ # '
109
109
# ' }
110
- # '
110
+ # '
111
111
112
112
113
113
# ' @rdname api
114
114
# ' @export
115
- api_create <- function (x = last_plot(), filename = NULL ,
115
+ api_create <- function (x = last_plot(), filename = NULL ,
116
116
fileopt = c(" overwrite" , " new" ),
117
117
sharing = c(" public" , " private" , " secret" ), ... ) {
118
118
fileopt <- match.arg(fileopt , c(" overwrite" , " new" ))
@@ -140,7 +140,7 @@ api_create.data.frame <- api_create_grid
140
140
api_download_plot <- function (id , username ) {
141
141
f <- api_download_file(id , username )
142
142
api_expect_filetype(f , " plot" )
143
-
143
+
144
144
as_widget(
145
145
api_download_file(id , username , " plots" , " content?inline_data=true" )
146
146
)
@@ -152,7 +152,7 @@ api_download_plot <- function(id, username) {
152
152
api_download_grid <- function (id , username ) {
153
153
f <- api_download_file(id , username )
154
154
api_expect_filetype(f , " grid" )
155
-
155
+
156
156
prefix_class(
157
157
api_download_file(id , username , " grids" ), " api_grid_local"
158
158
)
@@ -174,20 +174,20 @@ api_download_file <- function(id, username, endpoint = "files", ...) {
174
174
# ' @export
175
175
api <- function (endpoint = " /" , verb = " GET" , body = NULL , ... ) {
176
176
api_check_endpoint(endpoint )
177
-
177
+
178
178
# construct the url
179
179
url <- httr :: modify_url(
180
- get_domain(" api" ),
180
+ get_domain(" api" ),
181
181
scheme = " https" ,
182
182
# TODO: should anything else in the endpoint (besides whitespace) be escaped?
183
183
path = file.path(" v2" , gsub(" \\ s+" , " +" , endpoint ))
184
184
)
185
-
185
+
186
186
# default to unboxing (i.e., no arrays of length 1)
187
187
if (! is.null(body ) && ! inherits(body , " json" )) {
188
188
body <- to_JSON(body )
189
189
}
190
-
190
+
191
191
resp <- httr :: RETRY(
192
192
verb = verb
193
193
, url = url
@@ -199,6 +199,6 @@ api <- function(endpoint = "/", verb = "GET", body = NULL, ...) {
199
199
, terminate_on_success = TRUE
200
200
, ...
201
201
)
202
-
202
+
203
203
structure(process(resp ), class = " api" )
204
204
}
0 commit comments