R Shiny:如何在 NULL 时不显示绘图

Joh*_*ohn 4 r shiny

如果您在 R 工作室中运行此代码,您会发现 NULL 数据的绘图仍然是一大块白色。

数据为NULL时怎么不显示呢?

在其他图表中,大白板看起来不太好。

library(shiny)

server <- function(input, output) {
  output$x = renderPlot(NULL)
}

ui <- fluidPage(
  br(),
  plotOutput("x"),
  tags$head(tags$style(HTML("
                            body {
                            margin: 0;
                            font-family: helvetica, sans-serif;
                            background: #F2F0F0;
                            }

                            .shiny-plot-output{
                            max-width: 100%;
                            box-shadow: 0px 0px 6px #888;
                            margin-left: auto;
                            margin-right: auto;
                            }
                            ")))  
  )

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

Rob*_*t.S 5

我找到了一种替代解决方案(因为上面的解决方案对我不起作用)。它利用了shinyjs包。这是一个最小的可重现示例。

library(shinyjs)
library(shinythemes)


shinyApp(
  ui = fluidPage(theme = shinytheme("darkly"),
                 useShinyjs(), # IMPORTANT, you have to call this function in your UI to use shinyjs
                 numericInput("num","Number", 5),
                 plotOutput("text1")
  ),
  server = function(input, output) {
    
    observe({
      if (input$num < 5) {
        hide("text1")
      } else {
        show("text1")
      }
    })
    
    output$text1 <- renderPlot({
      plot(rnorm(input$num))
    })
  }
)
Run Code Online (Sandbox Code Playgroud)

仅当 x > 5 时才会显示该图。否则,该图(以及此处通过使用闪亮主题中的主题突出显示的空白darkly- 不存在)