Skip to content

Commit 8ef0e0b

Browse files
committed
Merge pull request #41 from ropensci/chriddyp-GET-requests
Get requests
2 parents c341328 + 0b6a973 commit 8ef0e0b

File tree

2 files changed

+99
-24
lines changed

2 files changed

+99
-24
lines changed

R/plotly.R

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#'
66
#' @description
77
#' A call to \code{plotly(username, key)} creates an object of class
8-
#' 'PlotlyClass', which has 3 methods:
8+
#' 'PlotlyClass', which has methods:
99
#' \itemize{
10-
#' \item Plotting: py$plotly(x1, y1[,x2,y2,...], kwargs=kw) or
11-
#' py$plotly({data1[,data2,...]}, kwargs=kwargs)
10+
#' \item Plotting: py$plotly(x1, y1[, x2, y2, ...], kwargs=kwargs) or
11+
#' py$plotly({data1[, data2, ...]}, kwargs=kwargs), py$ggplotly()
1212
#' \item Styling Data: py$style(data1,data2,..., kwargs=kwargs)
1313
#' \item Styling Layout: py$layout(layout, kwargs=kwargs)
14+
#' \item Utilities: py$get_figure(file_owner, file_id)
1415
#' }
1516
#'
1617
#' @import knitr
@@ -26,29 +27,29 @@
2627
#' @export
2728
#' @examples \dontrun{
2829
#' ## View https://plot.ly/API for more examples
29-
#' ## Generate a simple plot
30-
#' username <- 'anna.lyst' # fill in with your plotly username
31-
#' api_key <- 'y37zkd' # fill in with your plotly API key
30+
#' ## Generate a simple plot
31+
#' username <- 'anna.lyst' # fill in with your plotly username
32+
#' api_key <- 'y37zkd' # fill in with your plotly API key
3233
#' py <- plotly(username, api_key)
3334
#' ## generate some data
34-
#' x <- c(0,1,2)
35-
#' y <- c(10,11,12)
35+
#' x <- c(0, 1, 2)
36+
#' y <- c(10, 11, 12)
3637
#'
3738
#' ## Send data to Plotly. Plotly will render an interactive graph and will
3839
#' ## return a URL where you can view your plot
3940
#' ## This call sends data to Plotly, Plotly renders an interactive
40-
#' ## graph, and returns a URL where you can view your plot
41-
#' response <- py$plot(x,y)
42-
#' response$url # view your plot at this URL
43-
#' browseURL(response$url) # use browseURL to go to the URL in your browser
41+
#' ## graph, and returns a URL where you can view your plot
42+
#' response <- py$plot(x, y)
43+
#' response$url # view your plot at this URL
44+
#' browseURL(response$url) # use browseURL to go to the URL in your browser
4445
#'
45-
#' ## Export ggplots directly to plot.ly.
46+
#' ## Export ggplots directly to plot.ly
4647
#' ggiris <- qplot(Petal.Width, Sepal.Length, data=iris, color=Species)
4748
#' py$ggplotly(ggiris)
4849
#' data(canada.cities, package="maps")
49-
#' viz <- ggplot(canada.cities, aes(long, lat))+
50-
#' borders(regions="canada", name="borders")+
51-
#' coord_equal()+
50+
#' viz <- ggplot(canada.cities, aes(long, lat)) +
51+
#' borders(regions="canada", name="borders") +
52+
#' coord_equal() +
5253
#' geom_point(aes(text=name, size=pop), colour="red",
5354
#' alpha=1/2, name="cities")
5455
#' py$ggplotly(viz)
@@ -72,13 +73,14 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
7273
}
7374

7475
# public attributes/methods that the user has access to
75-
pub <- list(username=username, key=key, filename="from api", fileopt=NULL)
76+
pub <- list(username=username, key=key, filename="from api", fileopt=NULL,
77+
version="0.4.0")
7678
priv <- list()
7779

