Home > Mobile >  Automating selectize_input programmatically
Automating selectize_input programmatically

Time:01-02

I am trying to reduce code duplication by writing functions to generate user interfaces.

However, it seems I am unable to create multiple selectizeInput elements with the arguments selected and selectize both set to FALSE. This is important because I need some elements initially set to NULL which can only be done this way.

Example that fails

library(shiny)
multi_selectize <- function(ids, titles, choices,size){
  Map(
    function(id, lab, choices,
             size) {
      selectizeInput(inputId = id,
                     label = lab,choices = choices,
                     selected = FALSE,selectize = FALSE,
                     size = size
      )
    },
    ids,
    titles,
    choices,
    size
  )
  
}
multi_selectize(c("test_0", "test_1"), titles = c("Test0","Test1"),
              choices = list(c("Lorem Ipsum", "Hello World"),
                             c("Ipsum Lorem", "World Hello")),
              size = c(1,2))

The above returns:

Error in selectInput(inputId, ..., selectize = FALSE, width = width) : formal argument "selectize" matched by multiple actual arguments

The following works but is not what I need:

multi_selectize_ <- function(ids, titles, choices,size){
  Map(
    function(id, lab, choices,
             size) {
      selectizeInput(inputId = id,
                     label = lab,
                     choices = choices,
                     size = size
      )
    },
    ids,
    titles,
    choices,
    size
  )
  
}

multi_selectize_(ids=c("test_0", "test_1"), 
                 titles = c("Test0","Test1"),
                choices = list(c("Lorem Ipsum", "Hello World"),
                               c("Ipsum Lorem", "World Hello")),
                size = c(1,2))


Result from above:

$test_0
<div >
  <label  id="test_0-label" for="test_0">Test0</label>
  <div>
    <select id="test_0"  size="1"><option value="Lorem Ipsum" selected>Lorem Ipsum</option>
<option value="Hello World">Hello World</option></select>
    <script type="application/json" data-for="test_0">{"plugins":["selectize-plugin-a11y"]}</script>
  </div>
</div>

$test_1
<div >
  <label  id="test_1-label" for="test_1">Test1</label>
  <div>
    <select id="test_1"  size="2"><option value="Ipsum Lorem" selected>Ipsum Lorem</option>
<option value="World Hello">World Hello</option></select>
    <script type="application/json" data-for="test_1">{"plugins":["selectize-plugin-a11y"]}</script>
  </div>
</div>


How can I get the first to work?

CodePudding user response:

The souce of selectizeInput is

function (inputId, ..., options = NULL, width = NULL) 
{
    selectizeIt(inputId, selectInput(inputId, ..., selectize = FALSE, 
        width = width), options)
}

So the elipsis is passed directly from selectizeInput to selectInput. But the call to selectInput also includes an explicit selectize argument. So when you write

selectizeInput(selectize = FALSE)

the call to selectizeIt becomes

selectizeIt(
  inputId, 
  selectInput(inputId, selectize = FALSE, selectize = FALSE), 
  width=width, 
  options
)

Hence the error.

Im still working on the module demo, but may now leave it until the morning.

CodePudding user response:

For now, I have switched to selectInput since this is not a performance stringent application. This seems to work as required.

multi_selectize <- function(ids, titles, choices,size){
  Map(
    function(id, lab, choices,
             size) 
      selectInput(inputId = id,
        label = lab,
        choices = choices,
        selected = FALSE,
        selectize = FALSE,
        size = size
      )
    ,
    ids,
    titles,
    choices,
    size
  )

}

Call:

multi_selectize(c("test_0", "test_1"), titles = c("Test0","Test1"),
              choices = list(c("Lorem Ipsum", "Hello World"),
                             c("Ipsum Lorem", "World Hello")),
              size = c(1,2))

Result

$test_0
<div >
  <label  id="test_0-label" for="test_0">Test0</label>
  <div>
    <select id="test_0"  size="1"><option value="Lorem Ipsum">Lorem Ipsum</option>
<option value="Hello World">Hello World</option></select>
  </div>
</div>

$test_1
<div >
  <label  id="test_1-label" for="test_1">Test1</label>
  <div>
    <select id="test_1"  size="2"><option value="Ipsum Lorem">Ipsum Lorem</option>
<option value="World Hello">World Hello</option></select>
  </div>
</div>


See @Limey's answer explaining why selectizeInput fails.

  • Related