Skip to content

Commit 6a300f8

Browse files
committed
add event_data() support for ggplot2 & update example; move source to plot level
1 parent 900f7c8 commit 6a300f8

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

R/ggplotly.R

+13-18
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44
#' \url{https://plot.ly/ggplot2}
55
#'
66
#' @param p a ggplot object.
7-
#' @param filename character string describing the name of the plot in your plotly account.
8-
#' Use / to specify directories. If a directory path does not exist it will be created.
9-
#' If this argument is not specified and the title of the plot exists,
10-
#' that will be used for the filename.
11-
#' @param fileopt character string describing whether to create a "new" plotly, "overwrite" an existing plotly,
12-
#' "append" data to existing plotly, or "extend" it.
13-
#' @param world_readable logical. If \code{TRUE}, the graph is viewable
14-
#' by anyone who has the link and in the owner's plotly account.
15-
#' If \code{FALSE}, graph is only viewable in the owner's plotly account.
7+
#' @param width Width of the plot in pixels (optional, defaults to automatic sizing).
8+
#' @param height Height of the plot in pixels (optional, defaults to automatic sizing).
9+
#' @param source Only relevant for \link{event_data}.
1610
#' @seealso \link{signup}, \link{plot_ly}
1711
#' @import httr jsonlite
1812
#' @export
@@ -32,13 +26,9 @@
3226
#' ggplotly(viz)
3327
#' }
3428
#'
35-
ggplotly <- function(p = ggplot2::last_plot(), filename, fileopt,
36-
world_readable = TRUE) {
37-
l <- gg2list(p)
38-
# tack on special keyword arguments
39-
if (!missing(filename)) l$filename <- filename
40-
if (!missing(fileopt)) l$fileopt <- fileopt
41-
l$world_readable <- world_readable
29+
ggplotly <- function(p = ggplot2::last_plot(), width = NULL, height = NULL,
30+
source = "A") {
31+
l <- gg2list(p, width = width, height = height, source = source)
4232
hash_plot(p$data, l)
4333
}
4434

@@ -116,9 +106,12 @@ guide_names <- function(p, aes = c("shape", "fill", "alpha", "area",
116106
#' Convert a ggplot to a list.
117107
#' @import ggplot2
118108
#' @param p ggplot2 plot.
109+
#' @param width Width of the plot in pixels (optional, defaults to automatic sizing).
110+
#' @param height Height of the plot in pixels (optional, defaults to automatic sizing).
111+
#' @param source Only relevant for \link{event_data}.
119112
#' @return figure object (list with names "data" and "layout").
120113
#' @export
121-
gg2list <- function(p) {
114+
gg2list <- function(p, width = NULL, height = NULL, source = "A") {
122115
# ggplot now applies geom_blank() (instead of erroring) when no layers exist
123116
if (length(p$layers) == 0) p <- p + geom_blank()
124117
layout <- list()
@@ -960,6 +953,8 @@ gg2list <- function(p) {
960953
}
961954

962955
l <- list(data = flipped.traces, layout = flipped.layout)
963-
956+
l$width <- width
957+
l$height <- width
958+
l$source <- source
964959
structure(add_boxed(rm_asis(l)), class = "plotly")
965960
}

R/plotly.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#' @param height Height in pixels (optional, defaults to automatic sizing).
2424
#' @param inherit logical. Should future traces inherit properties from this initial trace?
2525
#' @param evaluate logical. Evaluate arguments when this function is called?
26-
#' @param source Only relevant for \link{event_data}
26+
#' @param source Only relevant for \link{event_data}.
2727
#' @seealso \code{\link{layout}()}, \code{\link{add_trace}()}, \code{\link{style}()}
2828
#' @author Carson Sievert
2929
#' @export
@@ -90,16 +90,16 @@ plot_ly <- function(data = data.frame(), ..., type = "scatter",
9090
args = argz,
9191
env = list2env(data), # environment in which to evaluate arguments
9292
enclos = parent.frame(), # if objects aren't found in env, look here
93-
inherit = inherit,
94-
source = source
93+
inherit = inherit
9594
)
9695
# plotly objects should always have a _list_ of trace(s)
9796
p <- list(
9897
data = list(tr),
9998
layout = NULL,
10099
url = NULL,
101100
width = width,
102-
height = height
101+
height = height,
102+
source = source
103103
)
104104

105105
if (evaluate) p <- plotly_build(p)

inst/examples/plotlyEvents/app.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library(shiny)
22
library(plotly)
33

44
ui <- fluidPage(
5+
radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
56
plotlyOutput("plot"),
67
verbatimTextOutput("click"),
78
verbatimTextOutput("brush")
@@ -10,8 +11,13 @@ ui <- fluidPage(
1011
server <- function(input, output, session) {
1112

1213
output$plot <- renderPlotly({
13-
plot_ly(mtcars, x = mpg, y = wt, mode = "markers") %>%
14-
layout(dragmode = "select")
14+
if (identical(input$plotType, "ggplotly")) {
15+
p <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
16+
ggplotly(p) %>% layout(dragmode = "select")
17+
} else {
18+
plot_ly(mtcars, x = mpg, y = wt, mode = "markers") %>%
19+
layout(dragmode = "select")
20+
}
1521
})
1622

1723
output$click <- renderPrint({

inst/htmlwidgets/plotly.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ HTMLWidgets.widget({
5353
return obj;
5454
});
5555
Shiny.onInputChange(
56-
".clientValue-plotly_click-" + x.data[0].source,
56+
".clientValue-plotly_click-" + x.source,
5757
d
5858
);
5959
});
@@ -70,7 +70,7 @@ HTMLWidgets.widget({
7070
y: pts.map(function(pt) {return pt.y; })
7171
};
7272
Shiny.onInputChange(
73-
".clientValue-plotly_selected-" + x.data[0].source,
73+
".clientValue-plotly_selected-" + x.source,
7474
obj
7575
);
7676
}

man/gg2list.Rd

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ggplotly.Rd

+5-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_ly.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)