R Shiny how to add 'select all' in checkbox within renderui - r

I am using renderui in R Shiny to generate a checkbox, I'm wondering how to add a 'select all' option, which can be used to check and uncheck all the boxed.
here is an example of the checkbox I want
This link shows how to add the 'select all' button, but is not for the checkbox inside renderui. Currently the method I used is
output$trmt_id <- renderUI({
if(is.null(ep_info())) {
return()
}
trmt_ids <- unique(ep_info()$TRMT_ID)
# add 'selected = ' to pre-select ctrl
checkboxGroupInput("input_trmt_id", "Choose Treatment ID (Include Control)", choices = c(trmt_ids, 'All'))})
observeEvent(input$input_trmt_id, {
if (input$input_trmt_id %in% 'All') {
updateCheckboxGroupInput(session, "input_trmt_id", "Choose Treatment ID (Include Control)", choices = c(unique(ep_info()$TRMT_ID), 'all'), selected = unique(ep_info()$TRMT_ID))
}})
But the problem of this piece of code is user cannot unselect all boxed just by clicking 'all' again.
Do you have some better ideas? Thanks in advance!

No checkboxes, but an easy alternative with pickerInput :
library("shiny")
library("shinyWidgets")
ui <- fluidPage(
column(
width = 4,
pickerInput(
inputId = "id", label = "Choices :",
choices = c("Banana", "Blueberry", "Cherry", "Coconut", "Grapefruit",
"Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya"),
options = list(`actions-box` = TRUE, `selected-text-format` = "count > 2",
`count-selected-text` = "{0}/{1} fruits"),
multiple = TRUE
),
verbatimTextOutput(outputId = "res")
)
)
server <- function(input, output) {
output$res <- renderPrint({
input$id
})
}
shinyApp(ui = ui, server = server)
Result looks like :

Related

Change color of slider using updateSliderTextInput

I am trying to change the color of the slide when updating its values. I have tried different ways without success. The following code does not run, but replicates what I am trying to do:
if (interactive()) {
library("shiny")
library("shinyWidgets")
ui <- fluidPage(
br(),
sliderTextInput(
inputId = "mySlider",
label = "Pick a month :",
choices = month.abb,
selected = "Jan"
),
verbatimTextOutput(outputId = "res"),
radioButtons(
inputId = "up",
label = "Update choices:",
choices = c("Abbreviations", "Full names")
)
)
server <- function(input, output, session) {
output$res <- renderPrint(str(input$mySlider))
observeEvent(input$up, {
choices <- switch(
input$up,
"Abbreviations" = month.abb,
"Full names" = month.name
)
updateSliderTextInput(
session = session,
inputId = "mySlider",
choices = choices,
color = "red" # This is the line I need to add
)
}, ignoreInit = TRUE)
}
shinyApp(ui = ui, server = server)
}
Maybe has someone the answer to this?
I was able to give this some more thought and figured out a way to update the slider color based on an input. shinyWidgets::setSliderColor essentially just injects CSS to overwrite all the classes associated with the sliderInputs. So it needs to be included in the UI instead of the server. (Took a min to realize that).
I set up a blank uiOutput which is then updated by observing input$up with the new or default color.
Demo
ui <- fluidPage(
br(),
mainPanel(class = "temp",
uiOutput('s_color'), # uiOuput
sliderTextInput(
inputId = "mySlider",
label = "Pick a month :",
choices = month.abb,
selected = "Jan"
),
verbatimTextOutput(outputId = "res"),
radioButtons(
inputId = "up",
label = "Update choices:",
choices = c("Abbreviations", "Full names")
)
)
)
server <- function(input, output, session) {
output$res <- renderPrint(str(input$mySlider))
# output$s_color = renderUI({})
observeEvent(input$up, {
choices <- switch(
input$up,
"Abbreviations" = month.abb,
"Full names" = month.name
)
updateSliderTextInput(
session = session,
inputId = "mySlider",
choices = choices
)
output$s_color = renderUI({ # add color
if (input$up == "Full names") {
setSliderColor(c("Red"), c(1))
} else {
setSliderColor(c("#428bca"), c(1))
}
})
}, ignoreInit = TRUE)
}
shinyApp(ui = ui, server = server)

How do I use input from `selectizeInput` to filter a list of options and then `updateSelectizeInput`?

