Skip to content

Calling Function With Arguments In a Script

6 messages · Clint Bowman, Bert Gunter, Rich Shepard +1 more

#
I'm starting to put code in multi-use functions rather than in individual
scripts and have not learned how to invoke the function from the command
line. If this information is in Norman Matloff's 'The Art of R Programming'
or Hadley Wickham's 'Advanced R' please point me to the proper place.

   Here's an example, the script 'pairwise-plots-continuous-vars.R' consists
of this function:

plotpairs <- function(x1,x2,x3,y,plotmain) {
     require(compositions)
     opar <- par(mar=c(4,4,3,1))
     NO3 <- x1
     SO4 <- x2
     pH <- x3
     pairwisePlot(cbind(NO3,SO4,pH),clr(y),add.line=T)
     title(main=plotmain)
     par(opar)
     detach('package:compositions')
     return()
}

   (I suppose the return statement is superfluous since there is no value
returned to a calling function.)

   What I want to do is call plotpairs() with appropriate arguments for each
plot as needed.

TIA,

Rich
#
?source

as in source("pairwise-plots-continuous-vars.R")

then

plotpairs(first,second,third,wise,title)

should get you going

Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600

         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274
On Thu, 7 May 2015, Rich Shepard wrote:

            
#
See e.g. Chapter 10.4 in the "Intro to R Tutorial" on the "..." argument.

The general idea is to define your function as:

myfun <- function (named_arguments,...)
{
# Some code
## now call your function as
pairwisePlot(some_named_arguments,...)
}

You would then call myfun with the ...'s replaced by name=value
argument pairs for your pairwisePlot function. e.g.

myfun(named_arguments, arg1= value1, arg2=value2, etc.)

where the arg1, arg2, etc. arguments would be arguments for
pairwisePlot() passed down to it as ... arguments.

Not hard, really, once you see how it works. (I suppose that's a
tautology, though -- probably what physicists say about General
Relativity).

Incidentally, you could even have functions as arbitrary arguments to
myfun and ... contain the argument lists for the function, something
like:

myfunc <- function(fun,...) {fun(...) }

Think of the flexibility this gives! One of the glories of functional
programming -- functions are first class objects that can be used as
arguments just like anything else.

Cheers,
Bert


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Thu, May 7, 2015 at 1:50 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
#
On Thu, 7 May 2015, Clint Bowman wrote:

            
Clint,

   I did this before converting it to a function, when I modified the
variables in the script. Did not try it as a function, should have.

Thanks,

Rich
#
On Thu, 7 May 2015, Bert Gunter wrote:

            
Bert,

   Thanks. That was going to be my next step.

Much appreciated,

Rich
#
I don't know what your work flow looks like, but I certainly do not equate "writing R with functions" to "passing parameters at the command line".  Rather, these seem quite orthogonal to me. I often have one R file of functions, and another R file where I source the first file and keep a record of various invocations of those functions as I identify which parameter values answer questions I have, and I copy those to an interactive R console session for testing. 

I would find the operating system command line an uncomfortable place to experiment with those parameter values because I often want to use R to repeatedly invoke those functions with range of values and then plot those results.

In fact, I can hardly imagine a scenario where I wanted to specify arguments when invoking my R script from the command line, since I can examine the status of the system clock, files and databases from inside R to determine what needed to be done next if I wanted to invoke a script automatically. I would only want to do that if I planned to call R from another scripting language, and I haven't needed to do that yet.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On May 7, 2015 2:50:46 PM PDT, Rich Shepard <rshepard at appl-ecosys.com> wrote: