I am curious if there is any way to list only variables or functions in current environment, rather than listing all objects? Thanks. -- Daehyok Shin (Peter) Geography Department Univ. of North Carolina-Chapel Hill
On Sat, 19 Jun 2004, Shin wrote:> I am curious if there is any way to list only variables or functions in > current environment, rather than listing all objects? Thanks.Not really. What you can do is list the objects and then get the objects and look at the modes, as ls.str does. That could easily be modified to just list, but you may find what it gives helpful anyway. -- 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
These two functions will list the functions and variables respectively: ls.funs <- function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if(is.function(get(x)))x)) ls.vars <- function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if(!is.function(get(x)))x)) To use: ls.funs() ls.vars() Date: Sat, 19 Jun 2004 22:59:57 -0400 From: Shin <sdhyok at email.unc.edu> To: R Help <r-help at stat.math.ethz.ch> Subject: [R] A way to list only variables or functions? I am curious if there is any way to list only variables or functions in current environment, rather than listing all objects? Thanks. -- Daehyok Shin (Peter) Geography Department Univ. of North Carolina-Chapel Hill
Neat! Thanks. How about incorporating this support into standard commands, ls() or objects()? Daehyok Shin (Peter)> -----Original Message----- > From: Gabor Grothendieck [mailto:ggrothendieck at myway.COM] > Sent: Sunday, June 20, 2004 AM 10:06 > To: sdhyok at email.unc.edu; r-help at stat.math.ethz.ch > Subject: RE: [R] A way to list only variables or functions? > > > > > These two functions will list the functions and variables > respectively: > > ls.funs <- > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > (is.function(get(x)))x)) > > ls.vars <- > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > (!is.function(get(x)))x)) > > > To use: > > ls.funs() > ls.vars() > > > Date: Sat, 19 Jun 2004 22:59:57 -0400 > From: Shin <sdhyok at email.unc.edu> > To: R Help <r-help at stat.math.ethz.ch> > Subject: [R] A way to list only variables or functions? > > > I am curious if there is any way to list only variables or functions in > current environment, rather than listing all objects? Thanks. > > -- > Daehyok Shin (Peter) > Geography Department > Univ. of North Carolina-Chapel Hill > > > > _______________________________________________ > No banners. No pop-ups. No kidding.>
Interesting, but it seems too complex. In my opinion, just listing functions or non-functions meets my need. Anyway, thanks. Daehyok Shin (Peter)> -----Original Message----- > From: S?en Merser [mailto:merser at image.dk] > Sent: Sunday, June 20, 2004 AM 10:51 > To: Shin > Subject: Re: [R] A way to list only variables or functions? > > > hi > forgot who actualy wrote this code but it works nicely > use: > ls.objects(type='function') > > regards soren > > ls.objects<-function (pos = 1, pattern, mode = "any", type = "any"){ > Object.Name <- ls(pos = pos, envir = as.environment(pos), pattern > pattern) > Object.Mode <- rep("",length(Object.Name)) > Object.Type <- rep("",length(Object.Name)) > Variables <- rep("-",length(Object.Name)) > Observations <- rep("-",length(Object.Name)) > for (i in 1:length(Object.Name)){ > Object.Mode[[i]] <- mode(get(Object.Name[[i]])) > if(is.list(get(Object.Name[[i]]))){ > if(is.null(class(get(Object.Name[[i]])))) > Object.Type[[i]] <- c("unknown") > else { > Object.Attrib <- attributes(get(Object.Name[[i]])) > if(length(Object.Attrib$class) == 1) { > Object.Type[[i]] <- Object.Attrib$class > if(Object.Type[[i]]=="data.frame"){ > Variables[[i]] <- as.character(length(Object.Attrib$names)) > Observations[[i]] <- as.character(length(Object.Attrib$row.names)) > } > } > else { > if("data.frame" %in% Object.Attrib$class){ > Object.Type[[i]] <- "data.frame" > Variables[[i]] <- as.character(length(Object.Attrib$names)) > Observations[[i]] <- as.character(length(Object.Attrib$row.names)) > } > else { > if("aov" %in% Object.Attrib$class) Object.Type[[i]] <- "aov" > } > } > } > } > if(is.matrix(get(Object.Name[[i]]))){ > Object.Attrib <- dim(get(Object.Name[[i]])) > Object.Type[[i]] <- c("matrix") > Variables[[i]] <- as.character(Object.Attrib[2]) > Observations[[i]] <- as.character(Object.Attrib[1]) > } > if(is.vector(get(Object.Name[[i]])) && (Object.Mode[[i]]=="character" || > Object.Mode[[i]]=="numeric")){ > Object.Type[[i]] <- c("vector") > Variables[[i]] <- c("1") > Observations[[i]] <- as.character(length(get(Object.Name[[i]]))) > } > if(is.factor(get(Object.Name[[i]]))){ > Object.Type[[i]] <- c("factor") > Variables[[i]] <- c("1") > Observations[[i]] <- as.character(length(get(Object.Name[[i]]))) > } > if(is.function(get(Object.Name[[i]]))) Object.Type[[i]] <- c("function") > } > objList <- > data.frame(Object.Name,Object.Mode,Object.Type,Observations,Variables) > if(mode != "any") objList <- objList[objList[["Object.Mode"]] > == mode,] > if(type != "any") objList <- objList[objList[["Object.Type"]] > == type,] > return(objList) > } > ----- Original Message ----- > From: "Shin" <sdhyok at email.unc.edu> > To: "R Help" <r-help at stat.math.ethz.ch> > Sent: Sunday, June 20, 2004 4:59 AM > Subject: [R] A way to list only variables or functions? > > > > I am curious if there is any way to list only variables or functions in > > current environment, rather than listing all objects? Thanks. > > > > -- > > Daehyok Shin (Peter) > > Geography Department > > Univ. of North Carolina-Chapel Hill > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > >
On Sun, 20 Jun 2004, Shin, Daehyok wrote:> Neat! Thanks.Note that these are not correct, as the get is not done from the correct environment. The function ls.str I pointed you to is correct.> How about incorporating this support into standard commands, ls() or > objects()?Well, there already is ls[f].str.> Daehyok Shin (Peter) > > > -----Original Message----- > > From: Gabor Grothendieck [mailto:ggrothendieck at myway.COM] > > Sent: Sunday, June 20, 2004 AM 10:06 > > To: sdhyok at email.unc.edu; r-help at stat.math.ethz.ch > > Subject: RE: [R] A way to list only variables or functions? > > > > > > > > > > These two functions will list the functions and variables > > respectively: > > > > ls.funs <- > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > (is.function(get(x)))x)) > > > > ls.vars <- > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > (!is.function(get(x)))x)) > > > > > > To use: > > > > ls.funs() > > ls.vars() > > > > > > Date: Sat, 19 Jun 2004 22:59:57 -0400 > > From: Shin <sdhyok at email.unc.edu> > > To: R Help <r-help at stat.math.ethz.ch> > > Subject: [R] A way to list only variables or functions? > > > > > > I am curious if there is any way to list only variables or functions in > > current environment, rather than listing all objects? Thanks. > > > > -- > > Daehyok Shin (Peter) > > Geography Department > > Univ. of North Carolina-Chapel Hill > > > > > > > > _______________________________________________ > > No banners. No pop-ups. No kidding. > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- 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
Here they are again modified to remove that bug: ls.funs <- function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if(is.function(get(x,env=env)))x)) ls.var <- function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if(!is.function(get(x,env=env)))x)) Date: Sun, 20 Jun 2004 17:02:40 +0100 (BST) From: Prof Brian Ripley <ripley at stats.ox.ac.uk> To: Shin, Daehyok <sdhyok at email.unc.edu> Cc: <r-help at stat.math.ethz.ch> Subject: RE: [R] A way to list only variables or functions? On Sun, 20 Jun 2004, Shin, Daehyok wrote:> Neat! Thanks.Note that these are not correct, as the get is not done from the correct environment. The function ls.str I pointed you to is correct.> How about incorporating this support into standard commands, ls() or > objects()?Well, there already is ls[f].str.> Daehyok Shin (Peter) > > > -----Original Message----- > > From: Gabor Grothendieck [mailto:ggrothendieck at myway.COM] > > Sent: Sunday, June 20, 2004 AM 10:06 > > To: sdhyok at email.unc.edu; r-help at stat.math.ethz.ch > > Subject: RE: [R] A way to list only variables or functions? > > > > > > > > > > These two functions will list the functions and variables > > respectively: > > > > ls.funs <- > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > (is.function(get(x)))x)) > > > > ls.vars <- > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > (!is.function(get(x)))x)) > > > > > > To use: > > > > ls.funs() > > ls.vars() > > > > > > Date: Sat, 19 Jun 2004 22:59:57 -0400 > > From: Shin <sdhyok at email.unc.edu> > > To: R Help <r-help at stat.math.ethz.ch> > > Subject: [R] A way to list only variables or functions? > > > > > > I am curious if there is any way to list only variables or functions in > > current environment, rather than listing all objects? Thanks. > > > > -- > > Daehyok Shin (Peter) > > Geography Department > > Univ. of North Carolina-Chapel Hill > > > > > > > > _______________________________________________ > > No banners. No pop-ups. No kidding. > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- 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
Glad to know useful functions. How about adding lsv.str function to list only variables bound to values? In my opinion, we are more interested in values than functions in the process of data analysis. In addition, the simple solution of Grothendieck to display only names of objects has its own practical value. Daehyok Shin> -----Original Message----- > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > Sent: Sunday, June 20, 2004 PM 12:03 > To: Shin, Daehyok > Cc: r-help at stat.math.ethz.ch > Subject: RE: [R] A way to list only variables or functions? > > > On Sun, 20 Jun 2004, Shin, Daehyok wrote: > > > Neat! Thanks. > > Note that these are not correct, as the get is not done from the > correct environment. The function ls.str I pointed you to is correct. > > > How about incorporating this support into standard commands, ls() or > > objects()? > > Well, there already is ls[f].str. > > > Daehyok Shin (Peter) > > > > > -----Original Message----- > > > From: Gabor Grothendieck [mailto:ggrothendieck at myway.COM] > > > Sent: Sunday, June 20, 2004 AM 10:06 > > > To: sdhyok at email.unc.edu; r-help at stat.math.ethz.ch > > > Subject: RE: [R] A way to list only variables or functions? > > > > > > > > > > > > > > > These two functions will list the functions and variables > > > respectively: > > > > > > ls.funs <- > > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > > (is.function(get(x)))x)) > > > > > > ls.vars <- > > > function(env=sys.frame(-1))unlist(lapply(ls(env=env),function(x)if > > > (!is.function(get(x)))x)) > > > > > > > > > To use: > > > > > > ls.funs() > > > ls.vars() > > > > > > > > > Date: Sat, 19 Jun 2004 22:59:57 -0400 > > > From: Shin <sdhyok at email.unc.edu> > > > To: R Help <r-help at stat.math.ethz.ch> > > > Subject: [R] A way to list only variables or functions? > > > > > > > > > I am curious if there is any way to list only variables or > functions in > > > current environment, rather than listing all objects? Thanks. > > > > > > -- > > > Daehyok Shin (Peter) > > > Geography Department > > > Univ. of North Carolina-Chapel Hill > > > > > > > > > > > > _______________________________________________ > > > No banners. No pop-ups. No kidding. > > > > > > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html> >-- 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
> From: Duncan Murdoch > > On Mon, 21 Jun 2004 09:53:35 -0400, "Shin, Daehyok" > <sdhyok at email.unc.edu> wrote : > > >Glad to know useful functions. > >How about adding lsv.str function to list only variables > bound to values? > >In my opinion, we are more interested in values than functions in the > >process of data analysis. > > In R, functions often contain useful information about data (in their > attached environments). For example, the result of a smoothing fit > could include a function that calculates the fitted value at any > point. So the distinction between functions and values isn't as clear > as you seem to be thinking. > > However, it would be useful to get a slightly more informative version > of ls(), that returned a data.frame containing the name, length, > class, and other useful information for each object. Then if you > didn't want to see functions, you'd just select based on the class (or > mode, or some other column). > > I seem to recall that S-PLUS has such a function, but I forget the > name of it. Probably R does too, on CRAN if not in the base > packages.There are several variants of this posted to R-help and R-devel. As an example, search for ls.obj() or ls.object(). Andy> Duncan Murdoch >
>> In R, functions often contain useful information about data (in their >> attached environments). >There are several variants of this posted to R-help and R-devel. As >an >example, search for ls.obj() or ls.object().Check out ?browseEnv