@@ -118,22 +118,50 @@ verify_box <- function(proposed, schema) {
118
118
proposed
119
119
}
120
120
121
+
121
122
# make sure trace type is valid
122
123
# TODO: add an argument to verify trace properties are valid (https://github.com/ropensci/plotly/issues/540)
123
- verify_type <- function (type = NULL ) {
124
- if (is.null(type )) {
125
- message(" No trace type specified. Using the 'scatter' default." )
126
- type <- " scatter"
124
+ verify_type <- function (trace ) {
125
+ if (is.null(trace $ type )) {
126
+ attrs <- names(trace )
127
+ attrLengths <- lengths(trace )
128
+ if (all(c(" x" , " y" , " z" ) %in% attrs )) {
129
+ trace $ type <- if (all(c(" i" , " j" , " k" ) %in% attrs )) relay_type(" mesh3d" ) else relay_type(" scatter3d" )
130
+ } else if (all(c(" x" , " y" ) %in% attrs )) {
131
+ if (is.numeric(trace $ x ) && is.numeric(trace $ y )) {
132
+ trace $ type <- if (any(attrLengths ) > 15000 ) relay_type(" scattergl" ) else relay_type(" scatter" )
133
+ } else if (is.numeric(trace $ x )) {
134
+ trace $ type <- relay_type(" bar" )
135
+ trace $ orientation <- " h"
136
+ } else if (is.numeric(trace $ y )) {
137
+ trace $ type <- relay_type(" bar" )
138
+ } else {
139
+ trace $ type <- relay_type(" histogram2d" )
140
+ }
141
+ } else if (" y" %in% attrs || " x" %in% attrs ) {
142
+ trace $ type <- relay_type(" histogram" )
143
+ } else {
144
+ warning(" No trace type specified and no positional attributes specified" , call. = FALSE )
145
+ trace $ type <- relay_type(" scatter" )
146
+ }
127
147
}
128
- if (! is.character(type ) || length(type ) != 1 ) {
148
+ if (! is.character(trace $ type ) || length(trace $ type ) != 1 ) {
129
149
stop(" The trace type must be a character vector of length 1.\n " ,
130
150
call. = FALSE )
131
151
}
132
- if (! type %in% names(Schema $ traces )) {
152
+ if (! trace $ type %in% names(Schema $ traces )) {
133
153
stop(" Trace type must be one of the following: \n " ,
134
154
" '" , paste(names(Schema $ traces ), collapse = " ', '" ), " '" ,
135
155
call. = FALSE )
136
156
}
157
+ trace
158
+ }
159
+
160
+ relay_type <- function (type ) {
161
+ message(
162
+ " No trace type specified. Inferring a type of '" , type , " '.\n " ,
163
+ " Read more about this trace type here -> https://plot.ly/r/reference/#" , type
164
+ )
137
165
type
138
166
}
139
167
0 commit comments