[R] specifying arguments in functions and calling functions, within functions

K. Brand k.brand at erasmusmc.nl
Thu Sep 27 12:07:41 CEST 2012


Cheers Sarah, Rui, David,

Your effort clarifying my (several) confusions, especially with 
examples, most helpful for my understanding.

Not least the value of a fresh global environment _without_ confounding 
objects like:
 > scl
function(x) { median(x, na.rm=TRUE) }

And proper punctuation terminology :$

Thanks again,

Karl

On 26/09/12 20:53, David L Carlson wrote:
> Adding on to what Sarah has said, your function appears to limit the
> functions that can be passed to it, but it does not. The character strings
> "mean" and "median" will fail, but passing the function name directly will
> work:
>
>> Scale <- function(x, method=mean,...) {
>>     scl <- method
>>     scl(x, ...)
>> }
>
> The method=c("mean", "median") is irrelevant to the function because you
> never check to guarantee that only those strings are used in the function
> call. This is fortunate since those strings will fail whereas passing any
> function that requires a single vector as input will work just fine:
>
>> set.seed(42)
>> ex <- runif(10)
>> ex[5] <- NA
>> Scale(ex)
>   [1] NA
>> Scale(ex, na.rm=TRUE)
> [1] 0.635653
>> Scale(ex, "median")
> Error in Scale(ex, "median") : could not find function "scl"
>> Scale(ex, median)
> [1] NA
>> Scale(ex, median, na.rm=TRUE)
> [1] 0.7050648
>> Scale(ex, fivenum)
> [1] 0.1346666 0.5190959 0.7050648 0.8304476 0.9370754
>> Scale(ex, hist)
>
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Sarah Goslee
>> Sent: Wednesday, September 26, 2012 1:26 PM
>> To: K. Brand
>> Cc: r-help at r-project.org
>> Subject: Re: [R] specifying arguments in functions and calling
>> functions, within functions
>>
>> Hi,
>>
>> You have some basic confusion here, and a problem likely caused by an
>> object named scl that exists in your global environment.
>>
>> On Wed, Sep 26, 2012 at 11:56 AM, K. Brand <k.brand at erasmusmc.nl>
>> wrote:
>>> Esteemed R UseRs,
>>>
>>> Regarding specifying arguments in functions and calling functions
>>> within functions:
>>>
>>> ## beginning ##
>>> ## some data
>>> ex <- rnorm(10)
>>> ex[5] <- NA
>>>
>>> ## example function
>>> Scale <- function(x, method=c("mean", "median")) {
>>>     scl <- method
>>>     scl(x)
>>> }
>>>
>>> ## both return NA
>>> Scale(ex, method=median)
>>> median(ex, na.rm=FALSE)
>>>
>>> ## both return the median
>>> Scale(ex, method="median")
>>> median(ex, na.rm=TRUE)
>>>
>>> ## 1. Why does the use of apostrophes have this effect when calling
>>> ## a fucntion within a function?
>>
>> Those are double quotes, not apostrophes, and they don't have that
>> effect:
>>
>>> Scale(ex, method=median)
>> [1] NA
>>> Scale(ex, method="median")
>> Error in Scale(ex, method = "median") : could not find function "scl"
>>>
>>
>> You probably have something named scl in your global environment: you
>> can see that with ls().
>>
>> Take a look at:
>>
>>> class(median)
>> [1] "function"
>>> class("median")
>> [1] "character"
>>
>> In your first example, you're passing a function to Scale(), which
>> gives it a new name then uses it. In the second you're passing a
>> character string that happens to be the name of a function, which
>> Scale() gives a new name and then tries but fails to use (because it
>> isn't a function).
>>
>> See also:
>>> Scale(ex, "Nothing")
>> Error in Scale(ex, "Nothing") : could not find function "scl"
>>
>>> ## 2. What's the canonical use of apostrophes in functions like the
>> above:
>>> ## Scale <- function(x, method=c("mean", "median")) {....
>>> ## or
>>> ## Scale <- function(x, method=c(mean, median)) {....
>>
>> Depends on whether you want to pass a function or the name of a
>> function.
>>
>>> ## 3. How does one specify the arguments of a function being called
>>> ## within a function? i.e. i thought the use of '...' might work in
>>> ## the following but i was wrong.
>>>
>>> ## '...' has no apparent effect
>>> Scale <- function(x, method=c("mean", "median"),...) {
>>>     scl <- method
>>>     scl(x)
>>> }
>>
>> You can pass them explicitly as named arguments, or with ... as you
>> try. In either case you have to hand those off to the function you
>> want to use them:
>>
>> Scale <- function(x, method=c("mean", "median"),...) {
>>     scl <- method
>>      scl(x, ...)
>> }
>>
>> Scale <- function(x, method=c("mean", "median"), na.rm=FALSE) {
>>     scl <- method
>>      scl(x, na.rm=na.rm)
>> }
>>
>>> ## both return NA
>>> Scale(ex, method=median, na.rm=TRUE)
>>> Scale(ex, method=median, na.rm=FALSE)
>>> ## end ##
>>>
>>>
>>> I failed to comprehend anything google returned when trying to
>>> understand this myself. Greatly appreciate any thoughts &/or
>>> examples on this.
>>
>> Many functions that come with R are written purely in R and use these
>> capabilities so you can look at them for examples.
>>
>> Sarah
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>> ______________________________________________
>> 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.
>

-- 
Karl Brand
Dept of Cardiology and Dept of Bioinformatics
Erasmus MC
Dr Molewaterplein 50
3015 GE Rotterdam
T +31 (0)10 703 2460 |M +31 (0)642 777 268 |F +31 (0)10 704 4161



More information about the R-help mailing list