简体   繁体   中英

How to suppress all Plotly warnings in ShinyApp

I have a plotly function inside a ShinyApp, which can plot different plots (line, boxplot, violinplots) depending on the user Input.

Since the different traces take different arguments and require different layouts, I am seeing a lot of warnings, which I want to suppress. This comment did not help in this situation.

This answer seems to work in RStudio, but not within a Shinyapp and it prints the plot to the browser and also to RStudio.

The workaround described here works, but it feels like a hack and I dont use shinyjs in this app.

Any other idea, how I can suppress only plotly messages/warnings?

library(shiny)
library(plotly)

suppressPlotlyMessage <- function(p) {
  invisible(suppressMessages(suppressWarnings(print(plotly::plotly_build(p)))))
}

plotly_function <- function(df, type, mode, boxpoints) {
  p <- plot_ly(text = "text") %>%
    add_trace(data = df,
              x = ~timestamp, y = ~value,
              name = ~names, color = ~I(color),
              type = type, mode = mode,
              notched = TRUE, jitter = 0.3, boxpoints = boxpoints) %>%
    plotly::layout(barmode = 'group',
                   boxmode = 'group', boxgroupgap=0.4, violinmode  = 'group', violingroupgap =0.4,
                   showlegend = TRUE)
  # suppressPlotlyMessage(p)
  p
}

## UI ##################
ui <- fluidPage(
  checkboxInput("boxplots", "Show Plot as Boxplots"),
  conditionalPanel("input.boxplots == true",
    checkboxInput("showall", "Show all Points"),
    checkboxInput("violins", "Show Plot as Violinplots")
  ),
  actionButton("makewarning", "Create a Warning"),
  plotlyOutput("plot")
)

## SERVER ##################
server <- function(input, output, session) {
  observeEvent(input$makewarning, {
    warning("This warning should be visible")
  })
  output$plot <- renderPlotly({
    df <- data.frame(timestamp = rep(seq.POSIXt(Sys.time(), Sys.time()-1000000, length.out = 50), 2),
                     value = sin(1:50)*runif(100, 0, 50),
                     names = c(rep("name1",50), rep("name2",50)),
                     color = c(rep("#ff8300",50), rep("#5ddd40",50)))
    
    type = "scattergl"; mode = "markers+lines"
    boxpoints <- NULL
    if (input$boxplots == TRUE) {
      df$timestamp <- as.Date(df$timestamp)
      mode = NULL
      if (input$violins == TRUE) {
        type = "violin"
      } else {
        type = "box"
      }
    }
    if (input$showall) {
      boxpoints <- "all"
    }
    
    # browser()
    # plotly_function(df, type, mode, boxpoints)
    # suppressWarnings(plotly_function(df, type, mode, boxpoints))
    suppressWarnings(print(plotly_function(df, type, mode, boxpoints)))
    # suppressPlotlyMessage(plotly_function(df, type, mode, boxpoints))
    
  })
}

shinyApp(ui, server)

The comment you said didn't work suppresses messages , whereas you have warnings . This worked for me:

p<-plot_ly(*your plot here*)
suppressWarnings(plotly_build(p))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM