Skip to content

Commit 48c0bd7

Browse files
committed
Reindent and fix merge conflicts
2 parents c341328 + 4d99abb commit 48c0bd7

File tree

2 files changed

+100
-22
lines changed

2 files changed

+100
-22
lines changed

R/plotly.R

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
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()
1415
#' }
15-
#'
16+
#'
1617
#' @import knitr
1718
#' @import RJSONIO
1819
#' @param username plotly username
@@ -26,29 +27,29 @@
2627
#' @export
2728
#' @examples \dontrun{
2829
#' ## View https://plot.ly/API for more examples
29-
#' ## Generate a simple plot
30+
#' ## Generate a simple plot
3031
#' username <- 'anna.lyst' # fill in with your plotly username
3132
#' 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
#'
4546
#' ## 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,38 @@ 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, sep="/"),
140+
httpheader=headers,
141+
writefunction=response_handler$update,
142+
headerfunction = header_handler$update)
143+
resp_header = as.list(parseHTTPHeader(header_handler$value()))
144+
145+
# Parse status
146+
if (resp_header$status != "200") {
147+
print(resp_header$statusMsg)
148+
stop(resp_header$status)
149+
}
150+
151+
body_string = response_handler$value()
152+
resp = RJSONIO::fromJSON(body_string)
153+
if (!is.null(resp$error) && resp$error != "")
154+
stop(resp$err)
155+
if (!is.null(resp$warning) && resp$error != "")
156+
cat(resp$warning)
157+
if (!is.null(resp$message) && resp$error != "")
158+
cat(resp$message)
159+
160+
fig = resp$payload$figure
161+
162+
return(fig)
163+
}
130164
pub$iplot <- function(..., kwargs = list(filename = NULL, fileopt = NULL)) {
131165
# Embed plotly graphs as iframes for knitr documents
132166
r <- pub$plotly(..., kwargs = kwargs)
@@ -156,4 +190,4 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
156190
pub <- list2env(pub)
157191
class(pub) <- "PlotlyClass"
158192
return(pub)
159-
}
193+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
42+
expect_equivalent(figure$data[[1]]$x, list("1", "2", "3"))
43+
44+
});

0 commit comments

Comments
 (0)