Skip to content

Commit b49545b

Browse files
committed
merged karthiks edits with mine
2 parents 5d4ffbb + 703cd80 commit b49545b

File tree

7 files changed

+104
-111
lines changed

7 files changed

+104
-111
lines changed

.Rbuildignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.travis.yml
2-
kr_local.R
2+
Karthik_local.R
33
Makefile
44
man-roxygen
55
man-roxygen/^.*\.Rproj$

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
R/.Rhistory
2-
Rapp.history
2+
Rapp.history
3+
/Karthik_local.R

DESCRIPTION

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Package: plotly
22
Type: Package
33
Title: Interactive, publication-quality graphs online.
4-
Version: 0.3.4
4+
Version: 0.3.5
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",
8-
email = "[email protected]"))
8+
email = "[email protected]"),
9+
person("Karthik", "Ram", role = "aut",
10+
email = "[email protected]"))
911
License: MIT + file LICENSE
1012
Description: An interface to plotly's online graphing tools with desktop R
1113
environments. Send data to a plotly account and view the graphs in a web

R/plotly-package.r

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#' quality figures. This API allows R users to generate plot.ly graphs from their desktop
33
#' R environment.
44
#'
5-
#' An example of an interactive graph made from the R API: https://plot.ly/ ~chris/407/
5+
#' An example of an interactive graph made from the R API: https://plot.ly/~chris/407/
66
#'
77
#' \itemize{
88
#' \item Package: plotly
99
#' \item Type: Package
1010
#' \item Version: 0.3.4
11-
#' \item Date: 2014-03-06
11+
#' \item Date: 2014-03-07
1212
#' \item License: MIT
1313
#' }
1414
#'
@@ -29,4 +29,4 @@
2929
#' @docType package
3030
#' @title A R API for plot.ly
3131
#' @keywords package
32-
NULL
32+
NULL

R/plotly.R

+74-85
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' Plotly interface object. See up-to-date documentation and examples at https://plot.ly/API
44
#'
55
#' @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
77
#' has 3 methods:
88
#' \itemize{
99
#' \item Plotting: py$plotly(x1, y1[,x2,y2,...], kwargs=kw) or
@@ -13,6 +13,7 @@
1313
#' }
1414
#'
1515
#' @import knitr
16+
#' @import RJSONIO
1617
#' @param username plotly username
1718
#' @param key plotly API key
1819
#'
@@ -25,8 +26,8 @@
2526
#' @examples \dontrun{
2627
#' ## View https://plot.ly/API for more examples
2728
#' ## 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
3031
#' py <- plotly(username, api_key)
3132
#' ## generate some data
3233
#' x <- c(0,1,2)
@@ -49,86 +50,74 @@ plotly <- function(username=NULL, key=NULL){
4950
key <- getOption("plotlyKey", stop("you need an API key for Plot.ly - See the signup function"))
5051

5152
# 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()
5956

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\tsrc=\"",
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+
}

R/signup.R

+15-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#' response$api_key # key to access plotly with
2424
#' response$tmp_pw # temporary password to access your plotly account
2525
#' }
26-
2726
signup <- function(username=NULL, email=NULL){
2827
if(is.null(username))
2928
key <- getOption("plotlyUsername", stop("you need a user name for Plot.ly - See the signup function"))
@@ -32,17 +31,19 @@ signup <- function(username=NULL, email=NULL){
3231

3332
platform = 'R'
3433
version = as.character(packageVersion("plotly"))
35-
url = 'https://plot.ly/apimkacct'
36-
options(RCurlOptions = list(sslversion=3, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
37-
respst = postForm(url,
38-
platform=platform,
39-
version=version,
40-
email=email,
41-
un=username)
42-
resp <- fromJSON(respst, simplify=FALSE)
43-
if(!is.null(resp$filename)) pub$filename = resp$filename
44-
if(!is.null(resp$error)) cat(resp$err)
45-
if(!is.null(resp$warning)) cat(resp$warning)
46-
if(!is.null(resp$message)) cat(resp$message)
34+
url <- "https://plot.ly/apimkacct"
35+
options(RCurlOptions = list(sslversion = 3, cainfo = system.file("CurlSSL", "cacert.pem",
36+
package = "RCurl")))
37+
respst <- postForm(url, platform = platform, version = version, email = email,
38+
un = username)
39+
resp <- fromJSON(respst, simplify = FALSE)
40+
if (!is.null(resp$filename))
41+
pub$filename <- resp$filename
42+
if (!is.null(resp$error))
43+
cat(resp$err)
44+
if (!is.null(resp$warning))
45+
cat(resp$warning)
46+
if (!is.null(resp$message))
47+
cat(resp$message)
4748
return(resp)
48-
}
49+
}

man/plotly.Rd

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ after adding layers becomes a list class.
1515
}
1616
\description{
1717
A call to \code{plotly(username, key)} creates an object of
18-
class "PlotlyClass", which has 3 methods: \itemize{ \item
18+
class 'PlotlyClass', which has 3 methods: \itemize{ \item
1919
Plotting: py$plotly(x1, y1[,x2,y2,...], kwargs=kw) or
2020
py$plotly({data1[,data2,...]}, kwargs=kwargs) \item Styling
2121
Data: py$style(data1,data2,..., kwargs=kwargs) \item
@@ -31,8 +31,8 @@ See documentation and examples at https://plot.ly/API
3131
\dontrun{
3232
## View https://plot.ly/API for more examples
3333
## Generate a simple plot
34-
username <- "anna.lyst" # fill in with your plotly username
35-
api_key <- "y37zkd" # fill in with your plotly API key
34+
username <- 'anna.lyst' # fill in with your plotly username
35+
api_key <- 'y37zkd' # fill in with your plotly API key
3636
py <- plotly(username, api_key)
3737
## generate some data
3838
x <- c(0,1,2)
@@ -43,8 +43,8 @@ y <- c(10,11,12)
4343
## This call sends data to Plotly, Plotly renders an interactive
4444
## graph, and returns a URL where you can view your plot
4545
response <- py$plot(x,y)
46-
url = response$url # view your plot at this URL
47-
browseURL(url)
46+
response$url # view your plot at this URL
47+
browseURL(response$url) # use browseURL to go to the URL in your browser
4848
}
4949
}
5050
\author{

0 commit comments

Comments
 (0)