Re: [R] Passing additional arguments through '...'

2009-07-17 Thread Charles C. Berry

On Fri, 17 Jul 2009, escher2079 wrote:



Fair enough. I'm not going to post all the code because it's distracting, but
the following example function is demonstrative of the problem:

exfun<-function(...){
print(x)
}

I'm aware that leaving making the only inputs additional arguments is bad
form, but this is merely an example. So here if I call exfun as follows:

exfun(x=2)

I get "Error in print(x) : object 'x' not found". I guess this makes sense
since the additional arguments aren't really meant to be used like that, but
what I would like to know is how I would access the variable passed in the
additional arguments in exfun given that I know the name (which I can access
through sys.call). I hope that was helpful :/. Thanks!



Well, I am still not convinced that you have worked through the code in lm 
and understand what it is doing.


I would use

sc <- sys.call()

and then work on sc in the same way that lm works on mf to produce the 
model.frame that it passes to model.matrix()


Note to other readers: the list(...) idiom isn't really appropriate here 
as escher's (original) request was to be able to work around partial 
matching issues when named args are used BEFORE ... in the formals of a 
function and calls are constructed that rely on position taking precedence 
over partial matches in ... .


HTH,

Chuck






Charles C. Berry wrote:


I did not follow that.

But I might guess that you do not grok what

mf <- eval(mf, parent.frame())

is doing in lm(). If so, then you _really_ need to spend some time working
that through before attempting a customized argument matching scheme.

In any case, the suggestion to "provide commented, minimal,
self-contained, reproducible code" is often helpful to folks who read this
list in showing what you intend and provides a basis for discussion - even
if your code is incomplete or doesn't do quite what you want it to do.


HTH,

Chuck



--
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24537455.html
Sent from the R help mailing list archive at Nabble.com.

__
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.



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-17 Thread escher2079

Fair enough. I'm not going to post all the code because it's distracting, but
the following example function is demonstrative of the problem:

exfun<-function(...){
 print(x)
}

I'm aware that leaving making the only inputs additional arguments is bad
form, but this is merely an example. So here if I call exfun as follows:

exfun(x=2)

I get "Error in print(x) : object 'x' not found". I guess this makes sense
since the additional arguments aren't really meant to be used like that, but
what I would like to know is how I would access the variable passed in the
additional arguments in exfun given that I know the name (which I can access
through sys.call). I hope that was helpful :/. Thanks!




Charles C. Berry wrote:
> 
> I did not follow that.
> 
> But I might guess that you do not grok what
> 
>   mf <- eval(mf, parent.frame())
> 
> is doing in lm(). If so, then you _really_ need to spend some time working 
> that through before attempting a customized argument matching scheme.
> 
> In any case, the suggestion to "provide commented, minimal, 
> self-contained, reproducible code" is often helpful to folks who read this 
> list in showing what you intend and provides a basis for discussion - even 
> if your code is incomplete or doesn't do quite what you want it to do.
> 
> 
> HTH,
> 
> Chuck
> 

-- 
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24537455.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-17 Thread Charles C. Berry

On Fri, 17 Jul 2009, escher2079 wrote:



Thanks for your suggestions, they were quite helpful. I've just about solved
the problem, but now I just need to know where the additional arguments
passed through '...' are stored (in order to make my own matching scheme I
need to be able to change the value of those to the correct values upon
entering the function as well). Thanks!


I did not follow that.

But I might guess that you do not grok what

mf <- eval(mf, parent.frame())

is doing in lm(). If so, then you _really_ need to spend some time working 
that through before attempting a customized argument matching scheme.


In any case, the suggestion to "provide commented, minimal, 
self-contained, reproducible code" is often helpful to folks who read this 
list in showing what you intend and provides a basis for discussion - even 
if your code is incomplete or doesn't do quite what you want it to do.



HTH,

Chuck






Charles C. Berry wrote:


I think you'll need to dig into sys.call() and match.call() and put
together your own matching scheme to force a function to first match by
position and then match all else by name.

If match.call() is unfamiliar to you, it is advised to read the first 10
lines of lm().

HTH,

Chuck

p.s. every argument that comes AFTER '...' in the formals must match
exactly. Perhaps this would help you.



--
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24536935.html
Sent from the R help mailing list archive at Nabble.com.

__
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.



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-17 Thread escher2079

Thanks for your suggestions, they were quite helpful. I've just about solved
the problem, but now I just need to know where the additional arguments
passed through '...' are stored (in order to make my own matching scheme I
need to be able to change the value of those to the correct values upon
entering the function as well). Thanks!



