plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

customdata array access issues

Open RightChain opened this issue 2 years ago • 1 comments

I am having issues accessing array elements inside of customdata using traditional JS indexing. The dot property accessor works, but using a key value or a numeric index inside of brackets doesn't seem to work. Here is my minimal reproducible example with only the version that works.

library(data.table)
library(plotly)

# Create sample data set of arbitrary length
n <- 1e3
dt <- data.table(
  x=seq_len(n),
  y=rnorm(n),
  z=1e5*rnorm(n),
  'text_label'=as.character(seq_len(n))
)

# Create customdata -- performance does not scale well with large n (1e6)
customCols <- c('z','text_label') # columns to include in customdata as they are not passed elsewhere
customdata <- split(dt[,.SD,.SDcols=customCols], seq_len(nrow(dt))) # fastest method of splitting data.frame

# Create and print plot
p <- plot_ly(data=dt) %>%
  add_trace(
    x=~x,
    y=~y,
    customdata=customdata,
    hovertemplate='<b>Test Label: </b>%{customdata.text_label}',
    type='scatter',
    mode='markers'
  )
print(p)

Hovertemplates that do not work

hovertemplate='<b>Test Label: </b>%{customdata[1]}',
hovertemplate='<b>Test Label: </b>%{customdata["text_label"]}',

RightChain avatar Dec 31 '22 12:12 RightChain

Hi, I just wanted to add that I'm also facing this issue. It's especially a problem when my customdata has element names with periods in them, for example:

df <- iris
df_list <- purrr::list_transpose(as.list(df))

plot_ly(
   data = df, 
   type = "scatter", 
   mode = "markers",
   x = ~Sepal.Length, 
   y = ~Sepal.Width, 
   customdata = df_list, 
   hovertemplate = "%{customdata.Petal.Length}<br>%{customdata.Petal.Width}"
)

cttnguyen avatar Jun 13 '23 16:06 cttnguyen