Hi, Appologies for the simple nature of this question, I am unable to find the answer in manuals (EG and introduciton to R). I have written a function in a text editor and saved it with an .R extension. It is saved in my working directory. How can I run it, do I need to use source? If so, how do I supply the arguments to the function? Or does it need to be saved in a particular directory? Do I need a different file extension? Many thanks for any help. Robin Williams Met Office summer intern - Health Forecasting robin.williams@metoffice.gov.uk [[alternative HTML version deleted]]
You can 'source' it in to define it and then call it as usual. No particular directory since you have control of that with the 'source' function. File extension is by convention and anyone will work since source is just going to read in an text file and execute it. On Mon, Sep 8, 2008 at 10:46 AM, Williams, Robin <robin.williams at metoffice.gov.uk> wrote:> Hi, > Appologies for the simple nature of this question, I am unable to find > the answer in manuals (EG and introduciton to R). > I have written a function in a text editor and saved it with an .R > extension. It is saved in my working directory. How can I run it, do I > need to use source? If so, how do I supply the arguments to the > function? Or does it need to be saved in a particular directory? > Do I need a different file extension? > Many thanks for any help. > > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Monday, 08 September 2008, 15:46 (UTC+0100), Williams, Robin wrote:> I have written a function in a text editor and saved it with an .R > extension. It is saved in my working directory. How can I run it, do I > need to use source?Just to add to Jim's reply: if you want to have this function available every time you use R, you could put it into your "Rprofile" file. I'm using Linux, and for me this is just a text file saved in my home directory "~/.Rprofile" (note the dot). Everything in there gets read when I start R. Not sure about other OSes. Maybe google "Rprofile". HTH m. -- Marianne Promberger Graduate student in Psychology http://www.psych.upenn.edu/~mpromber
Hi Robin, source("filename.R") will open filename.R from the working directory and behave as if you had typed in its contents, line by line. You can include a full path if you like, or use file.choose() for a file not in your working directory. --Adam On Mon, 8 Sep 2008, Williams, Robin wrote:> Hi, > Appologies for the simple nature of this question, I am unable to find > the answer in manuals (EG and introduciton to R). > I have written a function in a text editor and saved it with an .R > extension. It is saved in my working directory. How can I run it, do I > need to use source? If so, how do I supply the arguments to the > function? Or does it need to be saved in a particular directory? > Do I need a different file extension? > Many thanks for any help. > > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >