[R] Working with additional input for user defined function
Eik Vettorazzi
E.Vettorazzi at uke.uni-hamburg.de
Tue Feb 1 17:49:14 CET 2011
almost there
fn1 <- function(x, y, ...) {
x1 <- x+y
if(is.null(names(list(...))))
res <- x1
else {
res <- list(...)
#further operations on res$aa res$zz, e.g.
#res<-res$zz+res$aa
#or
#for (a in res) {
#do something
#}
}
res
}
fn1(-2,2)
fn1(-2,2, zz=3:5,aa=6:8)
see the R-lang documentation, chapter 2.1.9.
I don't know, if you are looking for specific named arguments (as 'zz'
or 'aa') and neither what you want to do with the additional arguments.
Am 01.02.2011 17:28, schrieb Bogaso Christofer:
> Thanks Eik for your help. So far I am able to do following things:
>
>> fn1 = function(x, y, ...) {
> + x1 <- x+y
> + if(is.null(names(list(...))))
> + res <- x1
> + else
> + res <- get(names(list(...)))
> + return(res)
> +
> + } ;
>> fn1(-2,2)
> [1] 0
>> fn1(-2,2, zz=3:5)
> Error in get(names(list(...))) : object 'zz' not found
>
> As you see, still I am far from the solution. How to pass the value of "..."
> for further calculation?
>
> Thanks,
>
> -----Original Message-----
> From: Eik Vettorazzi [mailto:E.Vettorazzi at uke.uni-hamburg.de]
> Sent: 01 February 2011 19:08
> To: Bogaso Christofer
> Cc: r-help at r-project.org
> Subject: Re: [R] Working with additional input for user defined function
>
> Hi Christofer,
> just have a look at how other functions deal with this, e.g.
>> plot
>
> function (x, y, ...)
> {
> #*snip*
> hasylab <- function(...) !all(is.na(pmatch(names(list(...)),
> "ylab")))
> #*snip*
>
> got it?
> cheers
>
> Am 01.02.2011 11:29, schrieb Bogaso Christofer:
>> Hi all, suppose I have following user defined function:
>>
>>
>>
>> fn1 = function(x, y, ...) {
>>
>> z <- x+y; u=y^2
>>
>> # if something with name "add" exists
>> in the function argument then do some calculation and return that
>> calculated value, NOT z
>>
>> # if NOT then return z
>>
>> return(z)
>>
>> }
>>
>>
>>
>> As you see in the function definition, I put open space " '.' syntax "
>> , so that user can put additional argument. However here what I want
>> to do is, if user have additional input then above function would do
>> some ***special
>> calculation*** and return that value.
>>
>>
>>
>> How can I accomplice that?
>>
>>
>>
>> Thanks and regards,
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
>
> --
> Eik Vettorazzi
> Institut für Medizinische Biometrie und Epidemiologie Universitätsklinikum
> Hamburg-Eppendorf
>
> Martinistr. 52
> 20246 Hamburg
>
> T ++49/40/7410-58243
> F ++49/40/7410-57790
>
--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
More information about the R-help
mailing list