Giter Site home page Giter Site logo

r_style_fao's Introduction

R programming guidelines

This is the repository for the R programming guideline of the Food and Agricultural Organization of the United Nations.

Four Golden Rules

  1. Have meaningful names for your varable, function, and script

  2. Keep the functions simple and compact. Do one thing and do it well. (rule of thumb, do not exceed 100 lines).

  3. Wrap your code, do not exeed 80~100 characters, indent your code both 2 or 4 space are fine. Using an editor such as R Studio will help you with this.

  4. Comment your code, they act like pseudo codes. See example at functions/imputationExample.R and analysis/analysis.R.

Writing R codes

Style

Unlike other languages, there is no one single style for programming in R. Listed below are several sources of coding style, the style you choose does not really matter, just be consistent.

Google's R Style Guide

Hadley Wickham's R style

Paul Johnson's R style

Henrik Bengtsson's R style

Documentation, explanation and comments

Documentation is a daunting yet necessary task, thankfully these are made simple with the R package roxygen2.

A simple use of the roxygen declaration can be found in the hello_world.R and imputationExample.R under the functions folder.

Roxygen: Simple documentation for R

In order for other users to quickly understand and debug your code if necessary, comment are necessary.

A simple description such as Perform data manipulation or Performing linear regression for prediction should suffice.

The purpose is to tell the user what task is performed at each step. This allows people to skim through the script to understand the purpose and the logic of the analysis and program.

Managing scripts

Organization of project is another crucial element in helping people understanding your work.

Functions

Each function should be in a seperate script, the function should be compact and efficient and contained within a single folder.

A function should do only one thing, and do it well - Curly's Law

It is generally better to split up the function, each with their own documentation and do just one thing. You can then have a wrapper function to wrap everything up if you would like.

Functions should also have simple statements at the beginning of their execution that ensure that passed values are valid. For example, if a function takes three arguments a, b, and c and adds them together, then the function should ensure a, b, and c are all numeric before proceeding. These checks can easily be implemented via the stopifnot R function which returns an error if the argument is not true. Custom error messages can be implemented via: if(condition) stop("").

Tests should also be implemented to verify that functions work as expected. A "tests" directory should be created in the same directory as the package directory, and test files should be placed in the tests directory. These files should be named "test_..." and contain simple examples of use cases for these functions. The testthat package can be used to easily automate these tests. The stringr package has many good examples of what these test files should look like.

Analysis

This repository illustrates how I usually organize my work which may be very restrictive. However the important point to note is that there is a logical structure.

Preparing data

Reshaping Data with the reshape Package After the publication of this paper the author created new version of reshape - 'reshape2', where cast was changed to dcast/acast.

The Split-Apply-Combine Strategy for Data Analysis

stringr: modern, consistent string processing

Dates and Times Made Easy with lubridate

Building R packages

A good guideline from Leek group policy

r_style_fao's People

Contributors

malexan avatar mkao006 avatar jamaas avatar joshbrowning2358 avatar

Stargazers

Christian Mongeau avatar ykhorizon avatar timelyportfolio avatar Gerhard Nachtmann avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

r_style_fao's Issues

plyr instead of for?

To achieve more elegant code I would persuade users into using of plyr's functions instead of 'for' cycle.

Additional bonus from plyr:

  • its functions are more powerful and flexible;
  • they are suitable for parallel multicore processes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.