Here are two numericInput
fields whose total is shown in a third field.
If I highlight all of the digits in Field A and press delete or space the field is left blank and the sum no longer works, because not all the elements to add are numbers.
How is it possible to have the field show "0" if:
- All the digits are selected and either space or delete is pressed.
- All the digits are deleted.
Also, even though it's a numeric field the keys "e", ".", and "+" are allowed. Can they be prevented from appearing?
library("shiny")
library("bslib")
library("shinyWidgets")
ui <- bootstrapPage(
# https://bootswatch.com/journal/
theme = bs_theme(version = 5, "font_scale" = 1.0),
div(class = "container-fluid",
div(class = "row",
div(class="col-4",
numericInputIcon(
inputId = "a",
label = "A",
value = 10,
min = 0,
max = 9000,
width = "160px",
icon = list(NULL, per_month)
),
),
div(class="col-4",
numericInputIcon(
inputId = "b",
label = "B",
value = 255,
min = 0,
max = 9000,
width = "160px",
icon = list(NULL, per_month)
),
),
div(class="col-4",
numericInputIcon(
inputId = "total",
label = "Total",
value = 0,
min = 0,
max = 9000,
width = "160px",
icon = list(NULL, per_month)
),
),
)
)
)
server <- function(input, output, session) {
observe({
updateNumericInputIcon(
session = session,
inputId = "total",
value = sum(c(input$a, input$b),na.rm=T)
)
})
# This doesn't seem to work
observe({
if (!is.numeric(input$a)) {
updateNumericInput(session, "a", 0)
}
})
}
shinyApp(ui = ui, server = server)