In an .Rd example for a package, I want to use data from another package, but avoid loading the entire
package and avoid errors/warnings if that other package is not available.

If I don't care about loading the other package, I can just do:

if (require("ElemStatLearn", quietly=TRUE)) {
    data(prostate)
 #  rest of example
}

I'd rather just be able to do something like:

if (data(prostate, package="ElemStatLearn")) {
  #  rest of example
}

but it appears that data() doesn't return anything useful (like FALSE or NULL) in case the named data set doesn't exist, or the package cannot be found. Below are some test cases in a fresh R 2.13.1 session.

Is there someway I can incorporate such a data example silently without errors or warnings if the
package doesn't exist, as is the case with require()?

> data(prostate, package="ElemStatLearn")
> dd <- data(prostate, package="ElemStatLearn")
> dd
[1] "prostate"
> dd2 <- data(xxxxx, package="ElemStatLearn")
Warning message:
In data(xxxxx, package = "ElemStatLearn") : data set 'xxxxx' not found
> dd2
[1] "xxxxx"
> dd2 <- data(xxxxx, package="ElemStatLearn", verbose=FALSE)
Warning message:
In data(xxxxx, package = "ElemStatLearn", verbose = FALSE) :
  data set 'xxxxx' not found
>
> dd3 <- data(zzzzz, package="foobar")
Error in find.package(package, lib.loc, verbose = verbose) :
  there is no package called 'foobar'
> dd3
Error: object 'dd3' not found
>

try() doesn't seem to help here:

> ddtry <- try(data(zzzzz, package="foobar"))
Error in find.package(package, lib.loc, verbose = verbose) :
  there is no package called 'foobar'
> ddtry
[1] "Error in find.package(package, lib.loc, verbose = verbose) : \n there is no package called 'foobar'\n"
attr(,"class")
[1] "try-error"
>

--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to