Hi, I have 15 or 20 functions I've written to convert the sort of data I'm working with. They are currently in their own R file which I load by hand in Rgui before loading and running my main programs. Is there any way to have this file included in my R program like #include might in C? If not is there a simple newbie-type example of how to create a package so I could just say something like require(MWKFunctions) and be done with this? Thanks, Mark
Several solutions, easiest might be to use the source function in your program. Also, you could create a .Rprofile if you use them every time you use R. See ?source and ?Startup Erik -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Knecht Sent: Friday, July 24, 2009 11:25 AM To: r-help Subject: [R] Include files? Hi, I have 15 or 20 functions I've written to convert the sort of data I'm working with. They are currently in their own R file which I load by hand in Rgui before loading and running my main programs. Is there any way to have this file included in my R program like #include might in C? If not is there a simple newbie-type example of how to create a package so I could just say something like require(MWKFunctions) and be done with this? Thanks, Mark ______________________________________________ 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.
?Startup ?.Rprofile On Fri, Jul 24, 2009 at 12:25 PM, Mark Knecht<markknecht at gmail.com> wrote:> Hi, > ? I have 15 or 20 functions I've written to convert the sort of data > I'm working with. They are currently in their own R file which I load > by hand in Rgui before loading and running my main programs. > > ? Is there any way to have this file included in my R program like > #include might in C? > > ? If not is there a simple newbie-type example of how to create a > package so I could just say something like require(MWKFunctions) and > be done with this? > > Thanks, > Mark > > ______________________________________________ > 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?
Mark Knecht wrote:> > Hi, > I have 15 or 20 functions I've written to convert the sort of data > I'm working with. They are currently in their own R file which I load > by hand in Rgui before loading and running my main programs. > > Is there any way to have this file included in my R program like > #include might in C? > > If not is there a simple newbie-type example of how to create a > package so I could just say something like require(MWKFunctions) and > be done with this? > > Thanks, > Mark >If you are running a Unix based machine, rolling your own package is a snap. Just load the R file containing your functions: source('myFunctions.R') Then use the package.skeleton to create an R package from the functions you just loaded: package.skeletion('myPackage') package.skeleton will place just about every R object you currently have in your environment into your package- this is a great way to save datasets along with the functions you use to manipulate them. Now comes the part that can be difficult on Windows. package.skeleton created a directory called 'myPackage' that needs to be packed up into an archive and then installed. This can be done from within R by the following two commands (it may also be done directly from a command line, just execute the commands inside the quotes ' ' ): system( 'R CMD build myPackage' ) system( 'R CMD INSTALL myPackage' ) After that you can load up all your functions using: library(myPackage) After your package is loaded, if you had a data.frame or other variable named "myData" when you created the package, you can load it up using: data(myData) The difficulties that arise on Windows are due to the poor state of the Windows command line when it comes to the tools that are necessary to build the package- most importantly you need a Perl interpreter. Duncan Murdoch has put together an installer that can supply the missing tools- you can find it at: http://www.murdoch-sutherland.com/Rtools/ If it asks anything about modifying your PATH while installing, allow it to do so. Placing a folder of tools in the PATH makes the difference between using perl myScript.pl or /really/long/tiresome/hard/to/remember/path/to/perl myScript.pl ...or just using source works as well! ;) -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/Include-files--tp24647621p24648363.html Sent from the R help mailing list archive at Nabble.com.
On 7/24/2009 12:25 PM, Mark Knecht wrote:> Hi, > I have 15 or 20 functions I've written to convert the sort of data > I'm working with. They are currently in their own R file which I load > by hand in Rgui before loading and running my main programs. > > Is there any way to have this file included in my R program like > #include might in C?source()> If not is there a simple newbie-type example of how to create a > package so I could just say something like require(MWKFunctions) and > be done with this?I gave a talk on this at last year's useR; my slides are online at http://www.r-project.org/conferences/useR-2008/slides/Murdoch.pdf The title says it's for Windows, but most of it applies to any platform. Duncan Murdoch