3
3
# ' Plotly interface object. See up-to-date documentation and examples at https://plot.ly/API
4
4
# '
5
5
# ' @description
6
- # ' A call to \code{plotly(username, key)} creates an object of class " PlotlyClass" , which
6
+ # ' A call to \code{plotly(username, key)} creates an object of class ' PlotlyClass' , which
7
7
# ' has 3 methods:
8
8
# ' \itemize{
9
9
# ' \item Plotting: py$plotly(x1, y1[,x2,y2,...], kwargs=kw) or
13
13
# ' }
14
14
# '
15
15
# ' @import knitr
16
+ # ' @import RJSONIO
16
17
# ' @param username plotly username
17
18
# ' @param key plotly API key
18
19
# '
25
26
# ' @examples \dontrun{
26
27
# ' ## View https://plot.ly/API for more examples
27
28
# ' ## Generate a simple plot
28
- # ' username <- " anna.lyst" # fill in with your plotly username
29
- # ' api_key <- " y37zkd" # fill in with your plotly API key
29
+ # ' username <- ' anna.lyst' # fill in with your plotly username
30
+ # ' api_key <- ' y37zkd' # fill in with your plotly API key
30
31
# ' py <- plotly(username, api_key)
31
32
# ' ## generate some data
32
33
# ' x <- c(0,1,2)
@@ -49,86 +50,74 @@ plotly <- function(username=NULL, key=NULL){
49
50
key <- getOption(" plotlyKey" , stop(" you need an API key for Plot.ly - See the signup function" ))
50
51
51
52
# public attributes/methods that the user has access to
52
- pub = list (
53
- username = username ,
54
- key = key ,
55
- filename = ' from api' ,
56
- fileopt = NULL ,
57
- version = as.character(packageVersion(" plotly" ))
58
- )
53
+ pub <- list (username = username , key = key , filename = " from api" , fileopt = NULL ,
54
+ version = " 0.3.1" )
55
+ priv <- list ()
59
56
60
- priv = list ()
61
-
62
- pub $ makecall = function (args ,kwargs ,origin ){
63
- if (is.null(kwargs $ filename )) kwargs $ filename = pub $ filename
64
- if (is.null(kwargs $ fileopt )) kwargs $ fileopt = NULL
65
- url = ' https://plot.ly/clientresp'
66
- options(RCurlOptions = list (sslversion = 3 , cainfo = system.file(" CurlSSL" , " cacert.pem" , package = " RCurl" )))
67
- respst = postForm( url ,
68
- platform = " R" ,
69
- version = pub $ version ,
70
- args = toJSON(args ,collapse = ' ' ),
71
- un = pub $ username ,
72
- key = pub $ key ,
73
- origin = origin ,
74
- kwargs = toJSON(kwargs ,collapse = ' ' ))
75
- resp = fromJSON(respst , simplify = FALSE )
76
- if (! is.null(resp $ filename )) pub $ filename = resp $ filename
77
- if (! is.null(resp $ error )) cat(resp $ err )
78
- if (! is.null(resp $ warning )) cat(resp $ warning )
79
- if (! is.null(resp $ message )) cat(resp $ message )
80
- return (resp )
81
- }
82
-
83
- priv $ plotly_hook = function (before , options , envir ){
84
- if (! before ){
85
- # set width and height from options or defaults
86
- if (is.null(options [[' width' ]])) w = ' 100%'
87
- else w = options [[' width' ]]
88
- if (is.null(options [[' height' ]])) h = ' 600'
89
- else h = options [[' height' ]]
90
- paste(' <iframe height="' ,h ,' " id="igraph" scrolling="no" seamless="seamless"
91
- src="' , options [[' url' ]],' " width="' ,w ,' "></iframe>' ,sep = ' ' )
92
- }
93
-
94
- }
95
-
96
- pub $ plotly = function (... , kwargs = list (filename = NULL ,fileopt = NULL )){
97
- args = list (... )
98
- return (pub $ makecall(args = args ,kwargs = kwargs ,origin = ' plot' ))
99
- }
100
-
101
- pub $ iplot = function (... , kwargs = list (filename = NULL , fileopt = NULL )){
102
- # Embed plotly graphs as iframes for knitr documents
103
- r = pub $ plotly(... , kwargs = kwargs )
104
- # bind url to the knitr options and pass into the plotly knitr hook
105
- knit_hooks $ set(plotly = function (before ,options ,envir ){
106
- options [[' url' ]] = r [[' url' ]]
107
- priv $ plotly_hook(before ,options ,envir )
108
- })
109
- }
110
-
111
- pub $ embed = function (url ){
112
- # knitr hook
113
- knit_hooks $ set(plotly = function (before ,options ,envir ){
114
- options [[' url' ]] = url
115
- priv $ plotly_hook(before ,options ,envir )
116
- })
117
- }
118
-
119
- pub $ layout = function (... , kwargs = list (filename = NULL ,fileopt = NULL )){
120
- args = list (... )
121
- return (pub $ makecall(args = args ,kwargs = kwargs ,origin = ' layout' ))
122
- }
123
-
124
- pub $ style = function (... , kwargs = list (filename = NULL ,fileopt = NULL )){
125
- args = list (... )
126
- cat(kwargs )
127
- return (pub $ makecall(args = args ,kwargs = kwargs ,origin = ' style' ))
128
- }
129
-
130
- # # wrap up the object
131
- pub <- list2env(pub )
132
- class(pub ) <- " PlotlyClass"
133
- return (pub )
134
- }
57
+ pub $ makecall <- function (args , kwargs , origin ) {
58
+ if (is.null(kwargs $ filename ))
59
+ kwargs $ filename <- pub $ filename
60
+ if (is.null(kwargs $ fileopt ))
61
+ kwargs $ fileopt <- NULL
62
+ url <- " https://plot.ly/clientresp"
63
+ options(RCurlOptions = list (sslversion = 3 , cainfo = system.file(" CurlSSL" ,
64
+ " cacert.pem" , package = " RCurl" )))
65
+ respst <- postForm(url , platform = " R" , version = pub $ version , args = toJSON(args ,
66
+ collapse = " " ), un = pub $ username , key = pub $ key , origin = origin , kwargs = toJSON(kwargs ,
67
+ collapse = " " ))
68
+ resp <- fromJSON(respst , simplify = FALSE )
69
+ if (! is.null(resp $ filename ))
70
+ pub $ filename <- resp $ filename
71
+ if (! is.null(resp $ error ))
72
+ cat(resp $ err )
73
+ if (! is.null(resp $ warning ))
74
+ cat(resp $ warning )
75
+ if (! is.null(resp $ message ))
76
+ cat(resp $ message )
77
+ return (resp )
78
+ }
79
+ priv $ plotly_hook <- function (before , options , envir ) {
80
+ if (! before ) {
81
+ # set width and height from options or defaults
82
+ if (is.null(options [[" width" ]]))
83
+ w <- " 100%" else w <- options [[" width" ]]
84
+ if (is.null(options [[" height" ]]))
85
+ h <- " 600" else h <- options [[" height" ]]
86
+ paste(" <iframe height=\" " , h , " \" id=\" igraph\" scrolling=\" no\" seamless=\" seamless\"\n\t\t\t\t src=\" " ,
87
+ options [[" url" ]], " \" width=\" " , w , " \" ></iframe>" , sep = " " )
88
+ }
89
+ }
90
+ pub $ plotly <- function (... , kwargs = list (filename = NULL , fileopt = NULL )) {
91
+ args <- list (... )
92
+ return (pub $ makecall(args = args , kwargs = kwargs , origin = " plot" ))
93
+ }
94
+ pub $ iplot <- function (... , kwargs = list (filename = NULL , fileopt = NULL )) {
95
+ # Embed plotly graphs as iframes for knitr documents
96
+ r <- pub $ plotly(... , kwargs = kwargs )
97
+ # bind url to the knitr options and pass into the plotly knitr hook
98
+ knit_hooks $ set(plotly = function (before , options , envir ) {
99
+ options [[" url" ]] <- r [[" url" ]]
100
+ priv $ plotly_hook(before , options , envir )
101
+ })
102
+ }
103
+ pub $ embed <- function (url ) {
104
+ # knitr hook
105
+ knit_hooks $ set(plotly = function (before , options , envir ) {
106
+ options [[" url" ]] <- url
107
+ priv $ plotly_hook(before , options , envir )
108
+ })
109
+ }
110
+ pub $ layout <- function (... , kwargs = list (filename = NULL , fileopt = NULL )) {
111
+ args <- list (... )
112
+ return (pub $ makecall(args = args , kwargs = kwargs , origin = " layout" ))
113
+ }
114
+ pub $ style <- function (... , kwargs = list (filename = NULL , fileopt = NULL )) {
115
+ args <- list (... )
116
+ cat(kwargs )
117
+ return (pub $ makecall(args = args , kwargs = kwargs , origin = " style" ))
118
+ }
119
+ # # wrap up the object
120
+ pub <- list2env(pub )
121
+ class(pub ) <- " PlotlyClass"
122
+ return (pub )
123
+ }
0 commit comments