在闪亮的应用程序中抑制情节警告

编程入门 行业动态 更新时间:2024-10-12 01:30:20
本文介绍了在闪亮的应用程序中抑制情节警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a shiny app like the following:

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

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

I would like to suppress this warning because it's unnecessarily cluttering up my logs (yes, I know how to actually get rid of this warning by fixing the issue. But this is for illustrative purposes only. In my actual shiny app there is no getting rid of the warning).

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

Wrapping the final plott in renderPlotly() in suppressWarnings() does not work. Changing plott to suppressWarnings(print(plott)) does work but also prints the plot outside of the UI context. Can this be done cleanly?

推荐答案

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

In the example below I suppress warnings (globally), and later restore them, but after the plot is completed, using shinyjs::delay. A bit hacky, but warnings are suppressed. As an alternative, you can just do options(warn = -1) and restore the warning manually.

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)

更多推荐

在闪亮的应用程序中抑制情节警告

本文发布于:2023-07-18 14:16:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   抑制   情节

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!