R Shiny: how to not display a graph when NULL

If you run this code in your R studio, you will find that the graph for NULL data is still a huge block of white.

How can we not display it when the data is NULL.

A large board does not look so good among other graphs.

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)
+4
source share
3 answers

You can do something like this inside the .R server:

  if(is.null(df)){}
  else{
  output$x = renderPlot(df)
  }

This will check if the data you send to renderPlotwas NULL and only executed renderPlotif the data exists. This will probably make the canvas not a white, but a gray background. Only problem is that you are creating a shadow effect that will also appear on gray.

+2

( ). 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) {
    output$text1 <- renderPlot({
      if (input$num < 5) {
        hide("text1")
      } else {
        show("text1")
      }
      plot(rnorm(input$num))
    })
  }
)

, x > 5. ( , darkly ) )

+4

Good ideal for using a function shinyjs::showand shinyjs::hide, but how to use it in the newest function renderCachedPlot?

0
source

Source: https://habr.com/ru/post/1621373/


All Articles