Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for shortcut to combine selectize and textInput

Tags:

r

shiny

I'm trying to build a bit complex CRUD interface for databases (create, read, update and delete) (probably shiny is not the best tool for this, but I wanna give it a try since I'm not familiar with js). I've already found some nice examples, specially the one from Barbara. Now I'm looking for a shortcut to combine a selectizeInput and a textInput (maybe as an option of selectize?!), because otherwise my code gets so long if I have to do it like the following examples shows. The example shall demonstrate what I wanna get. (This is not what I'm looking for.)

Here's a very short reproducible example:

library(shiny)
startData <- c("Berlin", "London", "Paris")

ui <- fluidPage(
  selectizeInput("town", "Town", choices = c(startData, "new town")),
  uiOutput("newTown")
)

server <- function(input, output, session) {

  rV <- reactiveValues(towns = startData)

   output$newTown <- renderUI({
     if (input$town == "new town") {
       tagList(
         textInput("text", "New Town"),
         actionButton("entry", "save town")
       )
     }
   })
   # update selectizeInput when actionButton is clicked
   observeEvent(input$entry, {
     rV$towns <- c(rV$towns, input$text)
     updateSelectizeInput(
       session, "town", "Town",
       choices = c(rV$towns, "new town"),
       selected = input$text
     )
   })
}

# Run the application
shinyApp(ui = ui, server = server)

For a bit more complex example, I tried to implement a simple datatable to edit which you can find on my github account. Hopefully there's a shorter way doing so...

like image 457
frumentum Avatar asked Jan 29 '23 11:01

frumentum


1 Answers

Well, a selectize input is already a combination of textInput and selectInput. And as such, the desired functionality is already included in the selectize package. You can pass option arguments to the selectizeInput, which will be directly translated to the JavaScript package. See here for a list of options.

One of those is the create option that specifies if and when an option can be created by just typing something that does not match any given choice.

The whole server code in the following example can be omitted to show this, but I left some evaluations in there to clarify how to actually broaden the set of choices once created by the framework.

library(shiny)
startData <- c("Berlin", "London", "Paris")

ui <- fluidPage(
  selectizeInput("town", "Town", choices = startData, options=list(create=TRUE))
)

server <- function(input, output, session) {

  rV <- reactiveValues(towns = startData)

  observeEvent(input$town, {
    # nchar check, because emptying the text field results in "" choice.
    if (nchar(input$town) && !(input$town %in% rV$towns)) {
      rV$towns <- c(rV$towns, input$town)
    }

    print(rV$towns)
  })
}

shinyApp(ui = ui, server = server)
like image 121
K. Rohde Avatar answered Jan 31 '23 09:01

K. Rohde