On Wednesday 28 February 2007 11:38, James Anderson wrote:
> Hi,
>
> I have a rather simple question for writing my own function. Suppose
I
> wrote a script "A" which called function B written by myself. Seems
that I
> need to write the function B within the script A. Is there anyway to
write
> B as a separate script (so that we have A and B two scripts within
the same
> directory). I tried to do this but it seems that I must write
function B
> within script A for them to work. I am very familiar with matlab,
but new
> to R. I wonder how to do this, thanks.
R will not load scripts directly (it doesn't have a concept of the
path, like
matlab). You simply need to source('B.R') before calling it in A.
See the
help for source().
Sean
James Anderson wrote:
> Hi,
>
> I have a rather simple question for writing my own function. Suppose
> I wrote a script "A" which called function B written by myself.
Seems
> that I need to write the function B within the script A. Is there
> anyway to write B as a separate script (so that we have A and B two
> scripts within the same directory). I tried to do this but it seems
> that I must write function B within script A for them to work. I am
> very familiar with matlab, but new to R. I wonder how to do this,
> thanks.
Works for me:
> a <- function(x) x^2
> b <- function(y) a(y) - y
> b(2)
[1] 2
> b(3)
[1] 6
If both functions are in your workspace (the .GlobalEnv) then b should
be able to find a. When the functions are part of a package things get
a
bit more complicated, especially if the package has a namespace.
What were you doing that didn't work?
Best,
Jim
>
> James
>
>
> --------------------------------- Be a PS3 game guru.
>
> [[alternative HTML version deleted]]
>
> _______________________________________________ Bioconductor mailing
> list Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor Search the
> archives:
> http://news.gmane.org/gmane.science.biology.informatics.conductor
--
James W. MacDonald, M.S.
Biostatistician
Affymetrix and cDNA Microarray Core
University of Michigan Cancer Center
1500 E. Medical Center Drive
7410 CCGC
Ann Arbor MI 48109
734-647-5623
**********************************************************
Electronic Mail is not secure, may not be read every day, and should
not be used for urgent or sensitive issues.
On Wednesday 28 February 2007 17:38, James Anderson wrote:
> Hi,
>
> I have a rather simple question for writing my own function. Suppose
I
> wrote a script "A" which called function B written by myself. Seems
that I
> need to write the function B within the script A. Is there anyway to
write
> B as a separate script (so that we have A and B two scripts within
the same
> directory). I tried to do this but it seems that I must write
function B
> within script A for them to work. I am very familiar with matlab,
but new
> to R. I wonder how to do this, thanks.
?source
in "A.R":
source("B.R")
where "B.R" is your script file in the same directory as "A.R".
HTH ido
> James
>
> ---------------------------------
> Be a PS3 game guru.
Please send me one.