fifi_ny
fifi_ny

Reputation: 11

Hide warnings from R console

I am trying to hide warnings from console when I run my shiny app I tried adding this to my ui

tags$style(type="text/css",
         ".shiny-output-error { visibility: hidden; }",
         ".shiny-output-error:before { visibility: hidden; }"
)

but it is not working please help thanks in advance

Upvotes: 0

Views: 3083

Answers (2)

JohnCoene
JohnCoene

Reputation: 2261

This is probably not the best way to hide those red error messages. You likely see those some output depends on an input that is yet defined.

See this app below:

library(shiny)

ui <- fluidPage(
  selectInput("datasetName", "Dataset", c("", "pressure", "cars")),
  plotOutput("plot"),
  tableOutput("table")
)

server <- function(input, output, session) {
  dataset <- reactive({
    get(input$datasetName, "package:datasets", inherits = FALSE)
  })

  output$plot <- renderPlot({
    plot(dataset())
  })

  output$table <- renderTable({
    head(dataset(), 10)
  })
}

shinyApp(ui, server)

By simply placing req(input$datasetName) where input$datasetName is needed: the reactive we get rid of those.

library(shiny)

ui <- fluidPage(
  selectInput("datasetName", "Dataset", c("", "pressure", "cars")),
  plotOutput("plot"),
  tableOutput("table")
)

server <- function(input, output, session) {
  dataset <- reactive({
    req(input$datasetName) # add req
    get(input$datasetName, "package:datasets", inherits = FALSE)
  })

  output$plot <- renderPlot({
    plot(dataset())
  })

  output$table <- renderTable({
    head(dataset(), 10)
  })
}

shinyApp(ui, server)

Upvotes: 1

RolandASc
RolandASc

Reputation: 3923

The css that you have posted is to prevent red error messages from showing up on the Shiny app itself.

To suppress warning messages from showing up in the console when someone else runs the app themselves from R/RStudio, maybe most flexible is if you use options(warn = -1). See also ?warning. Then you can just override it to options(warn = 0) when you would like to see the warnings.
In this scenario it would be advisible to make sure you set the warning level back to zero (better in fact to whatever it was previously) with options(warn = 0) whenever the app exits (see ?on.exit), else you may confuse your users.

An alternative would be to use suppressWarnings as suggested in the link of the comment, which is safer in this regard. You could still make it depend on an option such that you could override it for your own purposes.

Upvotes: 0

Related Questions