47

I'm trying to run a r script from the command line, but I get warning messages when packages are loaded:

C:\Temp>Rscript myscript.r param
Warning message:
package 'RODBC' was built under R version 3.0.1
Warning message:
package 'ggplot2' was built under R version 3.0.1
Warning message:
package 'reshape2' was built under R version 3.0.1
Warning message:
package 'lubridate' was built under R version 3.0.1
Warning message:
package 'scales' was built under R version 3.0.1

I' tried to use suppressPackageStartupMessages:

suppressPackageStartupMessages(library(RODBC))

or supressMessages

suppressMessages(library(RODBC))

but these did not suppress these messages. How to get rid of these warnings?

jrara
  • 16,239
  • 33
  • 89
  • 120
  • 3
    If you want to get rid of messages the best solution would be to reinstall those packages. They won't work under R >= 3.0 anyway. – zero323 Sep 21 '13 at 09:27
  • 1
    I could not try but `suppressWarnings(library(RODBC))` should work. But I suggest to follow @zero323's advice. – sgibb Sep 21 '13 at 09:39
  • I think you need to update R. Apparently you use a version < 3.0.1. – Roland Sep 21 '13 at 10:22
  • Yes, I use version 3.0.0. But anyways, how can I suppress these messages, when upgrading is currently not possible? – jrara Sep 21 '13 at 10:30

3 Answers3

70

These are not messages but warnings. You can do:

suppressWarnings(library(RODBC))

or

suppressWarnings(suppressMessages(library(RODBC)))

to suppress both types.

Ben
  • 41,615
  • 18
  • 132
  • 227
flodel
  • 87,577
  • 21
  • 185
  • 223
33

I put this at the top of all my scripts and preface my library loads with it:

shhh <- suppressPackageStartupMessages # It's a library, so shhh!

Then you can load the library thusly:

shhh(library(tidyverse))

and rely on perfect silence.

Greenleaf
  • 535
  • 4
  • 12
1

I use a vectorized version of that:

library <- function (...) {
   packages <- as.character(match.call(expand.dots = FALSE)[[2]])
   suppressWarnings(suppressMessages(lapply(packages, base::library, character.only = TRUE)))
   return(invisible())
}

Then you just call library as usual:

library(tidyverse, ggplot2)
bm10fm
  • 21
  • 4