Is there a way to test if a library has been loaded? is.loaded does not give me what I want, I am looking to say: if loaded(Rdbi) dbSendQuery(conn, q) if loaded(RODBC) sqlQuery(conn, q) I need this to support both unix and windows platforms as I could not find a windows distribution for RdbiPgSQL. I am using R 2.1.0 and postgresql. I will be connecting to the database using Rdbi and RdbiPgSQL and have other developers using windows connect with RODBC, unless someone can suggest a better solution.
is library loaded
7 messages · Omar Lakkis, Duncan Murdoch, Robert Gentleman +3 more
On 9/13/2005 3:09 PM, Omar Lakkis wrote:
Is there a way to test if a library has been loaded? is.loaded does not give me what I want, I am looking to say: if loaded(Rdbi) dbSendQuery(conn, q) if loaded(RODBC) sqlQuery(conn, q) I need this to support both unix and windows platforms as I could not find a windows distribution for RdbiPgSQL. I am using R 2.1.0 and postgresql. I will be connecting to the database using Rdbi and RdbiPgSQL and have other developers using windows connect with RODBC, unless someone can suggest a better solution.
You can use the strangely named "require" function, as in
if (require(Rdbi)) dbSendQuery(conn, q)
else if (require(RODBC) sqlQuery(conn, q)
else stop("Nothing works")
Duncan Murdoch
Dear Omar,
The following function tests whether a package is in the search path (with
the package name given in quotes):
packageLoaded <- function(name) 0 != length(grep(paste("^package:", name,
"$", sep=""), search()))
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Omar Lakkis Sent: Tuesday, September 13, 2005 2:09 PM To: r-help at stat.math.ethz.ch Subject: [R] is library loaded Is there a way to test if a library has been loaded? is.loaded does not give me what I want, I am looking to say: if loaded(Rdbi) dbSendQuery(conn, q) if loaded(RODBC) sqlQuery(conn, q) I need this to support both unix and windows platforms as I could not find a windows distribution for RdbiPgSQL. I am using R 2.1.0 and postgresql. I will be connecting to the database using Rdbi and RdbiPgSQL and have other developers using windows connect with RODBC, unless someone can suggest a better solution.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi,
Almost surely this is a bad name. With the advent of name spaces it is
important to distinguish between loading and attaching. This function
tests for attached packages. To test for loaded packages we already have
loadedNamespaces.
Best wishes,
Robert
John Fox wrote:
Dear Omar,
The following function tests whether a package is in the search path (with
the package name given in quotes):
packageLoaded <- function(name) 0 != length(grep(paste("^package:", name,
"$", sep=""), search()))
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Omar Lakkis Sent: Tuesday, September 13, 2005 2:09 PM To: r-help at stat.math.ethz.ch Subject: [R] is library loaded Is there a way to test if a library has been loaded? is.loaded does not give me what I want, I am looking to say: if loaded(Rdbi) dbSendQuery(conn, q) if loaded(RODBC) sqlQuery(conn, q) I need this to support both unix and windows platforms as I could not find a windows distribution for RdbiPgSQL. I am using R 2.1.0 and postgresql. I will be connecting to the database using Rdbi and RdbiPgSQL and have other developers using windows connect with RODBC, unless someone can suggest a better solution.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Robert Gentleman, PhD Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M2-B876 PO Box 19024 Seattle, Washington 98109-1024 206-667-7700 rgentlem at fhcrc.org
Dear Robert, packageLoaded() may well be a bad name but loadedNamespaces() won't detect a package without a namespace. It therefore seemed safe to me to check the path, which would include both packages with and without namespaces. With respect to loading and attaching, I thought that library() both loaded a package (with or without a namespace) and attached it to the search path, but I must admit that I'm easily confused about these distinctions. Regards, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------
-----Original Message-----
From: Robert Gentleman [mailto:rgentlem at fhcrc.org]
Sent: Tuesday, September 13, 2005 3:53 PM
To: John Fox
Cc: uofiowa at gmail.com; r-help at stat.math.ethz.ch
Subject: Re: [R] is library loaded
Hi,
Almost surely this is a bad name. With the advent of name
spaces it is important to distinguish between loading and
attaching. This function tests for attached packages. To test
for loaded packages we already have
loadedNamespaces.
Best wishes,
Robert
John Fox wrote:
Dear Omar, The following function tests whether a package is in the
search path
(with the package name given in quotes):
packageLoaded <- function(name) 0 != length(grep(paste("^package:",
name, "$", sep=""), search()))
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Omar Lakkis Sent: Tuesday, September 13, 2005 2:09 PM To: r-help at stat.math.ethz.ch Subject: [R] is library loaded Is there a way to test if a library has been loaded? is.loaded does not give me what I want, I am looking to say: if loaded(Rdbi) dbSendQuery(conn, q) if loaded(RODBC) sqlQuery(conn, q) I need this to support both unix and windows platforms as I
could not
find a windows distribution for RdbiPgSQL. I am using R 2.1.0 and postgresql. I will be connecting to the database using Rdbi and RdbiPgSQL and have other developers using windows connect
with RODBC,
unless someone can suggest a better solution.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
-- Robert Gentleman, PhD Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M2-B876 PO Box 19024 Seattle, Washington 98109-1024 206-667-7700 rgentlem at fhcrc.org
On 13 Sep 2005, jfox at mcmaster.ca wrote:
packageLoaded() may well be a bad name but loadedNamespaces() won't detect a package without a namespace.
Right, that's a problem.
It therefore seemed safe to me to check the path, which would include both packages with and without namespaces. With respect to loading and attaching, I thought that library() both loaded a package (with or without a namespace) and attached it to the search path, but I must admit that I'm easily confused about these distinctions.
As I understand it, library(foo) will load and attach package "foo". If foo has a namespace, some of foo's dependencies may get loaded but not attached. This is only possible if said dependencies also use namespaces. So it is possible for a package to be loaded and not attached. In this case, the loaded package is not visible via search(), but is visible via loadedNamespaces() since only packages with namespaces can be loaded and not attached. Clear as mud? HTH, + seth
"Seth" == Seth Falcon <sfalcon at fhcrc.org>
on Tue, 13 Sep 2005 21:28:48 -0700 writes:
Seth> On 13 Sep 2005, jfox at mcmaster.ca wrote:
>> packageLoaded() may well be a bad name but loadedNamespaces() won't
>> detect a package without a namespace.
Seth> Right, that's a problem.
>> It therefore seemed safe to me to check the path, which would
>> include both packages with and without namespaces. With respect to
>> loading and attaching, I thought that library() both loaded a
>> package (with or without a namespace) and attached it to the search
>> path,
that's correct. But still your proposed function isn't doing
what its name suggests; so its name is really very misleading
or "bad" as Robert said.
OTOH, the name could be quite good if it's implementation
changed:
packageLoaded <- function(name)
{
## Purpose: is package 'name' loaded?
## --------------------------------------------------
(paste("package:", name, sep="") %in% search()) ||
(name %in% loadedNamespaces())
}
>> but I must admit that I'm easily confused about these distinctions.
Seth> As I understand it, library(foo) will load and attach package "foo".
correct
Seth> If foo has a namespace, some of foo's dependencies may get loaded but
Seth> not attached. This is only possible if said dependencies also use
Seth> namespaces.
Seth> So it is possible for a package to be loaded and not attached.
Yes. There's another maybe even more common case of package
loading without attaching:
e.g. using MASS::rlm(...) anywhere in your code silently
loads the MASS package but doesn't attach it.
Seth> In this case, the loaded package is not visible via search(), but is
Seth> visible via loadedNamespaces() since only packages with namespaces can
Seth> be loaded and not attached.
Indeed.
Further note that "package loading" is more than just loading the
exported R symbols from the namespace. E.g., it also dyn.load()s
the ./src/ stuff [ such that in the example, MASS::rlm() can
work at all ].
Seth> Clear as mud?
Seth> HTH,
Seth> + seth
Martin