How to disable multiple selections in checkboxGroupInput in shiny app - r

I have a checkboxGroupInput in my shiny app. It seems that the multiple selection is always enabled. What I need is to disable the multiple selection and only allow one selection at a time.
Does anyone know how to do that? Shiny app example below:
ui <- fluidPage(
checkboxGroupInput("icons", "Choose icons:",
choiceNames =
list(icon("calendar"), icon("bed"),
icon("cog"), icon("bug")),
choiceValues =
list("calendar", "bed", "cog", "bug")
),
textOutput("txt")
)
server <- function(input, output, session) {
output$txt <- renderText({
icons <- paste(input$icons, collapse = ", ")
paste("You chose", icons)
})
}
shinyApp(ui, server)
Here is the checkboxGroupInput function. I'm surprised there is no argument like multiple. Should I use other widget to serve that purpose? What should I use?
checkboxGroupInput(inputId, label, choices = NULL, selected = NULL,
inline = FALSE, width = NULL, choiceNames = NULL,
choiceValues = NULL)

Related

updateSelectInput fails to clear input object

In an R Shiny app, when trying to blank-out the choices and selection of a select object using the updateSelectInput() function, the input object does not reset. The input object retains the last choice selected.
The choice and selection are indeed removed from the select dropdown as I set them to character(0) (per references), but the input object resists reseting. Here is a very hackish solution I'm trying to avoid.
Is there a way to reset an input object to NULL, or character(0)? I know input is read only, but I'm wondering if I can reset it when the selectInput has been reset.
library(shiny)
cs <- c("A","B")
ui <- basicPage(
selectInput("options","Select", choices=NULL, selected = NULL),
actionButton("add","Add options"),
actionButton("clear","Clear options"),
verbatimTextOutput("text")
)
server <- function(input, output, session) {
observeEvent(input$clear, {
updateSelectInput(session,"options",
choices = character(0),
selected = character(0))
})
observeEvent(input$add, {
updateSelectInput(session,"options",
choices = c("A","B"),
selected = NULL)
})
output$text <- renderPrint({
str(input$options)
})
}
shinyApp(ui, server)
Setting the argument selectize to FALSE can produce a NULL.
ui <- basicPage(
selectInput("options",
"Select",
choices = NULL,
selected = NULL,
selectize = FALSE
),
actionButton("add","Add options"),
actionButton("clear","Clear options"),
verbatimTextOutput("text")
)
Its to do with internal implementation of the updateSelectInput as per Resetting selectInput to NULL in R Shiny, not ideal but try adding an extra space to "clear it"
observeEvent(input$clear, {
updateSelectInput(session,"options",choices = " ", selected = " ")
})

How does input from insertUI get stored?

I've created a SelectizeInput() UI using the insertUI() function. Essentially, I have an action button which adds a SelectizeInput() every time it's clicked. The idea is that the user selects columns from their data to put into groups. The creation of the UI works fine. I can also see in the Shiny trace that the selection works fine. However, I'm unsure how to access these variables to use in later plots. This is the code I have:
UI:
actionButton("cr_exp", "Create new biological group")
Server:
observeEvent(input$cr_exp, {
insertUI(
selector = "#cr_exp",
where = "afterEnd",
ui = selectizeInput(inputId = paste0("grp", input$cr_exp), label = "Select samples", choices = colnames(exp_dff()), options = list(create=TRUE), multiple=TRUE))
tags$div(id = paste0("grp", input$cr_exp))
})
In the shiny trace, it shows that the group is created, but I can't figure out how to access the value:
RECV {"method":"update","data":{"grp1":["MV4negControl01","MV4negControl02"]}}
You access the values just like any other input values: by the input element’s
id from the input reactive values:
library(shiny)
ui <- fluidPage(
actionButton("cr_exp", "Create new biological group"),
verbatimTextOutput("choices")
)
server <- function(input, output, session) {
observeEvent(input$cr_exp, {
insertUI(
selector = "#cr_exp",
where = "afterEnd",
ui = selectizeInput(
inputId = paste0("grp", input$cr_exp),
label = "Select samples",
choices = LETTERS,
options = list(create = TRUE),
multiple = TRUE
)
)
tags$div(id = paste0("grp", input$cr_exp))
})
output$choices <- renderPrint({
lapply(seq_len(input$cr_exp), function(i) input[[paste0("grp", i)]])
})
}
shinyApp(ui, server)

shinyWidgets updatePickerInput() server-side update (similar to updateSelectizeInput function)?

