Home > Mobile >  How to make a UI element render on load but then dependent on a button for further updates
How to make a UI element render on load but then dependent on a button for further updates

Time:12-23

I am trying to make a ui partially reactive in that when the app first loads, a result is calculated, but any further updated can only be made by triggering the action button. eg in app below, When app loads, the input1 value(4%) is multiplied by 400 and the result is shown as text output. Now any further update to input1 should not update the textOutput, until the Update button is pressed. I believe reactive is the wrong thing to use here, so I'd really appreciate guidance on this. Thank you

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  fluidRow(
    autonumericInput(
    inputId = "input1", label = "Percent Input", 
    value = 4, currencySymbol = " %",currencySymbolPlacement = "s",
    digitGroupSeparator = ",",decimalPlaces = 2),
    actionButton(inputId = "submit1", label = "Update"),
    verbatimTextOutput("resultUI")
  )
)

server <- function(input, output) {
  result<-reactive({
    400*input$input1/100
  })
  output$resultUI <- renderText({ result() })
}

shinyApp(ui = ui, server = server)

CodePudding user response:

You could use an eventReactive with ignoreNULL=FALSE so that it also triggers on load:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  fluidRow(
    autonumericInput(
      inputId = "input1", label = "Percent Input", 
      value = 4, currencySymbol = " %",currencySymbolPlacement = "s",
      digitGroupSeparator = ",",decimalPlaces = 2),
    actionButton(inputId = "submit1", label = "Update"),
    verbatimTextOutput("resultUI")
  )
)

server <- function(input, output) {
  result<-eventReactive(input$submit1,{
    400*input$input1/100
  },ignoreNULL = FALSE)
  
  output$resultUI <- renderText({ result() })
}

shinyApp(ui = ui, server = server)
  • Related