Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple outputs from reactive

Tags:

r

shiny

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?

like image 417
user2876812 Avatar asked Feb 07 '15 04:02

user2876812


2 Answers

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.

like image 86
Rekarrr Avatar answered Oct 31 '22 04:10

Rekarrr


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.

like image 26
Stanislav Avatar answered Oct 31 '22 04:10

Stanislav