r plotly dropdown with variable number of entries

I have a plotly chart that has a dropdown menu. I have seen examples where the number of entries in the dropdown list is constant. How does one create a dropdown menu with a variable number of entries?

Two examples:

...  layout( title=grTitle, yaxis = yAxis, xaxis=xAxis,
          showlegend = FALSE, hoverlabel = "right", margin = list(l = 50, r = 50, t = 60, b = 100),  
                      annotations = list(text = outCap,
                      font = list(size = 12), showarrow = FALSE, xref = 'paper', x = 0, yref = 'paper', y = -0.4) ,
    updatemenus = list(
      list(
        type = 'dropdown',
        active = 0,
        buttons = list(
          list(method = "restyle",
               args = list("transforms[0].value", unique(f.ctyDISL_PLT$geoname)[1]),
               label = unique(f.ctyDISL_PLT$geoname)[1]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(f.ctyDISL_PLT$geoname)[2]),
               label = unique(f.ctyDISL_PLT$geoname)[2]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(f.ctyDISL_PLT$geoname)[3]),
               label = unique(f.ctyDISL_PLT$geoname)[3]))

Is there a way to set the value of the "updatemenus" to the results of a function?
Like this:

...layout( title=grTitle, yaxis = yAxis, xaxis=xAxis,
          showlegend = FALSE, hoverlabel = "right", margin = list(l = 50, r = 50, t = 60, b = 100),  
                      annotations = list(text = outCap,
                      font = list(size = 12), showarrow = FALSE, xref = 'paper', x = 0, yref = 'paper', y = -0.4), 
    updatemenus = genDropdown(...)

TIA
AB

I created a solution that someone might find useful.
The solution creates a list that replicates the structure of the “button” specification in the “updatemenus” list in the “layout” code for the chart:
This is an example of the static dropdown (with 4 entries in this case):

 ... updatemenus = list(
                         list(
                           type = 'dropdown',
                           active = 0,
                           buttons = list(
                             list(method = "restyle",
                                  args = list("transforms[0].value", unique(f.place$geoname)[1]),
                                  label = unique(f.place$geoname)[1]),
                             list(method = "restyle",
                                  args = list("transforms[0].value", unique(f.place$geoname)[2]),
                                  label = unique(f.place$geoname)[2]),
                                  args = list("transforms[0].value", unique(f.place$geoname)[3]),
                                  label = unique(f.place$geoname)[3]),
                             list(method = "restyle",
                                  args = list("transforms[0].value", unique(f.place$geoname)[4]),
                                  label = unique(f.place$geoname)[4])
))

My solution replicates the list structure of the “buttons” “NameList” is a unique list of labels, in my case taken from a data frame:

genDropdown <- function(NameList) {
    outlist <- list(list(
      method = "restyle", 
      args=list("transforms[0].value", NameList[1]),
      label = NameList[1]
    ))
      
    for(i in 2:length(NameList)) {
       item <- list(list(
        method = "restyle", 
        args=list("transforms[0].value", NameList[i]),
        label = NameList[i]
        ))
       outlist <- c(outlist,item)
    }
 
    return(outlist)
  }

and the new “updatemenus” specification is:

... updatemenus = list(
                      list(
                        type = 'dropdown',
                        active = 0,
                        buttons = genDropdown(txtNames)
                      ))

Works like a charm.
Cheers--
AB

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.