This is an old Shiny issue: shiny app will not work if the same "output" is used two times in Ui.R
A simple example :
library(shiny)
## app.R ##
server <- function(input, output) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)
),
mainPanel(plotOutput("distPlot")
# If the next line is commented out, histogram appears correctly
,plotOutput("distPlot")
)
)
)
shinyApp(ui = ui, server = server)
This doesn't work because :
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. See this or this.
The result looks fine, but isn't what is expected (no histogram shown) :
The ShinyApp seems to be working normally :
Listening on http ://127.0.0.1:7081
Although I know this issue, I have already been tricked a few times in complex UIs, and I was wondering if there was a way to output an automatic warning in the console on this?
For example :
Warning message: Output 'distPlot' is used twice in UI - this is not supported and might lead to unexpected results
Thanks for sharing your solutions on this issue!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…