Does anyone know of a way to use shinyWidgets::pickerInput() with a large number of choices, but speed up the rendering? (similar to server-side selectize using updateSelectizeInput())
What I'm ultimately trying to do: have a shinyWidgets pickerInput() with a large number of choices (~1M).
The problem: it takes a really long time for the app to load with that many choices.
I know that one workaround is to use the shiny::selectizeInput() UI element, because there is a neat way to do a server-side shiny::updateSelectInput(), which makes both the loading of the initial UI element and the subsequent update with a bunch of options run pretty quickly (as described in this article).
However, I really like the formatting options and flexibility that shinyWidgets::pickerInput() offers.
Examples:
This works and loads quickly, but uses shiny::selectizeInput() instead of shinyWidgets::pickerInput(), which I want to use.
library(shiny)
ui = shiny::fluidPage(
shiny::uiOutput(outputId = "ui_output")
)
server = function(input, output) {
output$ui_output = shiny::renderUI({
shiny::selectizeInput(
inputId = "select",
label = "Select:",
choices = NULL
)
})
shiny::observe({
shiny::updateSelectizeInput(
inputId = "select",
choices = 1:1000000,
selected = 1,
server = TRUE
)
})
}
shiny::shinyApp(ui, server)
When trying to do the same with shinyWidgets::pickerInput(), I tried both loading the choices directly into the UI initially:
server = function(input, output) {
output$ui_output = shiny::renderUI({
shinyWidgets::pickerInput(
inputId = "select",
label = "Select:",
choices = 1:1000000
)
})
}
And also trying to same logic that I used in the first example, which is to initially load the UI object with choices = NULL, and then have a shiny::observe() call that updates the UI element's choices (this works, but is really slow):
server = function(input, output) {
output$ui_output = shiny::renderUI({
shinyWidgets::pickerInput(
inputId = "select",
label = "Select:",
choices = NULL
)
})
shiny::observe({
shinyWidgets::updatePickerInput(
session = getDefaultReactiveDomain(),
inputId = "select",
choices = 1:1000000,
selected = 1
)
})
}

selectizeInput paramter 'multiple' unchangeable when updated server-side

I have a selectizeInput (with parameter multiple = FALSE) in a shiny app. I´m not able to change the multiple-parameter afterwards by using the server-side updateSelectizeInput() and setting the option there.
Here is an example:
library(shiny)
ui <- fluidPage(
selectizeInput(
inputId = "name",
label = "Select Name:",
choices = NULL
)
)
server <- function(input, output, session) {
updateSelectizeInput(
inputId = "name",
choices = c("Markus", "Lisa", "Peter"),
options = list(maxItems = 10),
server = TRUE # set consciously, I have a big list to handle
)
}
shinyApp(ui, server)
If I don´t set the server parameter to TRUE, everything works just fine. Is this a bug or do I miss something?
To select multiple items, you can set multiple=TRUE in the selectizeInput as shown below.
library(shiny)
ui <- fluidPage(
selectizeInput(
inputId = "name",
label = "Select Name:",
choices = NULL, multiple=T
)
)
server <- function(input, output, session) {
updateSelectizeInput(
inputId = "name",
choices = c("Markus", "Lisa", "Peter"),
options = list(maxItems = 10),
server = TRUE # set consciously, I have a big list to handle
)
}
shinyApp(ui, server)

R shiny: Remove render UI created checkboxGroupInput upon deselection

I am trying to create a set of checkboxGroupInput choices that when deselected disappear. I am able to do this for selected choices of 2 or more. However, upon deselection of the final choice, it persists unselected rather than disappearing.
Please see the following, selecting options from the selectizeInput function.
In the server, it is important that the original UI selected and choices remain separate from the updateCheckboxGroupInput selected and choices functions as these are initially retrieved from persistent data storage that can then be manipulated.
library(shiny)
ui <- fluidPage(
column(width = 4, align = "left", uiOutput("choose_Number")),
br(),
column(width = 4, align = "left", div(
align = "left",
actionButton('add', 'Confirm Number(s)', style="color: #fff; background-color: #53C1BE"))
),
column(width = 4, uiOutput("my_checkboxGroupInput"))
)
server <- function(input, output, session) {
output$choose_Number <- renderUI({
selectizeInput("choose_Number", "Choose Number(s)", as.list(c(1:4)), options=list(create=TRUE,'plugins' = list('remove_button'),
persist = FALSE), multiple = TRUE)
})
lvl<-reactive(unlist(input$selected_Numbers))
observe({
if(input$add==0) return()
isolate({
current_selection<-paste(input$choose_Number,sep=", ")
updateCheckboxGroupInput(session, "selected_Numbers", choices = c(current_selection,lvl())
,selected=c(current_selection,lvl()))
})
})
observeEvent(input$selected_Numbers,({
updateCheckboxGroupInput(session, "selected_Numbers", choices = unique(as.list(lvl()))
,selected=c(lvl()))
})
)
observeEvent(input$add, {
updateSelectizeInput(session, "choose_Number", choices = as.list(1:4),
selected = character(0),
options = list(create=TRUE, 'plugins' = list('remove_button'), persist = FALSE))
})
output$my_checkboxGroupInput <- renderUI(
checkboxGroupInput("selected_Numbers", 'Chosen Number(s)',
choices = c(1,2,3),selected= c(1,2,3)))
}
shinyApp(ui, server)
Help appreciated in helping clear all unselected choices. Thank you
If your choices is a list it works as desired. Try this
observe({
updateCheckboxGroupInput(session, "selected_Numbers", choices = unique(as.list(lvl()))
,selected=c(lvl()))
})

Resources