[R] programming questions
Henrik Bengtsson
hb at biostat.ucsf.edu
Wed Nov 3 21:19:17 CET 2010
On Wed, Nov 3, 2010 at 1:04 PM, ivo welch <ivo.welch at gmail.com> wrote:
> 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
> }
See the Arguments class in R.utils, e.g.
library("R.utils");
my.fn <- function(mylist) {
# Assert a data.frame element exists
df <- Arguments$getInstanceOf(mylist$dataframe.in.mylist, "data.frame");
# Assert x >= 0 and of length 45:67.
x <- df$a.component.I.need;
x <- Arguments$getDoubles(x, range=c(0,Inf), length=c(45,67));
### could be various other operations
mylist$dataframe.in.mylist$a.component.I.need
}
/Henrik
>
> 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.welch at brown.edu, ivo.welch at 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 <eriki at 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 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.
>
More information about the R-help
mailing list