Nick Matzke
2011-Feb-01 04:04 UTC
[R] how to check if a library is loaded, from a function
Hi, I've written a function which I load with a source command. The function requires a certain library, phangorn, to work. I would like the function to check if phangorn is loaded as a library before running. For some reason, just putting require(phangorn) into the sourced function doesn't do anything. This must be simple, but I can't figure it out! Any help appreciated... Cheers! Nick -- ===================================================Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Graduate Student Instructor, IB200B Principles of Phylogenetics: Ecology and Evolution http://ib.berkeley.edu/courses/ib200b/ http://phylo.wikidot.com/ Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: matzke at berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 ----------------------------------------------------- "[W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together." Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm
David Winsemius
2011-Feb-01 04:49 UTC
[R] how to check if a library is loaded, from a function
On Jan 31, 2011, at 11:04 PM, Nick Matzke wrote:> Hi, > > I've written a function which I load with a source command. The > function requires a certain library, phangorn, to work. > > I would like the function to check if phangorn is loaded as a > library before running. For some reason, just putting > require(phangorn) into the sourced function doesn't do anything.If a package is already loaded then require() will not load it again. I think it is also going to be quiet if it's called in a sourced routine, so I would ask whether you have properly tested your claim that it "doesn't do anything"? If you start with a clean session and source your routine, does the routine fail because the package does not load?> > This must be simple, but I can't figure it out! Any help > appreciated... > > -- > ===================================================> Nicholas J. Matzke > Ph.D. Candidate, Graduate Student Researcher30 line sig elided. -- David Winsemius, MD West Hartford, CT
Prof Brian Ripley
2011-Feb-01 07:07 UTC
[R] how to check if a library is loaded, from a function
On Mon, 31 Jan 2011, Nick Matzke wrote:> Hi, > > I've written a function which I load with a source command. The function > requires a certain library, phangorn, to work.Do you mean an R package or an OS library (aka DLL)? I'll assume the former, but please do use accurate terminology.> I would like the function to check if phangorn is loaded as a library before > running. For some reason, just putting require(phangorn) into the sourced > function doesn't do anything.Did you check the return value? It does do something: compute a return value. You can also check "package:phangorn" %in% search()> This must be simple, but I can't figure it out! Any help appreciated... > > Cheers! > Nick > > -- > ===================================================> Nicholas J. Matzke > Ph.D. Candidate, Graduate Student Researcher > > Huelsenbeck Lab > Center for Theoretical Evolutionary Genomics > 4151 VLSB (Valley Life Sciences Building) > Department of Integrative Biology > University of California, Berkeley > > Graduate Student Instructor, IB200B > Principles of Phylogenetics: Ecology and Evolution > http://ib.berkeley.edu/courses/ib200b/ > http://phylo.wikidot.com/ > > > Lab websites: > http://ib.berkeley.edu/people/lab_detail.php?lab=54 > http://fisher.berkeley.edu/cteg/hlab.html > Dept. personal page: > http://ib.berkeley.edu/people/students/person_detail.php?person=370 > Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html > Lab phone: 510-643-6299 > Dept. fax: 510-643-6264 > > Cell phone: 510-301-0179 > Email: matzke at berkeley.edu > > Mailing address: > Department of Integrative Biology > 3060 VLSB #3140 > Berkeley, CA 94720-3140 > > ----------------------------------------------------- > "[W]hen people thought the earth was flat, they were wrong. When people > thought the earth was spherical, they were wrong. But if you think that > thinking the earth is spherical is just as wrong as thinking the earth is > flat, then your view is wronger than both of them put together." > > Isaac Asimov (1989). "The Relativity of Wrong." The Skeptical Inquirer, > 14(1), 35-44. Fall 1989. > http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595