【问题标题】:suppress plotly warnings in shiny app在闪亮的应用程序中抑制情节警告
【发布时间】:2016-12-25 10:39:49
【问题描述】:

我有一个闪亮的应用程序,如下所示:

server.R:

shinyServer(function(input, output) {

  output$trendPlot <- renderPlotly({
    plot_ly(movies, x = length, y=rating, mode='markers', color=as.factor(year), colors = c("#132B43", "#56B1F7")) -> plott

    plott
  })
})

ui.R:

library(shiny)
library(plotly)
library(ggplot2movies)  # Needed for the 'movies' data set

shinyUI(fluidPage(
  titlePanel("Movie Ratings!"),
  mainPanel(
    plotlyOutput("trendPlot")
  )
))

这会产生一个警告:

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

我想禁止显示此警告,因为它不必要地弄乱了我的日志(是的,我知道如何通过解决问题来真正消除此警告。但这仅用于说明目的。在我实际闪亮的应用程序中有没有摆脱警告)。

将最终的plott 包装在renderPlotly() 中的suppressWarnings() 不起作用。将 plott 更改为 suppressWarnings(print(plott)) 确实 工作,但也会在 UI 上下文之外打印绘图。这可以干净地完成吗?

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    在下面的示例中,我(全局)抑制警告,然后恢复它们,但在情节完成后,使用 shinyjs::delay。有点hacky,但警告被抑制了。 作为替代方案,您可以只执行 options(warn = -1) 并手动恢复警告。

    library(shiny)
    library(plotly)
    library(shinyjs)
    library(ggplot2movies)  # Needed for the 'movies' data set
    
    ui <- shinyUI(fluidPage(
      useShinyjs(),
      titlePanel("Movie Ratings!"),
      mainPanel(
        plotlyOutput("trendPlot")
      )
    ))
    
    server <- shinyServer(function(input, output) {
    
      # suppress warnings  
      storeWarn<- getOption("warn")
      options(warn = -1) 
    
      output$trendPlot <- renderPlotly({
    
        plot_ly(movies, x = length, y=rating, mode='markers', color=as.factor(year), colors = c("#132B43", "#56B1F7")) -> plott
    
        #restore warnings, delayed so plot is completed
        shinyjs::delay(expr =({ 
          options(warn = storeWarn) 
        }) ,ms = 100) 
    
        plott
      })
    })
    
    shinyApp(ui, server) 
    

    【讨论】:

    • 请注意,如果您只是在您的应用程序中执行options(warn = -1),它只会影响该应用程序会话,因此如果您在您的机器上开发并运行应用程序,您实际上不需要在您之后恢复它们关闭应用程序。您可以查看getOptions('warn')进行确认。
    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2021-12-18
    • 2013-07-08
    • 2018-12-29
    • 2014-12-05
    相关资源
    最近更新 更多