Chris Watson
Chris Watson

Reputation: 1367

Best practices to handle personal functions in R

I have written personal functions in R that are not specific to one (or a few) projects.

What are the best practices (in R) to put those kind of functions?

Is the best way to do it to have one file that gets sourced at startup? or is there a better (recommended) way to deal with this situation?

Upvotes: 5

Views: 918

Answers (4)

Bangyou
Bangyou

Reputation: 9816

From my experience, a package will be the best choice for personal functions. Firstly I put all new functions into a personal package, which I called it My. When I find some functions was similar and are worth to become an independent package, I will create a new package and move them.

Upvotes: 0

MadSeb
MadSeb

Reputation: 8234

Create a package named "utilities" , put utility functions in that package, try to aim for one function per file, and store the package in a source control system (e.g., GIT, SVN ). It will save you time in the long run.

P.S. .Rprofile tends to get accidentally deleted.

Upvotes: 4

Ari
Ari

Reputation: 1972

Most people use the .Rprofile file for this. Here are two links which talk about this file in some detail.

  1. http://www.statmethods.net/interface/customizing.html
  2. http://blog.revolutionanalytics.com/2013/10/sample-rprofile.html

At the top of my .Rprofile file I call library() for the various libraries which I normally use. I also have some personal handy functions which I've come to rely on. Because this file is sourced on startup, they are available to me every session.

Upvotes: 2

Christopher Louden
Christopher Louden

Reputation: 7582

If you have many, it would be good to make it into a package that you load each time you start working.

It is probably not a good idea to have a monolithic script with a bunch of functions. Instead break the file up into several files each of which either has only one function (my preference) or has a group of functions that are logically similar. That makes it easier to find things when you need to make changes.

Upvotes: 3

Related Questions