0

I want to suppress a loading message much like in this post, except that I am not loading the whole package but calling a function directly.

For example, calling raster functions as follows:

f <- system.file("external/test.grd", package="raster")
r <- raster::raster(f)
p <- raster::rasterToPolygons(r, dissolve = TRUE)

Results in the message:

Loading required namespace: rgeos

How can I prevent this message from appearing? The only solution I have found so far is to load rgeos as follows:

suppressPackageStartupMessages(library(rgeos))

But as I'm using the function raster::rasterToPolygons from within a package I don't really want to load rgeos in full.

rasenior
  • 81
  • 11
  • 1
    Have you tried `p <- suppressWarnings(raster::rasterToPolygons(r, dissolve = TRUE))`? – Dale Kube Nov 26 '18 at 18:49
  • If that function requires the package, then the entire package is getting loaded. Are you writing your own package? Did you try listing `rgeos` as a dependency? – MrFlick Nov 26 '18 at 19:36
  • Yes I had listed `rgeos` as a dependency of the package and tried `suppressMessages` but didn't seem to help. Importing `rgeos` in full in the NAMESPACE does the trick though. Seems excessive for the use of one function, but I guess it'll do! – rasenior Nov 27 '18 at 09:16
  • Sorry, I'm being dim: `suppressWarnings(raster::rasterToPolygons(r, dissolve = TRUE))` doesn't work (it's not a warning message), but `suppressMessages(raster::rasterToPolygons(r, dissolve = TRUE))` *does* work (without importing `rgeos` into the NAMESPACE). Could've sworn I tried that already! Oh well. Will add as answer. – rasenior Nov 27 '18 at 09:27

1 Answers1

1

Thought I'd tried this before but apparently not. Both of these options work:

p <- suppressMessages(raster::rasterToPolygons(r, dissolve = TRUE))
p <- suppressPackageStartupMessages(raster::rasterToPolygons(r, dissolve = TRUE))

I call the function explicitly using :: (advised by Hadley here), but you can also avoid the rgeos loading message by importing it in the NAMESPACE of your package. If using roxygen2, that means adding @import rgeos as an roxygen2 comment at the top of your function. I imagine @importFrom rgeos fun would work too, but I don't know which rgeos functions are being used by raster::rasterToPolygons.

rasenior
  • 81
  • 11