Search code examples
rshinyautofilltextinput

Using text input to calculate value for another text input in shiny app


I would like to give the option to either input a percentage or a value. When the user inputs one of the two, I would like the other to autofill.

So for the code:

textInput(DownDollars, "Downpayment (Dollars)", value = "", width = NULL, placeholder = NULL),

textInput(DownPercent, "Downpayment (Percent)", value = "", width = NULL, placeholder = NULL)

Is there a way to use the output of these inputs to replace the value = option?

UPDATE: Here is the code that doesn't work for using another input for the reference value:

ui <- fluidPage(
  numericInput("HomePrice", "Home Price", value = ""),
  
  numericInput("DownPaymentDollars", "Down Payment (Dollars)", value = "", width = NULL),
  
  numericInput("DownPaymentPercent", "Down Payment (Percent)", value = "", width = NULL)
)

server = function(input, output, session){
  
  #referenceValue <- 254
  
  observeEvent(input$DownDollars, {
    updateNumericInput(session, "DownPaymentPercent", value = input$DownDollars * 100 / input$HomePrice)
  })
  
  observeEvent(input$DownPercent, {
    updateNumericInput(session, "DownPaymentDollars", value = input$DownPercent * input$HomePrice / 100)
  })
  
}

shinyApp(ui, server)

Solution

  • First it's easier to use numericInput to get numeric values. The following code does what you need:

    library(shiny)
    
    
    ui <- fluidPage(
      numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
      
      numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
    )
    
    server = function(input, output, session){
      
      referenceValue <- 254
      
      observeEvent(input$DownDollars, {
        updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / referenceValue)
      })
      
      observeEvent(input$DownPercent, {
        updateNumericInput(session, "DownDollars", value = input$DownPercent * referenceValue / 100)
      })
      
    }
    
    shinyApp(ui, server)
    

    I defined a referenceValue to calculate the percentage, you can define this value as you want, for example taken from another user input.

    Be careful to define inputs IDs with quotes.

    EDIT

    When using another numericInput to get the reference value, you need to observe this new numericInput to update the calculations. One of the two updateNumericInput must be triggered by two observeEvent at the same time (see this post explaining the syntax):

    library(shiny)
    
    
    ui <- fluidPage(
      numericInput("HomePrice", "Home Price", value = ""),
      numericInput("DownDollars", "Downpayment (Dollars)", value = "", width = NULL),
      
      numericInput("DownPercent", "Downpayment (Percent)", value = "", width = NULL)
    )
    
    server = function(input, output, session){
      
      
      observeEvent({
        input$HomePrice
        input$DownDollars
        }, {
        updateNumericInput(session, "DownPercent", value = input$DownDollars * 100 / input$HomePrice)
      })
      
      observeEvent(input$DownPercent, {
        updateNumericInput(session, "DownDollars", value = input$DownPercent * input$HomePrice / 100)
      })
      
    }
    
    shinyApp(ui, server)