Organization source code R

All,

I am starting to write object-oriented R-code for the first time and I suppose there are several R files with dependencies between them. I'm new to R and haven't written anything outside of a massive script to test ideas yet. Are there any resources on the Internet that give tips on how to organize your code? In addition to describing how to create packages, I cannot find such a guide. At this point, I just want to organize the code so that it makes loading and interacting with a set of routines as simple as possible.

Appreciate any recommendations you can provide.

Chris

+45
r
Feb 17 '10 at 21:12
source share
2 answers

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

You should consider creating a package R. You can use the package.skeleton function to start with a given set of R. files. I also highly recommend using roxygen to document the package at the beginning, because it is much more difficult to do after the fact.

Read "Writing R-Extensions" . There is a section on this subject in the online book "Statistics with R". Also take a look at Creating R Packages: A Tutorial from Friedrich Leish. Finally, if you are in New York, come to the upcoming NY-R group meeting on โ€œCreating R-Packages: A Tender Introduction with Examplesโ€ .

Just to rephrase some good practice suggestions:

  • The package allows you to use the R CMD check , which is very useful when detecting errors; You can see separately using the codetools package.
  • The package also forces you to do the minimum amount of documentation, which leads to best practices in the long run.
  • You should also consider unit testing (e.g. RUnit ) if you want your code to be reliable / supported.
  • You should use a style guide (such as the Google Style Guide ).
  • Use a version control system from the start, and if you're planning on making your code open source, consider using github or r-forge.

Edit:

Regarding how to make incremental changes without rebuilding or installing the complete package: I think the simplest thing is to make changes to your corresponding R file, and then use the source command to load these changes. Once you upload your library to the R-session, it will always be lower in the environment (and lower in priority) than in .GlobalEnv, so any changes that youโ€™ve downloaded or downloaded will be used first (use the search command to see this). This way you can have your own package and you rewrite the changes when you test them in the environment.

Alternatively, you can use an IDE, such as StatET or ESS. They load individual lines or functions from the R package very easily. StatET is especially well-suited for handling control packages in a catalog structure.

+41
Feb 17 '10 at 21:18
source share

this is useful for others who are directed to this post when searching for them. I also came across the exact same scenario and did not find a resource that explained this clearly. Here is my attempt to put the solution in a few simple steps:
1) Create a new project directory
2) Create a package through studio R (same process as above)
3) Keep both in one place (to avoid confusion).
4) Install and download the packages: devtools and roxygen2.
5) use the load_all () function.

And you're done.

0
Feb 25 '17 at 20:27
source share



All Articles