1

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?

2 Answers 2

2

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)
  })
})

6
  • Actually, I would need output$res to be available for changes from the user (so it should be another input and not an output). As I said in the comment before, I am looking to have input$delta*input$size as default value in the input result
    – Akibaida
    Commented May 2, 2022 at 8:18
  • 1
    That reprex should run, and does what you describe -- the uiOutput will render as a numeric input with the id result.
    – mattr
    Commented May 2, 2022 at 13:20
  • 1
    You use uiOutput in a variety of situations, but one is your use case where inputs need to be dynamic. Essentially everything that is dynamic in a Shiny app must be defined in a server function, so we create the needed UI elements using uiOutput in the ui function and renderUI in the server function. You can interact with it as you would with any other input.
    – mattr
    Commented May 2, 2022 at 13:26
  • Thank you, your explanation clarified everything. Truly appreciate!
    – Akibaida
    Commented May 2, 2022 at 16:24
  • last question on this before closing the topic, how can I call the output$res into cat() function in an rmarkdown file?
    – Akibaida
    Commented May 3, 2022 at 9:21
0

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.

1
  • Not completely. Working on the basic case, I would like to have as default value of the input result, the product input$delta*input$size . The code I posted is not working as it provides error in value = input$delta*input$size
    – Akibaida
    Commented May 2, 2022 at 8:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.