@@ -96,7 +96,7 @@ plot_ly <- function(data = data.frame(), ..., type = "scatter",
9696# ' @author Carson Sievert
9797# ' @export
9898# '
99- add_trace <- function (p = get_plot (), ... ,
99+ add_trace <- function (p = last_plot (), ... ,
100100 group , color , colors , symbol , symbols , size ,
101101 data = NULL , evaluate = FALSE ) {
102102 # "native" plotly arguments
@@ -108,13 +108,13 @@ add_trace <- function(p = get_plot(), ...,
108108 if (! missing(symbol )) argz $ symbol <- substitute(symbol )
109109 if (! missing(symbols )) argz $ symbols <- substitute(symbols )
110110 if (! missing(size )) argz $ size <- substitute(size )
111- p <- get_plot( p )
111+ data <- data % || % if (is.data.frame( p )) p else list ( )
112112 tr <- list (
113113 args = argz ,
114- # if data is missing, adopt the most recent data environment
115- env = if (is.null(data )) p $ data [[length(p $ data )]]$ env else list2env(data ),
114+ env = list2env(data ),
116115 enclos = parent.frame()
117116 )
117+ p <- last_plot(p )
118118 p $ data <- c(p $ data , list (tr ))
119119 if (evaluate ) p <- plotly_build(p )
120120 hash_plot(data , p )
@@ -131,15 +131,15 @@ add_trace <- function(p = get_plot(), ...,
131131# ' @author Carson Sievert
132132# ' @export
133133# '
134- layout <- function (p = get_plot (), ... ,
134+ layout <- function (p = last_plot (), ... ,
135135 data = NULL , evaluate = FALSE ) {
136- p <- get_plot( p )
136+ data <- data % || % if (is.data.frame( p )) p else list ( )
137137 layout <- list (
138138 args = substitute(list (... )),
139- # if data is missing, adopt the most recent data environment
140- env = if (is.null(data )) p $ data [[length(p $ data )]]$ env else list2env(data ),
139+ env = list2env(data ),
141140 enclos = parent.frame()
142141 )
142+ p <- last_plot(p )
143143 p $ layout <- c(p $ layout , list (layout ))
144144 if (evaluate ) p <- plotly_build(p )
145145 hash_plot(data , p )
@@ -158,8 +158,7 @@ layout <- function(p = get_plot(), ...,
158158# ' @author Carson Sievert
159159# ' @export
160160# '
161- style <- function (p = get_plot(strict = FALSE ), ... , traces = 1 , evaluate = FALSE ) {
162- p <- get_plot(p )
161+ style <- function (p = last_plot(), ... , traces = 1 , evaluate = FALSE ) {
163162 idx <- traces > = length(p $ data )
164163 if (any(idx )) warning(" You've referenced non-existent traces" , call. = FALSE )
165164 style <- list (
@@ -184,14 +183,14 @@ style <- function(p = get_plot(strict = FALSE), ..., traces = 1, evaluate = FALS
184183# '
185184# ' @param l a ggplot object, or a plotly object, or a list.
186185# ' @export
187- plotly_build <- function (l = get_plot ()) {
186+ plotly_build <- function (l = last_plot ()) {
188187 # ggplot objects don't need any special type of handling
189- if (is.ggplot(l )) return (gg2list (l ))
188+ if (is.ggplot(l )) return (ggplotly (l ))
190189 l <- get_plot(l )
191190 # plots without NSE don't need it either
192191 nms <- lapply(l $ data , names )
193192 idx <- unique(unlist(lapply(l $ data , names ))) %in% c(" args" , " env" )
194- if (sum(idx ) != 2 ) return (struct (l , " plotly" ))
193+ if (sum(idx ) != 2 ) return (structure (l , class = unique( " plotly" , class( l )) ))
195194 nms <- names(l )
196195 # assume unnamed list elements are data/traces
197196 idx <- nms %in% " "
@@ -307,7 +306,7 @@ plotly_build <- function(l = get_plot()) {
307306 # traces shouldn't have any names
308307 x $ data <- setNames(x $ data , NULL )
309308 # add plotly class mainly for printing method
310- struct (x , " plotly" )
309+ structure (x , class = unique( " plotly" , class( x )) )
311310}
312311
313312# returns a _list of traces_.
0 commit comments