Re: [Rd] good practice for values not provided

2006-11-19 Thread Duncan Murdoch
On 11/19/2006 3:46 PM, Tamas K Papp wrote:
> Hi,
> 
> I am writing a collection of functions which I plan to share as a
> package later (when they are tested thoroughly), so I would like to do
> things "right" from the beginning...
> 
> I encountered a minor question of style.  Consider a function 
> 
> f <- function(a,b,x=NULL) {
>   ## ...
> }
> 
> if !is.null(x), f will use x to calculate the result, but if
> is.null(x), it will do something else not involving x at all (using
> any x would be meaningless here, so I can't use
> x=calcsomethingfrom(a,b)).
> 
> What's the accepted way of indicating this in R with a default for x?
> x=FALSE? x=NA? x=NULL?

I think the most common is x=NULL, but probably all of those are used in 
some package.  The advantage some default over no default and an 
is.missing(x) test, is that you can write g to call f with the same default:

g <- function(a,b,c,d,x=NULL) {
   f(a,b,x)
   # some more stuff
}

It would be harder if you wanted to signal missing x by not including it 
in the call, because you'd need a test in g.

Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] good practice for values not provided

2006-11-19 Thread Gabor Grothendieck
One possibility is:

f <- function(a, b, x) if (missing(x)) a+b else a-b-x

although that does have the disadvantage that one cannot explicitly tell
it not to use x but rather its denoted by its absence.

On 11/19/06, Tamas K Papp <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am writing a collection of functions which I plan to share as a
> package later (when they are tested thoroughly), so I would like to do
> things "right" from the beginning...
>
> I encountered a minor question of style.  Consider a function
>
> f <- function(a,b,x=NULL) {
>  ## ...
> }
>
> if !is.null(x), f will use x to calculate the result, but if
> is.null(x), it will do something else not involving x at all (using
> any x would be meaningless here, so I can't use
> x=calcsomethingfrom(a,b)).
>
> What's the accepted way of indicating this in R with a default for x?
> x=FALSE? x=NA? x=NULL?
>
> Thanks,
>
> Tamas
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] good practice for values not provided

2006-11-19 Thread Tamas K Papp
Hi,

I am writing a collection of functions which I plan to share as a
package later (when they are tested thoroughly), so I would like to do
things "right" from the beginning...

I encountered a minor question of style.  Consider a function 

f <- function(a,b,x=NULL) {
  ## ...
}

if !is.null(x), f will use x to calculate the result, but if
is.null(x), it will do something else not involving x at all (using
any x would be meaningless here, so I can't use
x=calcsomethingfrom(a,b)).

What's the accepted way of indicating this in R with a default for x?
x=FALSE? x=NA? x=NULL?

Thanks,

Tamas

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] array indexes in C

2006-11-19 Thread Tamas K Papp
Hi Ben,

Thanks for your answer.  I looked at the status of VLA on the GCC
homepage and it appears to be "broken". [1] Do you think that the code
below still works?  Or are you using a different compliler?

Thanks,

Tamas

[1] http://gcc.gnu.org/c99status.html


On Sun, Nov 19, 2006 at 09:55:17AM -0500, Benjamin Tyner wrote:
> Tamas,
> 
> You could write convenience functions, but I have used the C99 mechanism 
> for variable length arrays with no problems calling from R. One thing 
> you have to keep in mind though is that (as far as I know) the 
> dimensions must be passed before the array reference. So for example,
> 
> r <- .C("foo",
>   as.integer(ni),
>   as.integer(nj),
>   x = double(ni * nj),
>   ...)
> 
> with your function defined as
> 
> void foo(int *ni, int *nj, double x[*ni][*nj])
> {
> ...
> 
> Then in C you can access elements of x via x[3][4], for example.
> 
> Ben
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] array indexes in C

2006-11-19 Thread Benjamin Tyner
Tamas,

You could write convenience functions, but I have used the C99 mechanism 
for variable length arrays with no problems calling from R. One thing 
you have to keep in mind though is that (as far as I know) the 
dimensions must be passed before the array reference. So for example,

r <- .C("foo",
  as.integer(ni),
  as.integer(nj),
  x = double(ni * nj),
  ...)

with your function defined as

void foo(int *ni, int *nj, double x[*ni][*nj])
{
...

Then in C you can access elements of x via x[3][4], for example.

Ben

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel