Display pickerInput() title when values are selected by default - r

I would like to know how I could set the title of a pickerInput() to be displayed -"Please select a month"- when values are selected by default.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput(
inputId = "month",
label = "Select a month",
choices = month.name,
selected = month.name,
multiple = TRUE,
options = pickerOptions(
actionsBox = TRUE,
title = "Please select a month",
header = "This is a title"
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

"Please select a month" will display in the picker itself but only at initial execution. Also at initial execution, all months are selected. When any event is observed for the picker, the picker itself will then show the selected values.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput(
inputId = "month",
label = "Select a month",
choices = month.name,
selected = month.name,
multiple = TRUE,
options = pickerOptions(
actionsBox = TRUE,
title = "Please select a month",
selectedTextFormat = 'static',
header = "This is a title"
)
)
)
server <- function(input, output, session) {
observeEvent(input$month, {
updatePickerInput(session = session, inputId = "month",options = pickerOptions(selectedTextFormat = 'values'))
}, ignoreInit = TRUE)
}
shinyApp(ui, server)

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)

Why selectizeInput does not select the first choices when server = FALSE?

The documentation of selectizeInput describes the argument selected as follows.
selected The initially selected value (or multiple values if multiple
= TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.
I interpreted this sentence as the selected argument would be the first element of the choices argument if nothing is specified for the selected. However, in the following example, after the selection of any numbers for the first selectizeInput, the second selectizeInput does not select the first choice. It still displays the default message as a placeholder.
Notice that if I set server = TRUE, the second selectizeInput would be able to display the first choice as the selected.
I am wondering why server = TRUE or server = FALSE affects if the selected can be the first element in choices. I also want to know how I can make the behavior of server = FALSE as the same as when server = TRUE.
library(shiny)
ui <- fluidPage(
headerPanel("shinyStore Example"),
sidebarLayout(
sidebarPanel = sidebarPanel(
selectizeInput(inputId = "Select1", label = "Select A Number",
choices = as.character(1:3),
options = list(
placeholder = 'Please select a number',
onInitialize = I('function() { this.setValue(""); }'),
create = TRUE
))
),
mainPanel = mainPanel(
fluidRow(
selectizeInput(inputId = "Select2",
label = "Select A Letter",
choices = character(0),
options = list(
placeholder = 'Please select a number in the sidebar first',
onInitialize = I('function() { this.setValue(""); }'),
create = TRUE
))
)
)
)
)
server <- function(input, output, session) {
dat <- data.frame(
Number = as.character(rep(1:3, each = 3)),
Letter = letters[1:9]
)
observeEvent(input$Select1, {
updateSelectizeInput(session, inputId = "Select2",
choices = dat$Letter[dat$Number %in% input$Select1],
server = FALSE)
})
}
shinyApp(ui, server)
Why not create the second selectizeInput completely on the server side?
library(shiny)
ui <- fluidPage(
headerPanel("shinyStore Example"),
sidebarLayout(
sidebarPanel = sidebarPanel(
selectizeInput(inputId = "Select1", label = "Select A Number",
choices = as.character(1:3),
options = list(
placeholder = 'Please select a number',
onInitialize = I('function() { this.setValue(""); }'),
create = TRUE
))
),
mainPanel = mainPanel(
fluidRow(
uiOutput('select2')
)
)
)
)
server <- function(input, output, session) {
dat <- data.frame(
Number = as.character(rep(1:3, each = 3)),
Letter = letters[1:9]
)
output$select2 <- renderUI({
req(input$Select1)
selectizeInput(inputId = "Select2",
label = "Select A Letter",
choices = dat$Letter[dat$Number %in% input$Select1])
})
}
shinyApp(ui, server)

R Shiny SelectizeInput dropdown avoid auto close

How to prevent the selectizeInput/pickerInput dropdown from closing by clicking outside in R Shiny?. Here is the sample code to explain the problem. I need to always see the selectizeInput choices even the user clicks the shiny body somewhere. The main selectizeInput dropdown should not close.
library(shiny)
library(shinyjs)
library(shinyWidgets)
ui <- fluidPage(
div(HTML("<br><br><br>")),
selectizeInput(
'upwardId', label = "Select Number", choices = 1:40,
options = list(maxItems = 20)
),
div(HTML("<br><br><br>")),
pickerInput(
inputId = "month",
label = "Select a month",
choices = c("Jan","Feb","March","Apr","May"),
multiple = TRUE,
options = pickerOptions(
maxOptions=3,
dropupAuto = FALSE,
actionsBox = TRUE,
title = "Please select a month",
header = "title..."
))
)
server <- function(input, output, session){}
shinyApp(ui, server)

Disabling Confirm Button in confirmSweetAlert

I'm trying to disable the confirm button in confirmSweetAlert unless selectizeInput has some input within it. There seem to be solutions by using Javascript, such as swal.disableConfirmButton() and document.getElementsByClassName().disabled = true, but when I run them under shinyjs::runjs, these don't seem to work. Are there any solutions out there to resolve this issue? Here's my sample code:
shinyApp(
ui <- fluidPage(
actionButton("button", "Show Sweet Alert!")
),
server <- function(input, output, session) {
observeEvent(input$button, {
confirmSweetAlert(
session = session,
inputId = "letterSelect",
title = "Select a Letter!",
type = "info",
text = tags$div(
h4("Please select from the options below then press 'Confirm'.", align = "center"),
selectizeInput(
inputId = "letters",
label = NULL,
choices = c("A", "B", "C"),
options = list(placeholder = "None selected."),
multiple = TRUE,
width = '100%')
),
closeOnClickOutside = FALSE
)
})
}
)
This seems to work:
library(shiny)
library(shinyWidgets)
library(shinyjs)
shinyApp(
ui <- fluidPage(
useShinyjs(),
actionButton("button", "Show Sweet Alert!")
),
server <- function(input, output, session) {
observeEvent(input$button, {
confirmSweetAlert(
session = session,
inputId = "letterSelect",
title = "Select a Letter!",
type = "info",
text = tags$div(
h4("Please select from the options below then press 'Confirm'.", align = "center"),
selectizeInput(
inputId = "letters",
label = NULL,
choices = c("A", "B", "C"),
options = list(placeholder = "None selected."),
multiple = TRUE,
width = '100%')
),
closeOnClickOutside = FALSE
)
runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
})
observe({
if(is.null(input$letters)){
runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
}else{
runjs("Swal.getConfirmButton().removeAttribute('disabled');")
}
})
}
)

How can I change R-Shiny "checkboxinput" value to FALSE/TRUE programmatically?

I want to change the checkboxinput value to FALSE/TRUE during run-time. How can I do this?
checkboxInput(inputId = "smoother", label = "Overlay smooth trend line", value = FALSE)
You can use updateCheckboxInput(). See an example below:
Reproducible example:
library(shiny)
ui <- fluidPage(
actionButton(
inputId = "check",
label = "update checkbox"
),
checkboxInput(
inputId = "checkbox",
label = "Input checkbox"
)
)
server <- function(input, output, session) {
observeEvent(
eventExpr = input$check, {
updatedValue = !input$checkbox
updateCheckboxInput(
session = session,
inputId = "checkbox",
value = updatedValue
)
}
)
}
shinyApp(ui, server)

Resources