В данном конкретном случае возможны два варианта: обернуть первый график в shiny::conditionalPanel()
или использовать shiny::renderUI()
.
shiny::conditionalPanel()
Пример
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
conditionalPanel("input.bins == 4", plotOutput("distPlot")),
plotOutput("distPlot2"))))
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')})
}
shinyApp(ui = ui, server = server)
shiny::renderUI()
Пример
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
uiOutput("distPlots"))))
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot2 <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')})
output$distPlots <- renderUI({
if(input$bins == 4)
fluidPage(plotOutput("distPlot"), plotOutput("distPlot2"))
else
plotOutput("distPlot2")
})
}
shinyApp(ui = ui, server = server)
Оба подхода дают следующее: