Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shiny app will not work if the same "output" is used two times in Ui.R #743

Closed
crtahlin opened this issue Feb 25, 2015 · 8 comments
Closed

Comments

@crtahlin
Copy link
Contributor

crtahlin commented Feb 25, 2015

If the same "output" slot is referenced two times in Ui.R, the browser will not output results nor will the shiny app issue any errors for debugging.

I do not know if this is more of a browser issue than that of shiny, but I think some kind of warning/error message would be appropriate in this case.

Below is example code for reproducing the issue.

Server.R:

library(shiny)

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')

  })

Ui.R

library(shiny)

shinyUI(fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      h3("Example text"),
      plotOutput("distPlot"),
      plotOutput("distPlot")

    )
  )
))
@wch
Copy link
Collaborator

wch commented Feb 25, 2015

Yes, Shiny doesn't support multiple outputs with the same name. This code would generate HTML where two elements have the same ID, which is invalid HTML.

I agree that it would be nice to have a warning/error for this. Unfortunately isn't a simple way to do this. The validation process would require Shiny looking at the entire HTML document and checking for multiple instances of each ID. Presently there's nothing like this in Shiny, although it might not be a bad idea to add a validation stage that checks for this and other things.

@jcheng5
Copy link
Member

jcheng5 commented Mar 5, 2015

I think there's an error message in the JavaScript console of the browser at least. Although admittedly those are easy to miss.

@jcheng5 jcheng5 closed this as completed Mar 5, 2015
@mbannert
Copy link

mbannert commented Jul 5, 2016

Sorry to come up with this again, but is there an easy way to display things twice? Let's assume I'd like to show the same plotOutput in two different tabs of a shiny app. I understand it's not easily possible because of the invalid HTML / id thing, but I wonder whether there is an easy way to 'reference' to another output.

@bborgesr
Copy link
Contributor

bborgesr commented Jul 5, 2016

You don't need the same id to show the same thing. The easiest way would be to encapsulate all your logic and input dependencies in a reactive and then use that in the appropriate places. Ex:

library(shiny)
library(datasets)

# Define UI
ui <- fluidPage(
  selectInput('data', 'Choose dataset', c('cars', 'faithful')),
  numericInput('obs', 'How many obs to include?', 5),
  plotOutput('p1'),
  plotOutput('p2')
)

# Server logic
server <- function(input, output, session) {

  currentData <- reactive({
    data <- switch(input$data,
              "cars" = cars, "faithful" = faithful)
    head(data, n = input$obs)
  })

  output$p1 <- renderPlot({
    plot(currentData())
  })

  output$p2 <- renderPlot({
    plot(currentData())
  })
}

# Complete app with UI and server components
shinyApp(ui, server)

@chrispenshumba
Copy link

Or you can try modularising your app , using shiny modules. There's a link here to help https://www.ardata.fr/en/post/2019/02/11/why-using-modules/

@ismirsehregal
Copy link
Contributor

The example provided by @bborgesr can be reduced a little more by using:

  output$p2 <- output$p1 <- renderPlot({
    plot(currentData())
  })

@j-craggy
Copy link

I'm having an issue where if I run my Shiny app 'maximized' on the same monitor where the IDE runs (R Workbench), it takes about 20 seconds to render. Otherwise if the app is minimized, or runs maximized on another display other than where the IDE runs, the app renders quickly without lag. Maybe this is the wrong place to address my issue but I am hoping to find an answer as it's an odd behavior I cannot find an answer to. Thank you.

@corey-dawson
Copy link

The example provided by @bborgesr can be reduced a little more by using:

  output$p2 <- output$p1 <- renderPlot({
    plot(currentData())
  })

This was a great solution. Was looking to add a consistent title (uiOutput) across multiple tabs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants