[R] Does nargin and nargout work with R functions?

baptiste auguie baptiste.auguie at googlemail.com
Thu Nov 26 18:08:57 CET 2009


Hi,

I think you can use match.call() to retrieve the number of arguments
passed to a function (see below), but I don't think nargout makes
sense in R like it does in Matlab.

foo <- function(...){

  print(match.call())
  nargin <- length(as.list(match.call())) -1
  print(nargin)
}

foo(a=1, b=2)
foo()
foo(1:3, a=2, c=3)

HTH,

baptiste

2009/11/26 Jason Rupert <jasonkrupert at yahoo.com>:
> I am porting some MATLAB functions over to R and hopefully into a package, so I am curious if nargin and nargout work with R functions.
>
> Here is kind of an example of where I need to head in order to port "control-1.0.11" from Octave over to R.  The Octave "control-1.0.11" package has the capability to produce bode plots of transfer functions.  I hope to post this package once the port over to R is complete.
>
> Thanks again for all the feedback and insights.
>
> bode<-function(sys, w, outputs, inputs, plot_style)
> {
>
> # ...
>
> if (nargin < 1 || nargin > 5)
> {
>        print("This works")
> }
>
> if (nargout < 1)
> {
>       print("This also works")
> }
>
> return(list(mag_r, phase_r, w_r))
>
> }
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list