Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: Support for conditional @importFrom #378

Closed
richierocks opened this issue Sep 19, 2015 · 5 comments
Closed

feature request: Support for conditional @importFrom #378

richierocks opened this issue Sep 19, 2015 · 5 comments

Comments

@richierocks
Copy link

Some base-R functions are not available under all OSes. If you want to use these functions in a cross-platform way, then you need to conditionally import the function.

Full discussion on r-package-devel.

For example, utils::win.version is only available under Windows. If your package includes a function that calls this, the NAMESPACE file should contain a conditional importFrom:

if(.Platform$OS.type == "windows") {
importFrom(utils, win.version)
}

The way I'd typically write such a function would be this:

#' An OS-specific fn
#' 
#' This  fn uses win.version
#' @return The value of win.version, or NULL.
#' @importFrom utils win.version
#' #export
f <- function()
{
  if(.Platform$OS.type == "windows")
  {
    win.version()
  }
}

AFAIK, there currently isn't a way to specify the @importFrom line to make it conditional. This would be a nice feature.

Possible syntax would be #' @importFrom utils win.version .Platform$OS.type == "windows". That is, anything after the function name up to a newline would be considered as a condition to go in an if block.

@jgrn307
Copy link

jgrn307 commented Oct 4, 2015

I'd like to +1 this. A good example is trying to:
#' @importFrom utils shortPathName
This is only available in Windows, not in other OSs. My code is clever enough to not use this function, but the CRAN checks get caught on this. Catch 22. If I have this line, the package doesn't build on non-Windows. If I don't have it, I get yelled at by the R CMD CHECK

@hadley
Copy link
Member

hadley commented Oct 4, 2015

I think it's better to just do foo::bar and avoid the conditional include. But roxygen does need a way to include literal text in the namespace.

@jgrn307
Copy link

jgrn307 commented Oct 4, 2015

Is this allowed in CRAN submissions?

@hadley
Copy link
Member

hadley commented Oct 4, 2015

Yes

@bbolker
Copy link

bbolker commented Oct 4, 2015

related to #371

@hadley hadley closed this as completed Oct 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants