How to use suppressMessages() inside map()

311 Views Asked by At

I made a function my_fun() which generates messages with the message() function. And also uses pb$tick() to track progress with progress::progress_bar. I am running this function inside a purrr::map() call to iterate over a long list like: map(my_list, my_fun). So the message is displayed a over and over. I would like to suppress the message like map(my_list, suppressMessages(my_fun)) but it has no effect.

The call suppressMessages(map(my_list, my_fun)) works but that suppresses the progress_bar as well.

Is there a convenient way to remove the messages without removing the message() calls from my_fun()?

1

There are 1 best solutions below

0
AnilGoyal On

Though you have not included any reproducible example for your use case, yet in my opinion, the following code using anonymous function style of purrr should work.

map(my_list, ~ suppressMessages(my_fun(.x)))

Do not forget to use twiddle ~ in above.