passing arguments via "..."

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

passing arguments via "..."

Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
I would like to do something like the following:

Fancyhist<-function(x,...) {

# first, process x into xprocess somehow, then ...

  if (is.null(breaks)) {   # yes, I know this is wrong
# define the histogram breaks somehow, then call hist:
     hist(xprocess,breaks=breaks,...)
  } else {
# use breaks give in calling argument
     hist(xprocess,...)
  }

}

But, those of you who know R better than I do have already spotted that
"is.null(breaks)" just won't work.  Neither does "exists" or "missing".

I can think of ways to do this involving new arguments with new names --
like "histbreaks" or something -- but that seems inelegant.

Is there a way to do what I am trying to do here?

Thanks.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing arguments via "..."

Jonathan P Daily
It is not clear to me exactly what you are looking to do, but if you are
trying to check whether an argument has been passed via ..., I have used
this convention before.

fun <- function(arg, ...) {
opts <- list(...)
if('opt' %in% names(opts)) # Do something...
}

Does that help?
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

[hidden email] wrote on 03/29/2011 01:47:41 PM:

> [image removed]
>
> [R] passing arguments via "..."
>
> Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
>
> to:
>
> r-help
>
> 03/29/2011 01:51 PM
>
> Sent by:
>
> [hidden email]
>
> I would like to do something like the following:
>
> Fancyhist<-function(x,...) {
>
> # first, process x into xprocess somehow, then ...
>
>   if (is.null(breaks)) {   # yes, I know this is wrong
> # define the histogram breaks somehow, then call hist:
>      hist(xprocess,breaks=breaks,...)
>   } else {
> # use breaks give in calling argument
>      hist(xprocess,...)
>   }
>
> }
>
> But, those of you who know R better than I do have already spotted that
> "is.null(breaks)" just won't work.  Neither does "exists" or "missing".
>
> I can think of ways to do this involving new arguments with new names --
> like "histbreaks" or something -- but that seems inelegant.
>
> Is there a way to do what I am trying to do here?
>
> Thanks.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing arguments via "..."

Henrik Bengtsson-3
In reply to this post by Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
It's easier that you think;

Fancyhist <- function(x, break=NULL, ...) {
  # first, process x into xprocess somehow, then ...

  if (is.null(breaks)) {
    # define the histogram breaks somehow, then call hist:
  }

  hist(xprocess, break=breaks, ...)
}

/H

On Tue, Mar 29, 2011 at 10:47 AM, Cable, Samuel B Civ USAF AFMC
AFRL/RVBXI <[hidden email]> wrote:

> I would like to do something like the following:
>
> Fancyhist<-function(x,...) {
>
> # first, process x into xprocess somehow, then ...
>
>  if (is.null(breaks)) {   # yes, I know this is wrong
> # define the histogram breaks somehow, then call hist:
>     hist(xprocess,breaks=breaks,...)
>  } else {
> # use breaks give in calling argument
>     hist(xprocess,...)
>  }
>
> }
>
> But, those of you who know R better than I do have already spotted that
> "is.null(breaks)" just won't work.  Neither does "exists" or "missing".
>
> I can think of ways to do this involving new arguments with new names --
> like "histbreaks" or something -- but that seems inelegant.
>
> Is there a way to do what I am trying to do here?
>
> Thanks.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: passing arguments via "..."

Bert Gunter
In reply to this post by Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
Passing arguments can be tricky ( take a look at plot.default !!).
There may be a better approach then what you have considered.

hist is (S3) generic. So make a method for an S3 class that will
handle data structures like your x. Something like:

class(x ) <- c("zebra",class(x))
hist.zebra <- function(x,...)
{
########################################
## code to process x and turn it into a class for which an existing
hist method exists
## say you have turned it into y, of class "gnu" and there's an
existing hist.gnu method (use ## methods(hist) to see what already
exists). Then just :
#############################################

hist(y,...)
}

Whatever was in your original ... argument to hist.zebra will now get
passed to the ... argument of hist.gnu, which is what will be called
by the generic hist().  hist.gnu will use default argument values as
needed.

Is this what you want?

See:

AN Intro to R (have you read it?) for more on S3 methods and also

?UseMethod

-- Bert


On Tue, Mar 29, 2011 at 10:47 AM, Cable, Samuel B Civ USAF AFMC
AFRL/RVBXI <[hidden email]> wrote:

> I would like to do something like the following:
>
> Fancyhist<-function(x,...) {
>
> # first, process x into xprocess somehow, then ...
>
>  if (is.null(breaks)) {   # yes, I know this is wrong
> # define the histogram breaks somehow, then call hist:
>     hist(xprocess,breaks=breaks,...)
>  } else {
> # use breaks give in calling argument
>     hist(xprocess,...)
>  }
>
> }
>
> But, those of you who know R better than I do have already spotted that
> "is.null(breaks)" just won't work.  Neither does "exists" or "missing".
>
> I can think of ways to do this involving new arguments with new names --
> like "histbreaks" or something -- but that seems inelegant.
>
> Is there a way to do what I am trying to do here?
>
> Thanks.
>
> ______________________________________________
> [hidden email] 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.
>



--
Bert Gunter
Genentech Nonclinical Biostatistics

______________________________________________
[hidden email] 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.