[R] silently testing for data from another package for .Rd examples

Michael Friendly friendly at yorku.ca
Wed Aug 24 21:07:46 CEST 2011


On 8/24/2011 12:40 PM, Uwe Ligges wrote:
> Actually it is recommended to test for the availability of a valid 
> package with find.package(), particularly in this case where the name 
> of the package is already know.
>
> Best,
> Uwe
>
Thanks. So I guess the idiom I'm looking for is

 > length((find.package("xxxx", quiet=TRUE)))
[1] 0
 > length((find.package("lattice", quiet=TRUE)))
[1] 1
 >

However, AFAICS, find.package() was only introduced in R 2.13.x, so I'm 
a bit reluctant to use it in
a new package that need not otherwise require the latest major version.

The following (from Yihui) works for earlier versions, at least R 2.12.2

 > "xxxx" %in% .packages(all=TRUE)
[1] FALSE
 > "lattice" %in% .packages(all=TRUE)
[1] TRUE
 >
>
>
> On 24.08.2011 18:29, Yihui Xie wrote:
>> .packages(all = TRUE) will give you a list of all available packages
>> without really loading them like require().
>>
>> Regards,
>> Yihui
>> -- 
>> Yihui Xie<xieyihui at gmail.com>
>> Phone: 515-294-2465 Web: http://yihui.name
>> Department of Statistics, Iowa State University
>> 2215 Snedecor Hall, Ames, IA
>>
>>
>>
>> On Wed, Aug 24, 2011 at 9:28 AM, Michael Friendly<friendly at yorku.ca>  
>> wrote:
>>> 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 at 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.
>>>
>>
>> ______________________________________________
>> R-help at 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.


-- 
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



More information about the R-help mailing list