Skip to content

frameOrder warning when both frame and name are specified to add_trace #1919

@FrankBornais

Description

@FrankBornais

When a name is specified to a trace that also contains a frame argument the frameOrder element is not deleted within registerFrames which later triggers the warning that frameOrder is not a valid attribute of the object.

library(data.table)
library(plotly)

dt <- data.table(source = rep(c(rep("TEL", 2) , rep("WEB", 2), rep("OTH",2)),2), 
                 period = rep(c("AM", "PM"), 6), 
                 y_val = runif(12), 
                 year = c(rep(2020,6), rep(2021,6)))

# No warnings ----
p <- plot_ly()

for (yr in unique(dt$year)){
  which_lines <- which(dt$year==yr)
  p <- add_trace(p, x = dt$period[which_lines], y = dt$y_val[which_lines], frame = dt$source[which_lines], 
                 type = "scatter", mode = "lines+markers")
}

p


# Simply adding a name to the trace creates warnings ----
p1 <- plot_ly()

for (yr in unique(dt$year)){
  which_lines <- which(dt$year==yr)
  p1 <- add_trace(p1, x = dt$period[which_lines], y = dt$y_val[which_lines], frame = dt$source[which_lines],
                  type = "scatter", mode = "lines+markers", name = yr)
}

p1 
#> Warning messages:
#> 1: 'scatter' objects don't have these attributes: 'frameOrder'
#> Valid attributes include:
#>'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'xperiod', 'yperiod', 'xperiod0', 'yperiod0', 'xperiodalignment', 'yperiodalignment', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'texttemplate', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'texttemplatesrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
 
#> 2: 'scatter' objects don't have these attributes: 'frameOrder'
#> Valid attributes include:
#>'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'xperiod', 'yperiod', 'xperiod0', 'yperiod0', 'xperiodalignment', 'yperiodalignment', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'texttemplate', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'texttemplatesrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

This is the code within registerFrame

# remove frames from the trace names
  for (i in seq_along(p$x$data)) {
    tr <- p$x$data[[i]]
    if (length(tr[["name"]]) != 1)  next 
    nms <- strsplit(as.character(tr[["name"]]), br())[[1]]
    idx <- setdiff(seq_along(nms), tr$frameOrder %||% 0)
    p$x$data[[i]]$name <- if (length(idx)) paste(nms[idx], collapse = br()) else NULL
    p$x$data[[i]]$frameOrder <- NULL
  }

In the example above that triggers a warning length(tr[["name"]]) is equal to 2 and then it goes straight to the next iteration instead of removing frameOrder.

Replacing if (length(tr[["name"]]) != 1) next by the code below solved the issue for me:

    if (length(tr[["name"]]) != 1) {
      p$x$data[[i]]$frameOrder <- NULL
      next
    } 

I couldn't figure out wether the problem was that length(tr[["name"]]) should have been 1 instead of 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions