Как мне подавить предупреждения с помощью plotly?

#r #plotly

#r #plotly

Вопрос:

Я хотел бы остановить предупреждения в plotly, не отключая предупреждения глобально

Этот код переходит в функцию, поэтому его можно запустить в другом сеансе

Это мой код

       fit <-
        plot_ly(credit_old, 
            y = ~score, 
            color = ~fte, 
            type = "box")
      suppressWarnings(fit)   
  

Я все еще получаю

 Warning messages:
1: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels
  

Ответ №1:

suppressWarnings(fit) возвращает значение, которое fit . Затем этот результат автоматически печатается, поэтому отображается предупреждение. Это похоже на это

 result <- suppressWarnings(fit) # No warning here
print(result) # Warning here
#> Warning messages:
#> 1: In min(x, na.rm = na.rm) :
#>   no non-missing arguments to min; returning Inf
#> 2: In max(x, na.rm = na.rm) :
#>   no non-missing arguments to max; returning -Inf
  

Вы можете вызвать print inside suppressWarnings . Это не приводит к автоматической печати, поскольку print возвращает значение невидимым.

 suppressWarnings(print(fit))