BACKGROUND:
I have a large list of stock symbols, 27,000 rows, that I would like to be choices in a selectizeInput() on a shinyApp. Since the list is large I am using server = T in updateSelectizeInput().
AIM:
I would like the options list to not load/render until a user starts typing a string into selectizeInput(), so that I can return all symbols that start with that letter, to reduce loading all 27,000 rows in the input. I would like input$ticker to be what is observed and then what triggers the filtering code logic. How can i achieve this without using a specific button?
Shown below is
intended output, but with a button to produce the behavior instead of the user being in the text box. This is along the lines of what I would like, but does not automatically start searchign when I type in the box and has bad code smell to me.
current logic, using input$ticker in an observer to trigger selection of df and populate updateSelectize() with new choices, but is failing and the app is evaluating too soon?\
trying to load choices once, using upload button only doesn't work
REPREX:
1.
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
choice <- reactive(
tickers[startsWith(tickers$symbol, input$ticker), ]
)
observeEvent(input$update, {
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = choice(),
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
# REPREX for selectize, glitches and `input$ticker` observer causes loop gltich?
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
# updateSelectizeInput(
# session = session,
# label = "Ticker:",
# inputId ="ticker",
# choices = tickers,
# server = TRUE
# )
observeEvent(input$ticker, {
choices <- tickers[startsWith(tickers$symbol, input$ticker), ]
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = choices,
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
# REPREX for selectize
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
# One call to try and load ticker df
observeEvent(input$update, {
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = ticker,
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
SEE SIMILAR POSTS:
SO POST 1, SO POST 2, SO POST 3
What do you think about something like this?
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- fluidPage(
tags$head(
tags$script(
HTML(
'document.addEventListener("keydown", function(e) {
Shiny.setInputValue("key_pressed", e.key);
})'
)
)
),
fluidRow(
column(2, selectizeInput("select", "Select", choices = "")),
column(1, actionButton("btn", "Search"))
)
)
server <- function(input, output, session) {
observeEvent(input$btn, {
req(input$key_pressed)
updateSelectizeInput(session, "select", choices = tickers[startsWith(tickers, input$key_pressed)], server = TRUE)
})
}
shinyApp(ui, server)
Basically I think it is not possible to just use the words which are putted to the selectInput and we need separate input. I think that selectInput is truthy (isTruthy()) only after some option was chosen (and it can't be "" of course), so we can't use anything which is putted as a word to the selectInput box before some option is actually chosen. I'm not sure, but if I'm right, it is necessary to have separate input for what you want.
However, if we could assume that:
User will use only one letter to get the options to choose
Then we can use "keydown" event (keydown). Now the user doesn't need to put anything to the selectInput box, she/he can just use a key in the keyboards, like C (letter size does matter here, because we are using startsWith()) and then push "Search" button (but of course this letter can still be put to the selectInput box to mimic what you tried to achieve). We could even imagine solution without the button, but I'm afraid in most use-cases it will be not recommended, I mean if user can interact with the app using keyboard not only to choose the options, but also for other purposes, then we would recompute new options everytime user uses key in the keyboard for - well - nothing.
Turns out that selectizeInput doesn't accept a df and must be an atomic vector. When I used tickers[[1]], the issue seemed to be solved, and the list would no longer flash.

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()))
})

Show pickerInput depending on checkBoxInput result R shiny

I am trying to make a dynamic UI for my shiny dashboard. Here, I want to show a pickerInput field only when the input in a checkboxGroup is a specific value. For example, when the input from the checkboxGroup field is A, I want to show the pickerInput field, otherwise I want to show a different input field.
Currently, the part of my code looks, using conditionalPanel, like the following:
output$UI_selection <- renderUI({
tagList(
p(tags$i("Define the network")),
checkboxGroupInput(inputId = "choice1",
label = "Make a choice",
choices = list("A", "B")
),
conditionalPanel(condition = "input$choice1 == 'A'",
pickerInput(inputId = "select1",
label = "Select first:",
choices = list(
"Hierarchies" = grouplist_1),
selected = NULL,
options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
multiple = FALSE
)
)
)
})
However, this doesn't work and shows both the checkboxGroupInput as well as the PickerInput. Does anyone know how to fix this?
The shiny package functions (such as conditionalPanel) translate all of the R language code you supply into JS. Conditions you supply in conditionalPanel need to be interpretable in JS, which uses . in place of $.
You need to replace your condition = "input$choice1 == 'A'" with condition = "input.choice1 == 'A'".
Full working app is here:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
uiOutput("UI_selection")
)
server <- function(input, output, session) {
output$UI_selection <- renderUI({
tagList(
p(tags$i("Define the network")),
checkboxGroupInput(inputId = "choice1",
label = "Make a choice",
choices = list("A", "B")
),
conditionalPanel(condition = "input.choice1 == 'A'",
pickerInput(inputId = "select1",
label = "Select first:",
choices = list(
"Hierarchies" = c("X","Y","Z")),
selected = NULL,
options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
multiple = FALSE
)
)
)
})
}
shinyApp(ui, server)

Deselected All by updatePickerInput shiny R

I my shiny app I have few pickerInput elements. As a default nothing is selected.
pickerInput(
inputId = "pickerInput1",
label = NULL,
choices = c("Yes", "No"),
options = list(
`actions-box` = TRUE,
size = 12,
`selected-text-format` = "count > 3"
),
multiple = TRUE
)
The problem is that I have no idea how I can clear all of them (go to default value) after click on a special button. Unfortunately I propobly don't know how to use updatePickerInput. I tried:
observeEvent(input$Clear_FilterButton, {
updatePickerInput(session, "pickerInput1", selected = NULL)
})
but it doesn't work :( Any ideas what I am doing wrong?
If you are using the pickerInput from shinyWidgets, setting actions-box to TRUE should build Select All & Deselect All buttons by default. You don't need updatePickerInput. Click on your pickerInput to see these buttons.
Please refer to the documentation for additional details:
https://github.com/dreamRs/shinyWidgets
Update following up your comment:
Your comment made the question more clear. You can simply use selected = "" instead of selected = NULL. Here is a working example:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput(
inputId = "pickerInput1",
label = NULL,
choices = c("Yes", "No"),
options = list(
`actions-box` = TRUE,
size = 12
),
multiple = TRUE
),
actionButton(
inputId = "Clear_FilterButton",
label = "Clear"
)
)
server <- function(session, input, output) {
observeEvent(input$Clear_FilterButton, {
updatePickerInput(
session,
"pickerInput1",
selected = ""
)
})
}
shinyApp(ui = ui, server = server)

Resources