42971

How to check platform in .onLoad in the R package

I am trying to check if the package is run on Windows during the loading of the package and load some additional files. For some reason this doesn't 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 it?

Answer1:

EDIT to correct a wrong previous answer

After loading, loadNamespace looks for a hook function named .onLoad and calls it. The functions loadNamespace is usually called implicitly when library is used to load a package. However you can be useful at times to call these functions directly. So the following(essentially your code) works for me under 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 also you can have the same thing using library, but you should unload the namespace before (I guess it checks if the package is already loaded and don't call all hooks):

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

don't use this , it is a wrong but accepted solution

You should initialize function parameters:

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

Recommend

  • error in installing RWeka package in R
  • mzR / Rcpp package anomaly
  • Linux - Error loading rJava in Rstudio: libjvm.so: missing?
  • JAVA_HOME cannot be determined error with XLConnect
  • How to make the target depend on lib file, but exclude it from $^ (VPATH involved)?
  • tcltk in portable R
  • Prompting for SAS ODBC connection password when running in batch mode
  • Is installed package can be move to SDCard
  • Automating table/object name scan and search in SAS
  • Consuming a web service with the Netbeans Platform
  • get all processes in parallel
  • How to override __call__ in celery on main?
  • Get the last date of each month in a list of dates in Python
  • Main Thread Checker: UI API called on a background thread iOS 11 Xcode 9 GM Seed
  • what does prefix @- mean in makefile?
  • Activation Function choice for Neural network
  • How to use Windows Media Foundation with UWP without a topology
  • How can I replace the server in Web Component Tester
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • x64 applications using gdi+: what are the consequences on performance?
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Sony Xperia Z Tablet not found by adb
  • Obtain ObjectIdHex value from mgo query
  • Javascript convert timezone issue
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Apache 2.4 - remove | delete | uninstall
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How do you join a server to an Active Directory (domain)?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • Converting MP3 duration time