[R] Data consistency checks in functions

Kuhn, Max Max.Kuhn at pfizer.com
Fri Jun 22 15:58:39 CEST 2007


Anup,

There are two ways to pass arguments to functions in R: as named
arguments or by position*.

Users *can* supply arguments that are inconsistent with the order that
you specify in the function definition, but only if they are used as
named arguments:

   myfun(X = someMatrix, values = aVector, theta = whatever) 

If the arguments are passed by position (as in myfun(beta, val1)), R
will assume that the first argument is theta, the second is X, etc since
that is how the function is defined.

My suggestion would be to leave these arguments without defaults and put
a lot of checks in the function (using is.matrix, is.vector and a few
that check the content of the data I those objects).


* You can also mix the two:

   foo(data, outcome, start = rep(0, 3))



Max

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Anup Nandialath
Sent: Friday, June 22, 2007 12:19 AM
To: r-help at stat.math.ethz.ch
Subject: [R] Data consistency checks in functions

Dear friends,

I'm writing a function with three arguments

myfun <- function(theta, X, values)

{
....
....
}

in this function, I'm trying to write consistency checks. In order to
compute the statistic of interest I only need theta and values. The idea
of having X in there is that, if values is not provided by the user,
then values is computed from X.

my problem is I'm trying to write consistency checks. For instance if i
say

output <- myfun(beta, val1), how do I ensure that R reads this as
passing arguments to "theta" and "values". In other words is it possible
to bypass X completely if values is provided. Also how is it possible
for R to recognize the second argument as being values and not X. This
is important because X is a matrix and values is a vector. Therefore any
checks using the dimensions of either one will land in trouble if it
does not correctly capture that. 

Thanks in advance
Sincerely

Anup

       
---------------------------------


	[[alternative HTML version deleted]]

______________________________________________
R-help at stat.math.ethz.ch 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.

----------------------------------------------------------------------
LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}



More information about the R-help mailing list