I have written a shiny app that presents different radio buttons to the user. Depending on the input, numbers will be added to a score/counter
and other radio buttons will pop up (and the previous will be disabled).
I am wondering now, how I could implement a "step back" button that would the user to go back one step (e.g., in the case of a misclick) which means:
- Hide the most recent radio button
- Activate the last radio button again
- Adjust the
score/counter
I learned how to add a "reset" button that calls session$reload()
which deletes everything and the user can start again. However, it would be much better if the user could just go back one step.
I found similar questions (Create general purpose "go back" button in shiny and https://www.collinberke.com/blog/posts/2021-09-12-shiny-implementing-a-next-and-back-button/index.html), however, these questions deal with a slightly different setting.
Example:
ui.R
ui <- fluidPage(
shinyjs::useShinyjs(),
# Add an invisible counter to store the total score
verbatimTextOutput(outputId = "counter", placeholder = TRUE),
# Add a radio button with two choices
radioButtons(inputId = "a",
# label = "a",
label = "a",
choices = c("10", "5"),
selected = ""),
# UI elements for the b and c radio buttons
uiOutput("b"),
uiOutput("c"),
uiOutput("d"),
uiOutput("c1"),
uiOutput("e"),
uiOutput("f"),
# Add a back button to allow the user to go back to the previous question
actionButton(
inputId = "reset_button",
label = "Reset",
width = "50%"
),
textOutput("reset_val")
)
server.R
server <- function(input, output, session) {
reset_rv <- reactiveVal(value = 0L)
# Initialize the counter to 0
counter <- reactiveValues(value = 0)
# Track the selected options
selected_options <- reactiveValues(
a = NULL,
b = NULL,
d = NULL,
c = NULL,
e = NULL,
f = NULL
)
# Update the counter when the a radio button is clicked
observeEvent(input$a, {
if (!is.null(input$a)) {
selected_options$a <- input$a
if (input$a == "5") {
counter$value <- counter$value + 0
output$b <- renderUI({
radioButtons(inputId = "b",
label = "b",
choices = c("a", "10"),
selected = "")
})
} else if (input$a == "10") {
counter$value <- counter$value + 8
output$c <- renderUI({
radioButtons(inputId = "c",
label = "c",
choices = c("L", "R"),
selected = "")
})
}}
shinyjs::disable("a")
})
# 2 -----------------------------------------------------------------------
observeEvent(input$b, {
if (!is.null(input$b)) {
selected_options$b <- input$b
if (input$b == "5") {
counter$value <- counter$value + 0
output$d <- renderUI({
radioButtons(inputId = "d",
label = "d",
choices = c("5", "10"),
selected = "")
})
} else if (input$b == "10") {
counter$value <- counter$value + 6
output$c1 <- renderUI({
radioButtons(inputId = "c1",
label = "c",
choices = c("L", "R"),
selected = "")})}}
shinyjs::disable("a")
shinyjs::disable("b")
})
observeEvent(input$c, {
if (!is.null(input$c)) {
selected_options$c <- input$c
if (input$c == "R") {
counter$value <- counter$value + 0
output$e <- renderUI({
radioButtons(inputId = "e",
label = "e",
choices = c("5", "10"),
selected = "")
})
} else if (input$c == "L") {
counter$value <- counter$value + 4
output$f <- renderUI({
radioButtons(inputId = "f",
label = "L",
choices = c("5", "10"),
selected = "")})}}
shinyjs::disable("a")
shinyjs::disable("c")
})
# Update the counter output
output$counter <- renderText({
paste("Score:", counter$value)
})
observeEvent(input$reset_button, {
reset_rv(input$reset_button)
session$reload()
})
}
Run
shinyApp(ui = ui, server = server)