[R-pkg-devel] Writing a portable package that imports functions only available on Windows

Duncan Murdoch murdoch.duncan at gmail.com
Sat Sep 19 13:20:03 CEST 2015


On 19/09/2015 6:07 AM, Richard Cotton wrote:
> I have a package that uses win.version from the utils package.
> 
> I've made my R code safe to use across platforms, I check that the OS is
> Windows before calling win.version.
> 
> The NAMESPACE file contains the line
> 
> importFrom(utils,win.version)
> 
> which causes an install failure under other OSes since that function
> doesn't exist.
> 
> Can I use this platform-dependent function and still have a cross-platform
> package?

I think there are two ways to do this.

You can use conditionals in the NAMESPACE file. For example, rgl has

if(tools:::.OStype() == "windows") {
importFrom(utils, getWindowsHandle)
}

This is really old code, and I'm surprised I don't get warnings about
the :::. I believe a better test would be

.Platform$OS.type == "windows"

but I just sent a new version of rgl to CRAN before noticing this, so
the fix will have to wait.

Alternatively, I believe if you use utils::win.version in the code
instead of importing it in the NAMESPACE file you won't get a warning.
(You need to condition the call on being on Windows, of course.)

> 
> Also, it seems like it would be nicer if the utils package always included
> this function and returned NA with a warning on platforms other than
> Windows.  Does that sound like a reasonable change?
> 

Platform-specific functions cause a number of problems; this is just one
of them.  It might be a good idea to do away with them completely, but
that would be pretty boring work, for little gain.

Duncan Murdoch



More information about the R-package-devel mailing list