8

I need to get multiple outputs from reactive component in shiny. Example:

output_a,output_b <- reactive({
  a <- input$abc
  b <- input$abc*10
  a
  b
})

How could something like above be done through which I can get two outputs a and b from one reactive component?

3 Answers 3

12

So I had the same issue, I wanted two outputs from one reactive (I was using a for loop and an ifelse statement to assign variables to 1 of 2 lists, and I needed both lists to be returned).

I found the following workaround, I'm not sure if it will also work for you, but I'm posting it here in case it helps someone:

combo_output <- reactive({
  a <- input$abc
  b <- input$abc*10
  combo <- list(a = a, b = b)
  combo
  })

then you can access these later as such:

    output$someOutput <- renderSomething({
        combo <- combo_output()
        a <- combo$a
        b <- combo$b
        ...
    })

Not sure if this is an optimal solution, but it worked for me.

Sign up to request clarification or add additional context in comments.

Comments

2

If I understand correctly you would like to create a reactive to changes in input$abc. Every time the UI would change the input$abc, you would like the values of the server for a and b to change.

If so: Based on the tutorial, I would suggest having 2 successive reactives:

output_a<-reactive({
   input$abc
})

output_b<-reactive({
   input$abc*10
})

Keep in mind that they will be successively executed, first you will get output_a and afterwards output_b.

Hope this helped you.

Comments

1

I have had luck using a return statement in combination with a list.


data <- reactive({
  a <- input$abc
  b <- input$abc*10
  return(list(a = a, b = b))
})

output$stuff <- renderDataTable({
    a <- data()[[1]]
    b <- data()[[2]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.