Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a count button to a shiny app that interacts with updateNumericInput

Tags:

r

shiny

tidyverse

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)

like image 409
Gh_Shiny Avatar asked Feb 15 '26 20:02

Gh_Shiny


2 Answers

Maybe this is closer to what you are looking for. You can use one observeEvent for input$add1 button (to increase the counter), and another observeEvent for input$inNumber (set set counter to the numericInput). Your hist could then reference the counter.

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
    updateNumericInput(session, "inNumber", value = counter$countervalue)
  })
  
  observeEvent(input$inNumber, {
    counter$countervalue <- input$inNumber
  })
  
  output$plot <- renderPlot({
    if (counter$countervalue > 0) {
      hist(rnorm(counter$countervalue))
    }
  })
  
}
like image 111
Ben Avatar answered Feb 21 '26 15:02

Ben


You have to reference the numeric input in the histogramm:

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 <- input$inNumber + 1     # if the add button is clicked, increment the value by 1 and update it
    updateNumericInput(session, "inNumber", value = counter$countervalue )
  })
  
  output$plot <- renderPlot({
    hist( rnorm(input$inNumber))
  })
}


shinyApp(ui, server)
like image 43
eastclintw00d Avatar answered Feb 21 '26 14:02

eastclintw00d