[ACCEPTED]-Organizing R Source Code-r

Accepted answer
Score: 45

This question is very closely related to: "How to organize large R programs?"

You 29 should consider creating an R package. You 28 can use the package.skeleton function to start with given 27 a set of R files. I also strongly recommend 26 using roxygen to document the package at the beginning, because 25 it's much more difficult to do it after 24 the fact.

Read "Writing R Extensions". The online book "Statistics 23 with R" has a section on this subject. Also take a look at 22 Creating R Packages: A Tutorial by Friedrich Leisch. Lastly, if you're 21 in NY, come to the upcoming NY use-R group 20 meeting on "Authoring R Packages: a gentle introduction with examples".

Just to rehash some suggestions 19 about good practices:

  • A package allows you to use R CMD check which is very helpful at catching bugs; separately you can look at using the codetools package.
  • A package also forces you to do a minimal amount of documentation, which leads to better practices in the long run.
  • You should also consider doing unit testing (e.g. with RUnit) if you want your code to be robust/maintainable.
  • You should consider using a style guide (e.g. Google Style Guide).
  • Use a version control system from the beginning, and if you're going to make your code open source, then consider using github or r-forge.


Regarding how do make 18 incremental changes without rebuilding and 17 installing the full package: I find the 16 easiest thing to do is to make changes in 15 your relevant R file and then use the source command 14 to load those changes. Once you load your 13 library into an R session, it will always 12 be lower in the environment (and lower in 11 priority) than the .GlobalEnv, so any changes 10 that you source or load in directly will 9 be used first (use the search command to see this). That 8 way you can have your package underlying 7 and you are overwriting changes as you're 6 testing them in the environment.

Alternatively, you 5 can use an IDE like StatET or ESS. They 4 make loading individual lines or functions 3 out of an R package very easy. StatET is 2 particularly well designed to handle managing 1 packages in a directory-like structure.

Score: 0

this is for benefit of others who are directed 9 to this post upon their search. I too faced 8 exactly same scenario and found no resource 7 which explained it clearly. Here is my attempt 6 to put the solution in a few simple steps:
1) Create 5 a new project directory
2) Create a Package 4 via R studio(same process as above)
3) Keep 3 both in same location(to avoid confusion).
4) Install 2 and load packages: devtools and and roxygen2.
5) use 1 function load_all().

And you are done.

More Related questions