[R] Help with functions
Liaw, Andy
andy_liaw at merck.com
Wed Feb 1 22:57:37 CET 2006
Just remember that you are writing _functions_, not _macros_. If you want
the function to be able to see an object, pass that object to the function
as one of the arguments. Alternatively, you can use the lexical scoping in
R and define the (sub)functions inside the `main' function.
Andy
From: Pryseley Assam
>
> Dear R-users
>
> I intend to create a function which calls some smaller
> other functions in return. Some of these smaller functions
> all call some functions. I do not know a good way to do this.
> I tried using the source() function to include the smaller
> functions within the main functions before they are called.
> This does not work, or maybe i am not doing the right thing.
> For example:
>
> the following quantities are computed in the main function
> (only a part of the main function is shown below)
>
> ###############################################################
> # quantities for bootstrap confidence intervals
> ###############################################################
> r.value<- cor(x,y)
> npb.B <- mean(r)-r.value
> npb.mean <- mean(r)
> npb.var <- var(r)
> r.star <- sort(r)
> z.star <- (r.star - mean(r.star))/sqrt(var(r.star))
>
>
> after which the main function calls the function
> (i.s.conf.int) shown below, which is saved in a different script file
>
> ###################################################
> # Confidence Interval
> ###################################################
> i.s.conf.int <- function(type) {
> switch(type,
> a = nci(),
> b = inci(),
> c = bbi(),
> d = pi(),
> e = si())
> }
>
> Also, this 'sub' function (i.s.conf.int) may call one of 5
> other smaller functions saved in yet another file, or the
> same file as the function "i.s.conf.int".
>
> However, these 5 functions need the quantities calculated
> in the main function. One of these functions is shown below:
>
> ###############################################################
> # nonparametric bootstrap normal confidence interval
> ###############################################################
> nci <-function () {
> npb.n.L <- r.value - 1.96*sqrt(npb.var)
> npb.n.U <- r.value + 1.96*sqrt(npb.var)
> npb.n <- array(data = c( npb.n.L, npb.n.U),
> dim = c(1,2),
> dimnames = list("Estimates" ,c("LowerBound","UpperBound")))
>
> npb.n.CI <- list( Type = "Normal Confidence Interval",
> ConfidenceInterval= npb.n)
> npb.n.CI
> }
>
> I use the source() function to include these
> "secondary/sub" functions, just before they are called the
> main function. I get an Error message when any of the 5 small
> functions is executing : that it can not find the quantities
> calculated in the main function.
>
> So my question boils down to how can i call functions from
> different files in the main function, so that the recognise
> quantities already calculated in the main function or passed
> as arguements to the main function ?
>
> Thanks
> Pryseley.
>
>
>
>
> ---------------------------------
> Bring words and photos together (easily) with
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list