Skip to content

Conversation

@yihui
Copy link
Contributor

@yihui yihui commented Jun 19, 2014

A test case:

states = setNames(state.abb, state.name)
runApp(list(
  ui = fluidPage(
    selectInput('x1', 'X1', states),
    checkboxGroupInput('x2', 'X2', states, inline=TRUE),
    radioButtons('x3', 'X3', states),
    selectizeInput('x4', 'X4', list(
      Eastern = c(`New York` = 'NY', `New Jersey` = 'NJ'),
      Western = c(`California` = 'CA', `Washington` = 'WA')
    ), multiple = TRUE),
    verbatimTextOutput('y1'),
    verbatimTextOutput('y2'),
    verbatimTextOutput('y3'),
    verbatimTextOutput('y4')
  ),
  server = function(input, output, session) {
    updateSelectInput(session, 'x1', 'X One',
                      c(states, `District of Columbia` = 'DC'),
                      selected = 'IA')
    updateCheckboxGroupInput(session, 'x2', choices = states[1:10])
    updateRadioButtons(session, 'x3', choices=sample(states, 5), inline=TRUE)
    updateSelectizeInput(session, 'x4', choices=list(
      Eastern = c(`Rhode Island` = 'RI', `New Jersey` = 'NJ'),
      Western = c(`Oregon` = 'OR', `Washington` = 'WA'),
      Middle = c(Iowa = 'IA')
    ))
    output$y1 = renderPrint(input$x1)
    output$y2 = renderPrint(input$x2)
    output$y3 = renderPrint(input$x3)
    output$y4 = renderPrint(input$x4)
  }
))

@yihui yihui changed the title Add <optgroup> support to selectInput()/selectizeInput Add <optgroup> support to selectInput()/selectizeInput() Jun 20, 2014
@yihui yihui added this to the 0.10.1 milestone Jun 20, 2014
@yihui
Copy link
Contributor Author

yihui commented Jun 20, 2014

Okay, this PR should be complete now.

For those who want to use updateSelectizeInput() with <optgroup>, it may require substantial knowledge about selectize.js. I may be able to add an example to the article in shiny dev center when I have got time.

yihui added 10 commits July 11, 2014 14:03
…lar, even when `choices` is a list of lists (optgroup)
…oices

note the nested level of optgroup can be greater than one here, however, the HTML4 spec only allows one level, i.e. optgroup must have select as its direct parent http://stackoverflow.com/q/1037732/559676
…d radioButtons(), and use the new generator generateOptions() for them
…Buttons() work

now session$sendInputMessage() does not send options as the choices data, but as a pre-generated raw HTML string; in shiny.js, we just receive this string, and .append() it to the input after the input is emptied
…generate it no matter what the child element is

e.g. we use <optgroup> for A even if A only has one element: list(A = 'a', B = c('b1', 'b2'))
@yihui
Copy link
Contributor Author

yihui commented Jul 11, 2014

I have made two examples for this PR.

I do not have anything else to add to this PR except a news item. These two examples will be added to shiny dev center later.

@wch
Copy link
Collaborator

wch commented Jul 16, 2014

For the update*Input functions, did they stop working after the other changes? That is, why does the HTML need to be generated on the server side?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part looks too tricky to me - I'd really prefer a more straightforward way of accomplishing the same goal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more straightforward way is probably to copy and paste, which is less desirable.

@yihui
Copy link
Contributor Author

yihui commented Jul 16, 2014

I have tested the update*Input functions, and all of them are still working (the relevant commit was d2fc851). See the test cases in the description of this PR. The reason to send raw HTML was due to this optgroup feature: I thought it would be difficult to generate optgroups in shiny.js. Since we have R code that generates the HTML code, we can just use it instead of repeating this task in JavaScript. The downside is the size of data passed through WebSockets (the JSON object of choices should be much smaller than the pre-generated HTML string). I talked about this to @jcheng5 and he thought we could optimize this later if necessary.

@wch
Copy link
Collaborator

wch commented Jul 16, 2014

OK, that sounds like a good reason to generate the HTML on the server side.

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

Successfully merging this pull request may close these issues.

2 participants