[R] Include files?

cls59 chuck at sharpsteen.net
Fri Jul 24 19:13:29 CEST 2009



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.




More information about the R-help mailing list