7880
pub$makecall <- function(args, kwargs, origin) {
79-
if (is.null(kwargs$filename))
81+
if (is.null(kwargs$filename))
8082
kwargs$filename <- pub$filename
81-
if (is.null(kwargs$fileopt))
83+
if (is.null(kwargs$fileopt))
8284
kwargs$fileopt <- NULL
8385
url <- "https://plot.ly/clientresp"
8486
options(RCurlOptions=list(sslversion=3,
@@ -89,13 +91,13 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
8991
key=pub$key, origin=origin,
9092
kwargs=toJSON(kwargs, collapse=""))
9193
resp <- fromJSON(respst, simplify = FALSE)
92-
if (!is.null(resp$filename))
94+
if (!is.null(resp$filename))
9395
pub$filename <- resp$filename
94-
if (!is.null(resp$error))
96+
if (!is.null(resp$error))
9597
cat(resp$err)
96-
if (!is.null(resp$warning))
98+
if (!is.null(resp$warning))
9799
cat(resp$warning)
98-
if (!is.null(resp$message))
100+
if (!is.null(resp$message))
99101
cat(resp$message)
100102
return(resp)
101103
}
@@ -127,6 +129,37 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
127129
do.call(pub$iplot, pargs)
128130
}
129131
}
132+
pub$get_figure <- function(file_owner, file_id) {
133+
headers <- c("plotly-username"=pub$username,
134+
"plotly-apikey"=pub$key,
135+
"plotly-version"=pub$version,
136+
"plotly-platform"="R")
137+
response_handler <- basicTextGatherer()
138+
header_handler <- basicTextGatherer()
139+
curlPerform(url=paste("https://plot.ly/apigetfile", file_owner, file_id,
140+
sep="/"),
141+
httpheader=headers,
142+
writefunction=response_handler$update,
143+
headerfunction=header_handler$update)
144+
resp_header <- as.list(parseHTTPHeader(header_handler$value()))
145+
146+
# Parse status
147+
if (resp_header$status != "200") {
148+
cat(resp_header$statusMsg)
149+
stop(resp_header$status)
150+
}
151+
152+
body_string <- response_handler$value()
153+
resp <- RJSONIO::fromJSON(body_string)
154+
if (!is.null(resp$error) && resp$error != "")
155+
stop(resp$err)
156+
if (!is.null(resp$warning) && resp$error != "")
157+
cat(resp$warning)
158+
if (!is.null(resp$message) && resp$error != "")
159+
cat(resp$message)
160+
161+
resp$payload$figure
162+
}
130163
pub$iplot <- function(..., kwargs = list(filename = NULL, fileopt = NULL)) {
131164
# Embed plotly graphs as iframes for knitr documents
132165
r <- pub$plotly(..., kwargs = kwargs)
@@ -156,4 +189,4 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
156189
pub <- list2env(pub)
157190
class(pub) <- "PlotlyClass"
158191
return(pub)
159-
}
192+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
context("get_figure")
2+
3+
test_that("requests made by a user who doesn't exist error a 404", {
4+
py <- plotly("user_does_not_exist", "api_key_shouldnt_matter")
5+
expect_error({
6+
py$get_figure("get_test_user", 0)
7+
}, "404")
8+
})
9+
10+
test_that("requests made to retrieve a file that doesn't error return a 404", {
11+
py <- plotly("get_test_user", "vgs6e0cnoi")
12+
expect_error({
13+
py$get_figure("get_test_user", 1000)
14+
}, "404")
15+
})
16+
17+
test_that("requests made with the wrong API key error a 401", {
18+
py <- plotly("get_test_user", "some_invalid_api_key")
19+
expect_error({
20+
py$get_figure("get_test_user", 1)
21+
}, "401")
22+
})
23+
24+
test_that("requests made to retrieve some elses private file errors a 403", {
25+
py <- plotly("get_test_user_2", "0f9es4r6tm")
26+
expect_error({
27+
py$get_figure("get_test_user", 1)
28+
}, "403")
29+
})
30+
31+
test_that("requests made to retrieve some elses private file errors a 403", {
32+
py <- plotly("get_test_user_2", "0f9es4r6tm")
33+
expect_error({
34+
py$get_figure("get_test_user", 1)
35+
}, "403")
36+
})
37+
38+
test_that("retrieving a public figure ... works.", {
39+
py <- plotly("get_test_user_2", "0f9es4r6tm")
40+
figure <- py$get_figure("get_test_user", 0)
41+
expect_equivalent(figure$data[[1]]$x, list("1", "2", "3"))
42+
})

0 commit comments

Comments
 (0)