具有两个条件的闪亮 renderPlotly

Shiny renderPlotly with two conditions

我正在用 Shiny 开发应用程序。我想使用提交按钮渲染图。如果用户选中输入复选框,我还想打印标签。我能够通过按钮渲染情节。但是当复选框被选中时它不起作用。

代码如下:

 library(shiny)
 library(plotly)

 ui <-  fluidPage(
 actionButton('RUN', 'Run'),
 checkboxInput('PrintLab', 'Label', FALSE),
 plotlyOutput("plot1")
 )

 server = function(input, output) {
 output$plot1 <- renderPlotly({
 req(input$RUN)
 isolate({
   ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
           geom_point(size=1, colour = "grey"))
   })

  req(input$PrintLab)
  isolate({
   ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
           geom_point(size=1, colour = "grey") +
           geom_text(aes(label=wt), size=3, colour = "black"))
   })

 })
}

 runApp(shinyApp(ui, server))

我不是 Shiny 专家,但 req(input$PrintLab) 我觉得不对。 这是否实现了您的目标?

server <- function(input, output) {
  output$plot1 <- renderPlotly({
    req(input$RUN)

    if (!input$PrintLab) {
      ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
             geom_point(size=1, colour = "grey"))
    } else {
      ggplotly(ggplot(data = mtcars, aes(wt, hp)) + 
             geom_point(size=1, colour = "grey") +
             geom_text(aes(label=wt), size=3, colour = "black"))
    }

  })
}

(我敢肯定有更好的方法。这只是我的想法。)