thanks, eric---I need a little more clarification. *yes, I write functions and then forget them. so I want them to be self-sufficient. I want to write functions that check all their arguments for validity.) For example,
my.fn <- function( mylist ) { stop.if.not( is.defined(mylist) ) ## ok, superfluous stop.if.not( is.defined(mylist$dataframe.in.mylist )) stop.if.not( is.defined(mylist$dataframe.in.mylist$a.component.I.need) ) ### other checks, such as whether the component I need is long enough, positive, etc. ### could be various other operations mylist$dataframe.in.mylist$a.component.I.need } so my.fn( asd ) ## R gives me an error, asd is not in existence my.fn( NULL ) ## second error: the list component 'dataframe.in.mylist' I need is not there my.fn( data.frame( some.other.component=1:4 ) ) ## second error; the list component 'dataframe.in.mylist' I need is not there my.fn( list( hello=1, silly=data.frame( x=1:4 ) ) ) ## second error: dataframe.in.mylist does not exist my.fn( list( hello=2, dataframe.in.mylist= data.frame( a.component.I.need=1:4 )) ## ok exists() works on a "stringified" variable name. how do I stringify in R? PS: btw, is it possible to weave documentation into my user function, so that I can type "?is.defined" and I get a doc page that I have written? Ala perl pod. I think I asked this before, and the answer was no. /iaw ---- Ivo Welch (ivo.we...@brown.edu, ivo.we...@gmail.com) CV Starr Professor of Economics (Finance), Brown University http://www.ivo-welch.info/ On Wed, Nov 3, 2010 at 3:40 PM, Erik Iverson <er...@ccbr.umn.edu> wrote: > >> alas, should R not come with an is.defined() function? > > ?exists > > a variable may >> >> never have been created, and this is different from a variable >> existing but holding a NULL. this can be the case in the global >> environment or in a data frame. >> >> > is.null(never.before.seen) >> Error: objected 'never.before.seen' not found >> > is.defined(never.before.seen) ## I need this, because I do not >> want an error: >> [1] FALSE > > exists("never.before.seen") #notice the quotes > [1] FALSE > >> >> your acs function doesn't really do what I want, either, because { >> d=data.frame( x=1:4); exists(acs(d$x)) } tells me FALSE . I really >> need >> >> > d <- data.frame( x=1:5, y=1:5 ) >> > is.defined(d$x) >> TRUE > > with(d, exists("x")) > >> > is.defined(d$z) >> FALSE > > with(d, exists("z")) > >> > is.defined(never.before.seen) >> FALSE > > exists("never.before.seen") > >> > is.defined(never.before.seen$anything) ## if a list does not >> exist, anything in it does not exist either >> FALSE > > This one I'm a bit confused about. If you're > programming a function, then the user either: > > 1) passes in an object, which is bound to a > local variable, and therefore exists. You can > do checks on that object to see that it conforms > to any constraints you have set. > > 2) does not pass in the object, in which case > you can test for that with ?missing. > > Is writing your own functions for others to > use what you're doing? > > --Erik > > ______________________________________________ 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.