Getting multiple outputs from reactive

為{幸葍}努か 提交于 2020-01-23 07:47:47

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/28378724/getting-multiple-outputs-from-reactive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!