Skip to content

Commit eb4bb1f

Browse files
committed
get_figure
1 parent 55b8c85 commit eb4bb1f

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

R/plotly.R

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ plotly <- function(username=NULL, key=NULL){
6161
if(is.null(key))
6262
key <- getOption("plotlyKey", stop("you need an API key for Plot.ly - See the signup function"))
6363

64-
# public attributes/methods that the user has access to
65-
pub <- list(username = username, key = key, filename = "from api", fileopt = NULL,
66-
version = "0.3.1")
64+
# public attributes/methods that the user has access to
65+
pub <- list(username = username, key = key, filename = "from api", fileopt = NULL,
66+
version = "0.4.0")
6767
priv <- list()
6868

6969
pub$makecall <- function(args, kwargs, origin) {
@@ -114,6 +114,38 @@ plotly <- function(username=NULL, key=NULL){
114114
do.call(pub$iplot, pargs)
115115
}
116116
}
117+
pub$get_figure <- function(file_owner, file_id) {
118+
headers = c("plotly-username"=pub$username,
119+
"plotly-apikey"=pub$key,
120+
"plotly-version"=pub$version,
121+
"plotly-platform"="R")
122+
response_handler = basicTextGatherer()
123+
header_handler = basicTextGatherer()
124+
curlPerform(url=paste("https://plot.ly/apigetfile", file_owner, file_id, sep="/"),
125+
httpheader=headers,
126+
writefunction=response_handler$update,
127+
headerfunction = header_handler$update)
128+
resp_header = as.list(parseHTTPHeader(header_handler$value()))
129+
130+
# Parse status
131+
if (resp_header$status != "200") {
132+
print(resp_header$statusMsg)
133+
stop(resp_header$status)
134+
}
135+
136+
body_string = response_handler$value()
137+
resp = RJSONIO::fromJSON(body_string)
138+
if (!is.null(resp$error) && resp$error != "")
139+
stop(resp$err)
140+
if (!is.null(resp$warning) && resp$error != "")
141+
cat(resp$warning)
142+
if (!is.null(resp$message) && resp$error != "")
143+
cat(resp$message)
144+
145+
fig = resp$payload$figure
146+
147+
return(fig)
148+
}
117149
pub$iplot <- function(..., kwargs = list(filename = NULL, fileopt = NULL)) {
118150
# Embed plotly graphs as iframes for knitr documents
119151
r <- pub$plotly(..., kwargs = kwargs)

0 commit comments

Comments
 (0)