2016-08-18 151 views
1

我有一个闪亮的应用程序如下所示:抑制闪亮的应用plotly警告

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 

我想压制这个警告,因为它不必要地混淆了我的日志(是的,我知道如何通过修复问题实际摆脱此警告。但这仅用于说明目的。在我实际的闪亮应用程序中没有摆脱警告)。

包装最后plottrenderPlotly()suppressWarnings()不起作用。改变plottsuppressWarnings(print(plott))确实工作,但也打印UI情境以外的情节。这可以干净地完成吗?

回答

1

在下面的示例中,我禁止警告(全局),稍后恢复它们,但在绘图完成后使用shinyjs :: delay。有点哈克,但警告被压制。 作为一种替代方法,您可以只执行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)