5
votes

I am trying to make a reactive UI with sliders that drop in and out via a dropdown in shiny. I have a server with reactiveUI sliders (server.R):

library(shiny)
shinyServer(function(input, output) {
  output$slider1 <- reactiveUI(function() {
    sliderInput("s1", "slide 1", min = 1,  max = 100, value = 1)  
  })

  output$slider2 <- reactiveUI(function() {
    sliderInput("s2", "slide 2", min = 1,  max = 100, value = 1)   
  }) 
})

I can run the server fine with the following code (ui.R):

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Hello Shiny!"),
  sidebarPanel(

    selectInput("dataset", "number of buckets:", 
                choices = c(1,2,3)),

    conditionalPanel(
      condition = "input.dataset==2",
      uiOutput("slider1"),uiOutput("slider2")),

    conditionalPanel(
      condition = "input.dataset==1",
      sliderInput("s1", "slide 1", min = 1,  max = 100, value = 1) 
      )
  ),
  mainPanel(
  )
))

but if I try to make both conditionalPanels call uiOutput, the server freezes:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Hello Shiny!"),
  sidebarPanel(

    selectInput("dataset", "number of buckets:", 
                choices = c(1,2,3)),

    conditionalPanel(
      condition = "input.dataset==2",
      uiOutput("slider1"),uiOutput("slider2")),

    conditionalPanel(
      condition = "input.dataset==1",
      uiOutput("slider1") 
      )
  ),
  mainPanel(
  )
))

I have played around with this, and found that it happens anytime that use the same condition variable and multiple uiOutput calls. Any suggestions? Thanks.

1
The error message in the console reads Uncaught Duplicate binding for ID slider1. My guess is that binding variables occurs prior to execution of any conditional statements, which is why the error throws up.Ramnath
Thanks Ramnath. How did you get the error message to print to the console?Jim Crozier
If you are using Google Chrome, just right click on the page and choose Inspect Element. This will open up Developer Tools and you will find a tab named Console, which has the error messages. For Firefox, I believe you can use FireBug.Ramnath
I would recommend you post this on Shiny Google Group to get a faster answer.Ramnath
It's not just that there can only be one unique ID for each reactiveUI call--output IDs must be unique, period (and the same for input IDs). If you have two outputs on the page at the same time that both have the same ID, that should be considered a bug in the application, regardless of how they were created.Joe Cheng

1 Answers

5
votes

See the comment from @Joe for the answer.

Basically, outputIDs and inputIDs have to be unique; two UI elements with the same IDs on the same page emits and error. This is a limitation of the reactivity in shiny.

The work around from @Jim is to create multiple elements for each output or input used by the client, e.g.

 output$slider2_1 <- ...
 output$slider2_2 <- ...