Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: plotly
Title: Create interactive web-based graphs via plotly's API
Version: 1.0.1
Version: 1.0.2
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cph"),
email = "[email protected]"),
person("Scott", "Chamberlain", role = "aut",
Expand Down Expand Up @@ -38,6 +38,8 @@ Suggests:
knitr,
devtools,
shiny,
htmltools,
curl,
rmarkdown,
RColorBrewer
LazyData: true
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.0.2 -- 2 Aug 2015

* last_plot() will now look for the last plotly object; if not found, it will try to find the last ggplot object.
* Officially added the filename, fileopt, and world_readable arguments to plot_ly() and ggplotly().
* If plotly offline is not available, the shiny.launch.browser option is changed to open a web brower. See #245.
* Various namespace/documentation improvements for R CMD check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

1.0.1 -- 2 Aug 2015

Removed the stream() function as it wasn't ready to be included.
Expand Down
18 changes: 16 additions & 2 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
#' \url{https://plot.ly/ggplot2}
#'
#' @param p a ggplot object.
#' @param filename character string describing the name of the plot in your plotly account.
#' Use / to specify directories. If a directory path does not exist it will be created.
#' If this argument is not specified and the title of the plot exists,
#' that will be used for the filename.
#' @param fileopt character string describing whether to create a "new" plotly, "overwrite" an existing plotly,
#' "append" data to existing plotly, or "extend" it.
#' @param world_readable logical. If \code{TRUE}, the graph is viewable
#' by anyone who has the link and in the owner's plotly account.
#' If \code{FALSE}, graph is only viewable in the owner's plotly account.
#' @seealso \link{signup}, \link{plot_ly}
#' @import httr jsonlite
#' @export
Expand All @@ -23,8 +32,13 @@
#' ggplotly(viz)
#' }
#'
ggplotly <- function(p = ggplot2::last_plot()) {
ggplotly <- function(p = ggplot2::last_plot(), filename, fileopt,
world_readable = TRUE) {
l <- gg2list(p)
# tack on special keyword arguments
if (!missing(filename)) l$filename <- filename
if (!missing(fileopt)) l$fileopt <- fileopt
l$world_readable <- world_readable
hash_plot(p$data, l)
}

Expand Down Expand Up @@ -354,7 +368,7 @@ gg2list <- function(p) {
# x axis scale instead of on the grid 0-1 scale). This allows
# transformations to be used out of the box, with no additional d3
# coding.
theme.pars <- ggplot2:::plot_theme(p)
theme.pars <- getFromNamespace("plot_theme", "ggplot2")(p)

# Flip labels if coords are flipped - transform does not take care
# of this. Do this BEFORE checking if it is blank or not, so that
Expand Down
21 changes: 18 additions & 3 deletions R/plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
#' @param color Either a variable name or a vector to use for color mapping.
#' @param colors Either a colorbrewer2.org palette name (e.g. "YlOrRd" or "Blues"),
#' or a vector of colors to interpolate in hexadecimal "#RRGGBB" format,
#' or a color interpolation function like \link{grDevices::colorRamp}.
#' or a color interpolation function like \code{colorRamp()}.
#' @param symbol Either a variable name or a (discrete) vector to use for symbol encoding.
#' @param symbols A character vector of symbol types. Possible values:
#' 'dot', 'cross', 'diamond', 'square', 'triangle-down', 'triangle-left', 'triangle-right', 'triangle-up'
#' @param size A variable name or numeric vector to encode the size of markers.
#' @param inherit should future traces inherit properties from this initial trace?
#' @param filename character string describing the name of the plot in your plotly account.
#' Use / to specify directories. If a directory path does not exist it will be created.
#' If this argument is not specified and the title of the plot exists,
#' that will be used for the filename.
#' @param fileopt character string describing whether to create a "new" plotly, "overwrite" an existing plotly,
#' "append" data to existing plotly, or "extend" it.
#' @param world_readable logical. If \code{TRUE}, the graph is viewable
#' by anyone who has the link and in the owner's plotly account.
#' If \code{FALSE}, graph is only viewable in the owner's plotly account.
#' @param inherit logical. Should future traces inherit properties from this initial trace?
#' @param evaluate logical. Evaluate arguments when this function is called?
#' @seealso \code{\link{layout}()}, \code{\link{add_trace}()}, \code{\link{style}()}
#' @references \url{https://plot.ly/r/reference/}
Expand All @@ -45,6 +54,7 @@
#'
plot_ly <- function(data = data.frame(), ..., type = "scatter",
group, color, colors, symbol, symbols, size,
filename, fileopt, world_readable = TRUE,
inherit = TRUE, evaluate = FALSE) {
# "native" plotly arguments
argz <- substitute(list(...))
Expand All @@ -69,6 +79,11 @@ plot_ly <- function(data = data.frame(), ..., type = "scatter",
layout = NULL,
url = NULL
)
# tack on special keyword arguments
if (!missing(filename)) p$filename <- filename
if (!missing(fileopt)) p$fileopt <- fileopt
p$world_readable <- world_readable

if (evaluate) p <- plotly_build(p)
hash_plot(data, p)
}
Expand All @@ -83,7 +98,7 @@ plot_ly <- function(data = data.frame(), ..., type = "scatter",
#' @param color Either a variable name or a vector to use for color mapping.
#' @param colors Either a colorbrewer2.org palette name (e.g. "YlOrRd" or "Blues"),
#' or a vector of colors to interpolate in hexadecimal "#RRGGBB" format,
#' or a color interpolation function like \link{grDevices::colorRamp}.
#' or a color interpolation function like \code{colorRamp}.
#' @param symbol Either a variable name or a (discrete) vector to use for symbol encoding.
#' @param symbols A character vector of symbol types. Possible values:
#' 'dot', 'cross', 'diamond', 'square', 'triangle-down', 'triangle-left', 'triangle-right', 'triangle-up'
Expand Down
17 changes: 11 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ is.offline <- function(x) inherits(x, "offline")
# set a default for the offline bundle directory
if (Sys.getenv("plotly_offline") == "") {
Sys.setenv("plotly_offline" = "~/.plotly/plotlyjs")
# maybe rely a message if bundle is (or isn't) found?
# iframes won't work in RStudio viewer, so we override
# shiny's browser launch method
if (!has_offline())
options("shiny.launch.browser" = function(url) { browseURL(url) })
}
invisible(NULL)
}
Expand Down Expand Up @@ -61,13 +64,16 @@ get_plot <- function(data = NULL, last = FALSE) {
}
}

#' Retrive last plotly to be modified or created
#' Retrive and create the last plotly (or ggplot).
#'
#' @seealso \link{plotly_build}
#' @param data (optional) a data frame with a class of plotly (and a plotly_hash attribute).
#' @export
#'
last_plot <- function(...) {
p <- get_plot(..., last = TRUE)
last_plot <- function(data = NULL) {
p <- try(get_plot(data, last = TRUE), silent = TRUE)
if (inherits(p, "try-error")) p <- try(ggplotly(), silent = TRUE)
if (inherits(p, "try-error")) stop("The last plot doesn't exist")
structure(
p,
class = unique(c("plotly", class(p)))
Expand Down Expand Up @@ -143,8 +149,7 @@ get_domain <- function(type = "main") {

# plotly's special keyword arguments in POST body
get_kwargs <- function() {
c("filename", "fileopt", "style", "traces", "layout",
"world_readable", "kwarg_example")
c("filename", "fileopt", "style", "traces", "layout", "world_readable")
}

# POST header fields
Expand Down
2 changes: 1 addition & 1 deletion man/add_trace.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ a different trace will be created for each unique value.}

\item{colors}{Either a colorbrewer2.org palette name (e.g. "YlOrRd" or "Blues"),
or a vector of colors to interpolate in hexadecimal "#RRGGBB" format,
or a color interpolation function like \link{grDevices::colorRamp}.}
or a color interpolation function like \code{colorRamp}.}

\item{symbol}{Either a variable name or a (discrete) vector to use for symbol encoding.}

Expand Down
4 changes: 3 additions & 1 deletion man/ggplotly.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
\alias{ggplotly}
\title{Create plotly graphs using ggplot2 syntax}
\usage{
ggplotly(p = ggplot2::last_plot())
ggplotly(p = ggplot2::last_plot(), ...)
}
\arguments{
\item{p}{a ggplot object.}

\item{...}{additional arguments for changing the filename of the plot, etc.}
}
\description{
See up-to-date documentation and examples at
Expand Down
9 changes: 6 additions & 3 deletions man/last_plot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
% Please edit documentation in R/utils.R
\name{last_plot}
\alias{last_plot}
\title{Retrive last plotly to be modified or created}
\title{Retrive and create the last plotly (or ggplot).}
\usage{
last_plot(...)
last_plot(data = NULL)
}
\arguments{
\item{data}{(optional) a data frame with a class of plotly (and a plotly_hash attribute).}
}
\description{
Retrive last plotly to be modified or created
Retrive and create the last plotly (or ggplot).
}
\seealso{
\link{plotly_build}
Expand Down
2 changes: 1 addition & 1 deletion man/plot_ly.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ a different trace will be created for each unique value.}

\item{colors}{Either a colorbrewer2.org palette name (e.g. "YlOrRd" or "Blues"),
or a vector of colors to interpolate in hexadecimal "#RRGGBB" format,
or a color interpolation function like \link{grDevices::colorRamp}.}
or a color interpolation function like \code{colorRamp()}.}

\item{symbol}{Either a variable name or a (discrete) vector to use for symbol encoding.}

Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-plotly-knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ggplotly(p)
"
test_that("plotly embeds inside knitr", {
html <- knitr::knit2html(text = txt)
# why does this all of a sudden fail on Travis?
# https://travis-ci.org/ropensci/plotly/builds/73902815
expect_true(grepl("iframe", html))
})

Expand Down