Reputation: 2380
I wish to have a question where there are two answer options - a numeric field and a radio button. If one option is selected the other should be unselected.
A. In the example below, when the checkbox (that looks like a radio button) is selected the numericField is cleared. When the checkbox is unselected, the numeric field is reset to its default value of 100. So far so good.
B. When a number is entered in the numeric field, the checkbox should be unchecked. If the numeric field is cleared the checkbox should be selected since at least one of the answer options must be TRUE.
I'm having trouble with part B.
library("shiny")
library("bslib")
each_month <- "each month"
ui <- bootstrapPage(
tags$head(
tags$style("#i_pay{
font-size: 18px !important;
color: #525250;}"
)
),
theme = bs_theme(version = 5, "font-scale" = 0.8),
div(class = "container-fluid",
div(class = "row",
div(class="col-6",
numericInputIcon(
inputId = "i_pay",
label = "I pay",
value = 100,
min = 0,
max = 9000,
width = 220,
icon = list(NULL, each_month)
),
div(class="col-6",
prettyCheckbox(
inputId = "the_min",
label = "or the minimum",
value = FALSE,
status = "default",
shape = "round", # c("square", "curve", "round"),
outline = FALSE,
animation = NULL,
icon = NULL,
bigger = TRUE,
inline = TRUE,
width = NULL
)
)
)
)
)
)
server <- function(input, output, session) {
# If Checkbox
observeEvent(input$the_min, {
# Checked
if(input$the_min == TRUE){
updateNumericInputIcon(
inputId = "i_pay",
value = "")
} else {
# Unchecked
updateNumericInputIcon(
inputId = "i_pay",
value = 100)
}
})
num_reactive <- eventReactive(input$i_pay, {
if(num_reactive == ""){
updatePrettyCheckbox(
inputId = "the_min",
value = TRUE)
} else {
updatePrettyCheckbox(
inputId = "the_min",
value = FALSE)
}
})
}
shinyApp(ui, server)
Upvotes: 0
Views: 177
Reputation: 388862
Here is an option using observe
-
library(shiny)
library(bslib)
library(shinyWidgets)
each_month <- "each month"
ui <- bootstrapPage(
tags$head(
tags$style("#i_pay{
font-size: 18px !important;
color: #525250;}"
)
),
theme = bs_theme(version = 5, "font-scale" = 0.8),
div(class = "container-fluid",
div(class = "row",
div(class="col-6",
numericInputIcon(
inputId = "i_pay",
label = "I pay",
value = 100,
min = 0,
max = 9000,
width = 220,
icon = list(NULL, each_month)
),
div(class="col-6",
prettyCheckbox(
inputId = "the_min",
label = "or the minimum",
value = FALSE,
status = "default",
shape = "round", # c("square", "curve", "round"),
outline = FALSE,
animation = NULL,
icon = NULL,
bigger = TRUE,
inline = TRUE,
width = NULL
)
)
)
)
)
)
server <- function(input, output, session) {
# If Checkbox
observe({
# Checked
if(input$the_min){
updateNumericInputIcon(session,
inputId = "i_pay",
value = "")
} else {
# Unchecked
updateNumericInputIcon(session,
inputId = "i_pay",
value = 100)
}
})
observe({
if(!is.na(input$i_pay) && input$i_pay != '') {
updatePrettyCheckbox(session, "the_min", value = FALSE)
}
})
}
shinyApp(ui, server)
Upvotes: 1