renderPlot 需要一个生成绘图的表达式。因此,您不能将 html 标签传递给它。
但是,您可以使用conditionalPanel 根据条件显示 UI 元素:
library(shiny)
ui <- fluidPage(
checkboxInput("toggle", "toggle"),
conditionalPanel("input.toggle == true", plotOutput("myPlot")),
conditionalPanel("input.toggle == false", p("Generic message"))
)
server <- function(input, output, session) {
output$myPlot <- renderPlot({plot(1:10)})
}
shinyApp(ui, server)
另一种方法可以通过使用renderUI来实现:
library(shiny)
ui <- fluidPage(
checkboxInput("toggle", "toggle"),
uiOutput("myUIOutput")
)
server <- function(input, output, session) {
output$myPlot <- renderPlot({plot(1:10)})
output$myUIOutput <- renderUI({
if(input$toggle == TRUE){
plotOutput("myPlot")
} else {
p("Generic message")
}
})
}
shinyApp(ui, server)