BlueRhapsody
BlueRhapsody

Reputation: 93

R Shiny make two inputs equal

I want to somehow make two inputs equal to one another when I press an Actionbutton. I have several tabs in my Shiny Dashboard and in some of the tabs there are repeat data inputs. For example, in tab 1 we have an input for AGE, we have the same input in tab 2. I want to make it such that if I input a value for age and click "run" (an Actionbutton), then it automatically makes the age in the other tab equal to the one in the current tab.

I'm really really new to using Shiny, so I made a few attempts at using reactive commands, but I'm not sure why it isn't working:

  reactive(  
if(input$runbutton==0){
input$numeric1=input$numeric2
})

That code has no effect, it doesn't even return an error.

Upvotes: 0

Views: 500

Answers (1)

MrFlick
MrFlick

Reputation: 206308

You should use updateNumericInput() to change the value of an input. Also it makes more sense to use observeEvent() to track button presses. Here a simple sample application

ui <- fluidPage(
    numericInput("numeric1","in1",1), 
    numericInput("numeric2","in2",2), 
    actionButton("runbutton", "set equal")
 )

server <- function(input, output, session) {
    observeEvent(  input$runbutton, {
        updateNumericInput(session, "numeric1", value=input$numeric2)
    })
}


shinyApp(ui=ui, server=server)

Upvotes: 4

Related Questions