Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the same output binding on different tab panels in shiny

Tags:

r

shiny

In my server.R file, I have a reactive called myNet which generates a visNetwork. In my ui.R, I have multiple tab panels that ideally will have different input widgets that effect the visNetwork.

Is it possible to re-use the same binding?

Currently, when I try to run the code similar to below, I receive an error: Uncaught Duplicate binding for ID vis.


Snippet of server.R

  myNet <- reactive({
    nodes <- df_nodes
    edges <- df_edges        
    visNetwork(nodes, edges, height = '800px')
  })

  output$vis <- renderVisNetwork(
    myNet()
  )

Snippet of ui.R

  ...

  tabPanel("First Panel",
    sidebarLayout(
      sidebarPanel(
        sliderInput("input1", "Title 1", 
                    min=1, max=10, value=1),
        sliderInput("input2", "Title 2",
                    min=1, max=10, value=1),
        sliderInput("input3", "Title 3",
                    min=1, max=10, value=1)
      ),
      mainPanel(
        visNetworkOutput("vis", height = '800px') # *** ISSUE HERE***
      )
    )
  ),
  tabPanel("Second Panel",
    sidebarLayout(
      sidebarPanel(
        sliderInput("input4", "Title 4", 
                    min=1, max=10, value=1),
        sliderInput("input5", "Title 5",
                    min=1, max=10, value=1),
      ),
      mainPanel(
        visNetworkOutput("vis", height = '800px') # *** ISSUE HERE***       
      )
    )
  ), ...
like image 336
JasonAizkalns Avatar asked Jun 16 '15 15:06

JasonAizkalns


1 Answers

I ended up opening this GitHub issue. The consensus appears to be that the following is the best method:

output$vis_1 <- output$vis_2 <- renderVisNetwork(myNet())

Alternatively, as alluded to in the comments, you could use a not-so-DRY approach:

output$vis_1 <- renderVisNetwork(myNet())
output$vis_2 <- renderVisNetwork(myNet())

Joe Cheng appropriately made the following comment on the GitHub issue:

"Allowing multiple outputs to share the same ID would have a lot of gnarly side effects, it makes my head hurt to even think about what it would mean. For example, two Leaflet maps with the same ID--but they both have lots of state in the browser, and communicate that state back to the client. How can you make sense of that when they share the same ID?"

All said and told, I was able to ahieve the same behavior using the dynamic UI approach with a switch that toggles which controls are displayed to the user via uiOutput() (in the ui.R file) and output$ui <- renderUI({...}) (in the server.R file).

like image 93
JasonAizkalns Avatar answered Nov 19 '22 23:11

JasonAizkalns