1

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)
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • “This comment did not help in this situation.” — Why not? It does solve the problem. Of course you could do the same for the `renderPlotly` function itself (beware that its arguments are non-standard evaluated, so you’ll need to do the same). – Konrad Rudolph Nov 19 '20 at 12:04
  • Would you mind showing me how that comment can be used in that app to suppress the warnings? I didn't manage to do it.. – SeGa Nov 19 '20 at 12:30
  • 1
    Actually I just tested it and it doesn’t work at all. I’m now a bit confused at which point the warnings are actually getting generated. That being said, I actually strongly recommend you *don’t* ignore these warnings, since they’re entirely legitimate — fix the code instead. That is, vary the plotting code such that only the options belonging to a given trace type are getting passed, rather than all of them. Your current code is hacky and brittle, and might break in a future version of plotly, if these warnings are promoted to errors (which they *should already be*). – Konrad Rudolph Nov 19 '20 at 12:50
  • I dont see why the code has to be fixed, it works good. Plotly should just ignore arguments that are not needed for the given type. Otherwise I would have to write individual plotly function calls for all different configurations, which doesnt seem right for me.. – SeGa Nov 19 '20 at 12:55
  • 1
    “Plotly should just ignore arguments that are not needed for the given type.” — No. It does that currently, but that’s purely by accident: in general, R does *not* usually ignore invalid function arguments. The fact that plotly does is an *oversight*, and it’s entirely plausible that the plotly authors decide to fix this at some point. — “ Otherwise I would have to write individual plotly function calls for all different configurations” — That’s not necessary. You just have to swap out the moving parts (i.e. the individual `add_trace` calls and, potentially, the layout). – Konrad Rudolph Nov 19 '20 at 12:58

1 Answers1

0

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))
rld01
  • 133
  • 5