Charles C. Berry wrote:
> 
> I think you'll need to dig into sys.call() and match.call() and put 
> together your own matching scheme to force a function to first match by 
> position and then match all else by name.
> 
> If match.call() is unfamiliar to you, it is advised to read the first 10 
> lines of lm().
> 
> HTH,
> 
> Chuck
> 
> p.s. every argument that comes AFTER '...' in the formals must match 
> exactly. Perhaps this would help you.
> 

-- 
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24536935.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-17 Thread escher2079

Thanks everyone for your suggestions, they've been very helpful. I think I've
just about solved my problem, but not quite yet. As suggested, I've used
sys.call and match.call. Here is what I have so far:

fun.tester <- function(abc,def,ghi,jkl,...){
 mf <- match.call(expand.dots = FALSE)
 sc <- sys.call()
 nmf <- names(mf)[-1]
 nmf <- nmf[-length(nmf)]
 nsc <- names(sc)[-(1:(length(nmf)+1))]
 nmf[(length(nmf)+1):(length(nmf)+length(nsc))]<-nsc
 lsc <- as.list(sc)[-1]
 list(nmf,lsc)
}

So I have the variable names in the order they were put in (as long as the
additional arguments are put in last), and the values in the order in which
they were put in as well. The last thing I need to do is to assign the
values in lsc into the variables identified by the string values in nmf.
Unfortunately, I haven't been able to work this part out. Any suggestions?
-- 
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24534723.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-15 Thread Charles C. Berry

On Wed, 15 Jul 2009, escher2079 wrote:



Hi,

I know this is a simple question, but I've been having problems passing
additional arguments through '...'. It is not matching the arguments
correctly if the permanent argument of the function begins with the same
letter as the additional argument. The following example will help show what
I mean:

fun.tester <- function(abc,...){
+ print(abc)
+ }

But if I input:
fun.tester(0,a=1)

It returns the value '1' for abc. It does however, work properly if I input:
fun.tester(abc=0,a=1)



I think you'll need to dig into sys.call() and match.call() and put 
together your own matching scheme to force a function to first match by 
position and then match all else by name.


If match.call() is unfamiliar to you, it is advised to read the first 10 
lines of lm().


HTH,

Chuck

p.s. every argument that comes AFTER '...' in the formals must match 
exactly. Perhaps this would help you.



It seems like a simple problem, so I would assume I'm doing something
stupid, but I haven't been able to find a solution anywhere. Thanks!
--
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24501159.html
Sent from the R help mailing list archive at Nabble.com.

__
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.



Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
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.


Re: [R] Passing additional arguments through '...'

2009-07-15 Thread Bert Gunter
Please consult the R Language Definition for a detailed explantion, but...

In brief, the evaluator first tries to match formal arguments by name, first
exactly, then partially, before matching by position, so "a" partially
matches formal argument "abc".

e.g. contrast 
> fun.tester(0,b=1) ## "b" does not partially match "abc"
[1] 0

Bert Gunter
Genentech Nonclinical Biostatistics

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of escher2079
Sent: Wednesday, July 15, 2009 9:06 AM
To: r-help@r-project.org
Subject: [R] Passing additional arguments through '...'


Hi,

I know this is a simple question, but I've been having problems passing
additional arguments through '...'. It is not matching the arguments
correctly if the permanent argument of the function begins with the same
letter as the additional argument. The following example will help show what
I mean:

fun.tester <- function(abc,...){
+ print(abc)
+ }

But if I input:
fun.tester(0,a=1)

It returns the value '1' for abc. It does however, work properly if I input:
fun.tester(abc=0,a=1)

It seems like a simple problem, so I would assume I'm doing something
stupid, but I haven't been able to find a solution anywhere. Thanks!
-- 
View this message in context:
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501
159p24501159.html
Sent from the R help mailing list archive at Nabble.com.

__
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.

__
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.


[R] Passing additional arguments through '...'

2009-07-15 Thread escher2079

Hi,

I know this is a simple question, but I've been having problems passing
additional arguments through '...'. It is not matching the arguments
correctly if the permanent argument of the function begins with the same
letter as the additional argument. The following example will help show what
I mean:

fun.tester <- function(abc,...){
+ print(abc)
+ }

But if I input:
fun.tester(0,a=1)

It returns the value '1' for abc. It does however, work properly if I input:
fun.tester(abc=0,a=1)

It seems like a simple problem, so I would assume I'm doing something
stupid, but I haven't been able to find a solution anywhere. Thanks!
-- 
View this message in context: 
http://www.nabble.com/Passing-additional-arguments-through-%27...%27-tp24501159p24501159.html
Sent from the R help mailing list archive at Nabble.com.

__
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.