I am building a shiny app and I need a count button to be added that interacts with the numeric input. So I want a numeric input which users can freely use, but that also has a button that users can click that adds 1 to the numeric input (so if a user selects 20, and clicks the button the input becomes 21). I have both working separately, but can't get the interaction to work. The numeric input updates with the add button, but if I change the value with the add numeric input the simply continues at the number of clicks thus far. This is what I now have:
library(shiny)
# https://shiny.rstudio.com/reference/shiny/1.3.2/updateNumericInput.html
# https://gist.github.com/aagarw30/69feeeb7e813788a753b71ef8c0877eb
ui <- shinyUI(
fluidPage(
tags$b("Simple counter using reactiveValues() - An example"),
numericInput("inNumber", "Input number", 0),
actionButton("add1", "+ 1"),
plotOutput("plot")
)
)
server <- function(input, output, session) {
counter <- reactiveValues(countervalue = 0) # Defining & initializing the reactiveValues object
observeEvent(input$add1, {
counter$countervalue <- counter$countervalue + 1 # if the add button is clicked, increment the value by 1 and update it
})
observeEvent(input$add1, {
updateNumericInput(session, "inNumber", value = counter$countervalue )
})
output$plot <- renderPlot({
hist( rnorm(input$add1))
})
}
shinyApp(ui, server)