[英]How to suppress all Plotly warnings in ShinyApp
我在 ShinyApp 中有一个 plotly function,它可以根据用户输入绘制 plot 不同的图(线图、箱线图、小提琴图)。
由于不同的跟踪采用不同的 arguments 并且需要不同的布局,我看到了很多警告,我想抑制它们。 此评论在这种情况下没有帮助。
这个答案似乎在 RStudio 中有效,但在 Shinyapp 中无效,它会将 plot 打印到浏览器以及 RStudio。
此处描述的解决方法有效,但感觉就像一个 hack,我不在此应用程序中使用shinyjs
。
任何其他想法,我如何只能抑制 plotly 消息/警告?
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)
您所说的评论无效会抑制消息,而您会收到警告。 这对我有用:
p<-plot_ly(*your plot here*)
suppressWarnings(plotly_build(p))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.