How to solve bug with numericInput in shiny: Input "jumps" with multiple input fields

228 Views Asked by At

For a project I am using multiple numericInput fields in shiny to enter certain amounts. The app user should be able to add new amounts if needed and type in any kind of numeric value, either via the arrows or manually. However, when adding new fields and trying to add amounts manually the input seems to "jump" and sometimes the whole app crashes.

I thought about using textInput fields instead of numericInput but this does not seem to work either.

This is a MWE of the code I am using. Once you add a couple of amount fields via the plus-button and enter values manually, it starts jumping/not working.

ui <- fluidPage(#....design etc.,

    mainPanel(
      uiOutput("inputwidgets"),

      actionButton(inputId = "number",
                   label = icon(name = "plus",
                                lib = "font-awesome")),
      actionButton(inputId = "delete_number",
                   label = icon(name = "minus",
                                lib = "font-awesome")),

      actionButton("update", "Calculate")



    )
)

server <- function(input, output) {
  reac <- reactiveValues()

  observeEvent(c(input$number,input$delete_number), {
    # you need to add 1 to not start with 0
    add <- input$number+1
    # restriction for delete_number > number
    delete <- if(input$delete_number > input$number) add else input$delete_number
    calc <- add - delete
    reac$calc <- if(calc > 0) 1:calc else 1
  })
  # By clicking the actionButton "number" an additional row appears
  observe({
    req(reac$calc)
    output$inputwidgets = renderUI({
      input_list <- lapply(reac$calc, function(i) {
      amount <- input[[paste0("amount",i)]]

        # for each dynamically generated input, give a different name
        fluidRow(
          column(2,

                 # Input: Specify the amount ----
                 numericInput(
                   paste0("amount",i),
                   label="Amount",
                   #step = 1000,
                   value = if(!is.null(amount)) amount else 0
                 )
          )
        )
      })
      do.call(tagList, input_list)
  })
})

}
# Create Shiny app ----
shinyApp(ui = ui, server = server)

This issue is really annoying and disturbs the whole user experience. Would be grateful about any kind of help with this.

0

There are 0 best solutions below