How to test platform in .onLoad in package R

I am trying to check if a package is running on Windows while downloading a package and downloading some additional files. For some reason this does not work (added to my zzz.R):

.onLoad <- function(libname, pkgname){ if(.Platform$OS.type == "windows") { # do specific task here } } 

How to make it work, is there a better way to do this?

+2
r
source share
1 answer

EDIT to correct incorrect previous answer

After loading, loadNamespace looks for a hook function named .onLoad and calls it. The loadNamespace functions loadNamespace usually called implicitly when the library used to load the package. However, sometimes you can be useful for calling these functions directly. So the following (essentially your code) works for me under the Windows platform.

 .onLoad <- function(libname, pkgname ) { if(.Platform$OS.type == "windows") print("windows") else print("others") } 

Then you test it using:

 loadNamespace("loadpkg") [1] "windows" <environment: namespace:loadpkg> 

Note that you can use the same thing with the library , but before you do this, you must unload namespace (I think it checks to see if the package is loaded and does not call all interceptors):

 unloadNamespace("loadpkg") library("loadpkg") [1] "windows" 

do not use it, it is a wrong decision

You must initialize the function parameters:

 .onLoad <- function(libname = find.package(pkg_name), pkgname = pkg_name){ if(.Platform$OS.type == "windows") { # do specific task here } } 
+1
source share

All Articles