我有一个闪亮的应用程序,如下所示:
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
})
})
Run Code Online (Sandbox Code Playgroud)
ui.R
:
library(shiny)
library(plotly)
library(ggplot2movies) # Needed for the 'movies' data set
shinyUI(fluidPage(
titlePanel("Movie Ratings!"),
mainPanel(
plotlyOutput("trendPlot")
)
))
Run Code Online (Sandbox Code Playgroud)
这会产生警告:
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
Run Code Online (Sandbox Code Playgroud)
我想抑制此警告,因为它不必要地使我的日志混乱(是的,我知道如何通过解决问题来实际消除此警告。但这只是出于说明目的。在我真正的闪亮应用中,没有摆脱警告)。
结束语最后plott
在renderPlotly()
中suppressWarnings()
不起作用。更改plott
为suppressWarnings(print(plott))
确实可以,但也可以在UI上下文外部打印图。能做到干净吗?
在下面的示例中,我抑制了警告(全局),后来又恢复了警告,但是在绘制完成之后,使用了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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2549 次 |
最近记录: |