Reputation: 535
As this link says about Action Button, the input$action
return the number of clicks
On the button.
enter link description here
My question is:
In other words:
I click 3 times , the input$action
sends me the value 3, I store this value, and starts again(somehow, I need to make input$action
come back to zero), clicking 8 times the input$action sends me the value 8 I store again e keeps going….
Upvotes: 1
Views: 311
Reputation: 7116
If we really need to "reset" the button, we can re-render it. Here is an example app:
library(shiny)
library(dplyr)
ui <- fluidPage(
uiOutput("action"),
actionButton("reset", "Reset"),
verbatimTextOutput("values")
)
server <- function(input, output, session) {
output$action <- renderUI({
actionButton("action_but", "Action")
})
vals <- reactiveVal(tibble(clicks = integer(0)))
counter <- reactiveVal(0)
observe({
counter(input$action_but)
})
observeEvent(input$reset,
{
vals(add_row(vals(), clicks = as.integer(input$action_but)))
output$action <- renderUI({
actionButton("action_but", "Action")
})
counter(input$action_but)
},
ignoreInit = TRUE
)
output$values <- renderPrint({
list(counter(), vals())
})
}
shinyApp(ui, server)
Upvotes: 2
Reputation: 2623
The input$button
is read only, so you can't reset the input$button
increment without resetting the entire app, which would cause you to lose your saved values. Alternatively, you can keep a copy of the values and reset the count on your copy.
Not sure what you want to do with the reset, but you can still count the number of button presses between your steps, 3, 8 etc. So 8 button presses from 3 would be at input$button == 11
.
library(shiny)
ui <- fluidPage(
# Copy the line below to make an action button
actionButton("action", label = "Action"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")),
column(6, verbatimTextOutput("countOutput")))
)
server <- function(input, output) {
# You can access the value of the widget with input$action, e.g.
output$value <- renderPrint({ input$action })
df <- reactiveValues(three = NULL, eight = NULL, thirteen = NULL)
observe({
presses <- c(3, 8, 16)
if(is.null(input$action)){
} else if(input$action == presses[1] & is.null(df$three)) {
df$three = input$action
} else if(input$action == presses[2] & is.null(df$eight)){
df$eight = input$action - presses[1]
} else if(input$action == presses[3] & is.null(df$thirteen)) {
df$thirteen = input$action - presses[2]
}
})
output$countOutput <- renderPrint({
paste(df$three[1], df$eight[1], df$thirteen[1])
})
}
shinyApp(ui, server)
Upvotes: 1
Reputation: 7636
It may be simpler to not use the value of the actionButton
directly and just record the number of clicks between events in an external reactiveValues
object. This for example has two buttons, one to increase the value and another to add the final value to a vector and reset the counter:
library(shiny)
ui <- fluidPage(
actionButton("action", label = "Action"),
actionButton("store", label = "Store"),
hr(),
fluidRow(column(2, verbatimTextOutput("value")))
)
server <- function(input, output) {
values <- reactiveValues(curr = 0,
out = integer())
observeEvent(input$action, {
values$curr = values$curr + 1
})
observeEvent(input$store, {
values$out <- c(values$out, isolate(values$curr))
values$curr <- 0
})
output$value <- renderPrint({
values$out
})
}
shinyApp(ui, server)
Created on 2022-01-06 by the reprex package (v2.0.1)
Upvotes: 2