Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom label and html display in server-side selectize elements #2172

Closed
akaever opened this issue Aug 29, 2018 · 1 comment
Closed

Custom label and html display in server-side selectize elements #2172

akaever opened this issue Aug 29, 2018 · 1 comment
Milestone

Comments

@akaever
Copy link

akaever commented Aug 29, 2018

In the current shiny CRAN version, it's possible to specify values, labels, and displayed html for server-side selectize elements separately. As described in

https://shiny.rstudio.com/articles/selectize.html

Looks like this option was dropped in the current github version, which would be a pity. Here's an example working with the CRAN version:

library(shiny)

runApp(list(
  ui = bootstrapPage(
    selectizeInput("sel1", label = "Selectize 1", choices = NULL)
  ),
  
  server = function(input, output, session) {
    valueStrs <- as.character(1:10)
    labelStrs <- paste("Label", valueStrs)
    htmlStrs <- paste0("<b>", valueStrs, "</b>")
    
    choiceFrame <- data.frame(value = valueStrs, label = labelStrs, 
                              html = htmlStrs, stringsAsFactors = FALSE)
    selected <- valueStrs[2]
    
    updateSelectizeInput(
      session, "sel1", server = TRUE, 
      choices = choiceFrame, 
      selected = selected, 
      
      options = list(render = I(
        '{ option: function(item, escape) {
            return "<div>Select " + item.html + "</div>";
            }
         }'
      ))
    )
    
    observe({
      print(input$sel1)
    })
    
  }
))

The dev version seems to interpret the choices as list and tries to group according to the column names (which is the documented behavior). One could specify some HTML as names(choices) and use a custom render function to display this (item.label). However, the escaped HTML is then also shown as label...

# test with github version
runApp(list(
  ui = bootstrapPage(
    selectizeInput("sel1", label = "Selectize 1", choices = NULL)
  ),
  
  server = function(input, output, session) {
    valueStrs <- as.character(1:10)
    names(valueStrs) <- paste0("<b>", valueStrs, "</b>")
    selected <- valueStrs[2]
    
    updateSelectizeInput(
      session, "sel1", server = TRUE, 
      choices = valueStrs, 
      selected = selected, 
      
      options = list(render = I(
        '{ option: function(item, escape) {
            return "<div>Select " + item.label + "</div>";
            }
         }'
      ))
    )
    
    observe({
      print(input$sel1)
    })
    
  }
))

Is there a way to get back to the old behavior or to specify more data (columns) which could be used in the render function to generate HTML?

Thanks

@jcheng5
Copy link
Member

jcheng5 commented Sep 25, 2018

Adding this to the 1.2 milestone as it looks like an unintended regression.

jcheng5 added a commit that referenced this issue Sep 25, 2018
Fixes #2192. Two problems here:

1. It's not documented but apparently we supported data frames for
   choices in updateSelectInput/updateSelectizeInput (it doesn't
   appear to work correctly for selectInput/selectizeInput though).
   This was used in 023-optgroup-server as well as by the user who
   reported #2172.
2. The example in 023-optgroup-server was also counting on the
   default value of optgroupLabelField, which (starting post-Shiny
   v1.1) was being set to a new default of "group". That now won't
   happen unless optgroupField is also blank. I'm less confident
   about the ramifications of this change. The selectize docs with
   the relevant bits are here:
  https://github.com/selectize/selectize.js/blob/master/docs/usage.md#data_searching
jcheng5 added a commit that referenced this issue Sep 25, 2018
Fixes #2192. Two problems here:

1. It's not documented but apparently we supported data frames for
   choices in updateSelectInput/updateSelectizeInput (it doesn't
   appear to work correctly for selectInput/selectizeInput though).
   This was used in 023-optgroup-server as well as by the user who
   reported #2172.
2. The example in 023-optgroup-server was also counting on the
   default value of optgroupLabelField, which (starting post-Shiny
   v1.1) was being set to a new default of "group". That now won't
   happen unless optgroupField is also blank. I'm less confident
   about the ramifications of this change. The selectize docs with
   the relevant bits are here:
  https://github.com/selectize/selectize.js/blob/master/docs/usage.md#data_searching
@jcheng5 jcheng5 closed this as completed Oct 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants