Given is a checkbox in an R Shiny app. The shown choices represent numeric values. If one or more choices are selected the numeric values should be summed up to the end result.
If checkbox choices change the result should be updated
In the following an example:
library(shiny)
tbl <- cbind(letters[1:4], 1:4)
ui =fluidPage(
checkboxGroupInput("tbl","tbl:",tbl[,1]),
htmlOutput("tbl_sum")
)
server = function(input, output, session) {
tbl_1 <- reactive({
if (input$tbl == tbl[1,1]) {
tbl_txt <- tbl[1,2]
} else {0}
})
tbl_2 <- reactive({
if (input$tbl == tbl[2,1]) {
tbl_txt <- tbl[2,2]
} else {0}
})
tbl_3 <- reactive({
if (input$tbl == tbl[3,1]) {
tbl_txt <- tbl[3,2]
} else {0}
})
tbl_4 <- reactive({
if (input$tbl == tbl[4,1]) {
tbl_txt <- tbl[4,2]
} else {0}
})
tbl_sum <- reactive({ sum(tbl_1(), tbl_2(), tbl_3(), tbl_4()) })
output$tbl_sum <- renderText({
paste("<font color=red><b>", "here should appear a result", "</p>",
"for example: if 'b' and 'd' is checked the result should be", "</p>",
"<font color=black><font size=12><b>",
sum(as.numeric(tbl[2,2]) + as.numeric(tbl[4,2])))
})
}
runApp(list(ui = ui, server = server))
I´m neither sure if 'reactive' is the best way or rather 'observe', nor am I sure whether the data structure (tbl) and written function (tbl_1 <-, tbl_2 <-, tbl_...) are the best ways to solve this task. Smarter solutions welcome!
Your approach is a bit complicated, i.e. you can get all the selected values in one go using one reactive
. Also, instead of storing the categories and values in a matrix you might consider using a dataframe which allows to store the values as numbers.
library(shiny)
tbl <- data.frame(name = letters[1:4], value = 1:4)
ui <- fluidPage(
checkboxGroupInput(
"tbl", "tbl:", tbl$name
),
htmlOutput("tbl_sum")
)
server <- function(input, output, session) {
tbl_values <- reactive({
tbl[match(input$tbl, tbl$name), "value"]
})
output$tbl_sum <- renderText({
paste(
"<font color=red><b>", "here should appear a result", "</p>",
"for example: if 'b' and 'd' is checked the result should be", "</p>",
"<font color=black><font size=12><b>",
sum(tbl_values())
)
})
}
runApp(list(ui = ui, server = server))