5
5
# '
6
6
# ' @description
7
7
# ' A call to \code{plotly(username, key)} creates an object of class
8
- # ' 'PlotlyClass', which has 3 methods:
8
+ # ' 'PlotlyClass', which has methods:
9
9
# ' \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()
12
12
# ' \item Styling Data: py$style(data1,data2,..., kwargs=kwargs)
13
13
# ' \item Styling Layout: py$layout(layout, kwargs=kwargs)
14
+ # ' \item Utilities: py$get_figure(file_owner, file_id)
14
15
# ' }
15
16
# '
16
17
# ' @import knitr
26
27
# ' @export
27
28
# ' @examples \dontrun{
28
29
# ' ## 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
32
33
# ' py <- plotly(username, api_key)
33
34
# ' ## 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)
36
37
# '
37
38
# ' ## Send data to Plotly. Plotly will render an interactive graph and will
38
39
# ' ## return a URL where you can view your plot
39
40
# ' ## 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
44
45
# '
45
- # ' ## Export ggplots directly to plot.ly.
46
+ # ' ## Export ggplots directly to plot.ly
46
47
# ' ggiris <- qplot(Petal.Width, Sepal.Length, data=iris, color=Species)
47
48
# ' py$ggplotly(ggiris)
48
49
# ' 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() +
52
53
# ' geom_point(aes(text=name, size=pop), colour="red",
53
54
# ' alpha=1/2, name="cities")
54
55
# ' py$ggplotly(viz)
@@ -72,13 +73,14 @@ For more help, see https://plot.ly/R or contact <
[email protected] >.")
72
73
}
73
74
74
75
# 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" )
76
78
priv <- list ()
77
79
78
80
pub $ makecall <- function (args , kwargs , origin ) {
79
- if (is.null(kwargs $ filename ))
81
+ if (is.null(kwargs $ filename ))
80
82
kwargs $ filename <- pub $ filename
81
- if (is.null(kwargs $ fileopt ))
83
+ if (is.null(kwargs $ fileopt ))
82
84
kwargs $ fileopt <- NULL
83
85
url <- " https://plot.ly/clientresp"
84
86
options(RCurlOptions = list (sslversion = 3 ,
@@ -89,13 +91,13 @@ For more help, see https://plot.ly/R or contact <
[email protected] >.")
89
91
key = pub $ key , origin = origin ,
90
92
kwargs = toJSON(kwargs , collapse = " " ))
91
93
resp <- fromJSON(respst , simplify = FALSE )
92
- if (! is.null(resp $ filename ))
94
+ if (! is.null(resp $ filename ))
93
95
pub $ filename <- resp $ filename
94
- if (! is.null(resp $ error ))
96
+ if (! is.null(resp $ error ))
95
97
cat(resp $ err )
96
- if (! is.null(resp $ warning ))
98
+ if (! is.null(resp $ warning ))
97
99
cat(resp $ warning )
98
- if (! is.null(resp $ message ))
100
+ if (! is.null(resp $ message ))
99
101
cat(resp $ message )
100
102
return (resp )
101
103
}
@@ -127,6 +129,37 @@ For more help, see https://plot.ly/R or contact <
[email protected] >.")
127
129
do.call(pub $ iplot , pargs )
128
130
}
129
131
}
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
+ }
130
163
pub $ iplot <- function (... , kwargs = list (filename = NULL , fileopt = NULL )) {
131
164
# Embed plotly graphs as iframes for knitr documents
132
165
r <- pub $ plotly(... , kwargs = kwargs )
@@ -156,4 +189,4 @@ For more help, see https://plot.ly/R or contact <
[email protected] >.")
156
189
pub <- list2env(pub )
157
190
class(pub ) <- " PlotlyClass"
158
191
return (pub )
159
- }
192
+ }
0 commit comments