Home > database >  Shiny input as default value for another input
Shiny input as default value for another input

Time:04-30

Is it possible to have the product from two inputs as a default value for another input?

Even, more complex, is it possible to include an if cycle in the computation?

Example:

library(shiny) 

ui <- fluidPage(
numericInput("factor","Factor:")
numericInput("delta","Delta:"),
numericInput("size","Size:"),
numericInput("result","result:",value = input$delta*input$size)
) 

Advanced:

I would like to have input$delta*input$size multiplied for input$factor if input$factor is different than 100.

I tried to pass the value in the server by connecting it to an output and then having the latest as input but it did not work (I would say obviously).

Any help?

CodePudding user response:

If I understand the problem correctly, you want to update the numericInput for factor when delta and/or size change.

observe({
    if(as.numeric(isolate(input$factor)) != 100){ 
        updateNumericInput(inputId = "factor", value = input$delta*input$size)
    }
})

You need to isolate input$factor otherwise it will always revert back to the size*delta every time factor is changed by a user.

CodePudding user response:

To make one input dependent on the value of another, you need to specify it in the server. I usually render the result using uiOutput() and renderUI

library(shiny) 


shiny::shinyApp(ui = fluidPage(
  numericInput("factor","Factor:", 1),
  numericInput("delta","Delta:", 1),
  numericInput("size","Size:", 1),
  uiOutput('res')
  
), server = function(input, output){
  output$res <- renderUI({
    numericInput("result","result:",value = input$delta*input$size)
  })
})

  • Related