User Tools

Site Tools


tools:style_guides:r

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tools:style_guides:r [2015/05/26 11:28]
127.0.0.1 external edit
tools:style_guides:r [2024/09/10 14:39] (current)
vagudets correct links
Line 1: Line 1:
 +====== R style guide ======
  
-The style guidelines that we will apply to develop with R are from the [[http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml|Google'Style Guide]]with the following modifications agreed by the development team:+R code that is submitted to be included in any of the R tools developed at BSC should follow these style guidelinesbased on the tidyverse style guide:
  
-  *  Do not use dots in variable names+  * Use `<-` for variable assignment 
-  *  You can use underscores in variable names+  * Include spaces between operators (e.g. `+`, `-`, `&`), before `{`, and after `for`, `if`, `while`, `,` and `)`
-  *  Always use TRUE/FALSE instead of T/F+  * When possible, maximum line length should be 100 characters (soft limit of 80 characters)
-  *  There are two levels of commentsThe firstof general purpose, starts with a single hash and a spaceComments in this level should explain what the code is doingThe second, for stuff membersstarts with two hashes and a space. Comments in this level should explain details not needed to understand the general procedure but useful to make note of more technical aspects that might take time to find out later. +  * Number of indentation spaces is 2, using tabs for indentation is forbidden
-  *  The functions do not need to have a detailed explanation of usage, inputs and outputs in the .R code file, but it has to be in the associated .Rd documentation file. +  * Double quotes are recommended for stringsWhen writing quotes within quoted textuse double quotes outside and single quotes insideE.g.: `"Parameter 'na.rm' is missing."` 
-  *  A history of edition will be kept at the .Rd file associated to each function. The format of the history is as follows: +  * Self-explanatory names are preferred for variables. Try to be consistent with the variable naming style. Generally: avoid special characters (except underscores) and reserved words (ex: if, for, else, ...) 
-History: +  * Remember to include short comments to make the code easier to understand. Comments should be in their own line and they should start with `#` followed by a space. Comments that start with `##` and a space are for details not needed to understand the general procedure but useful to make note of more technical aspects of the code.
-  0.1  -  2011-03  (E. User, example.user@ic3.cat)  -  What has been done +
-   0.2  -  2013-03  (A. User, another.user@ic3.cat)  -  What has been changed or added.+
  
-Attention: These rules do not apply for the documentation .Rd files in 'man' folder. 
  
 +====== Examples ======
 +
 +  # Proper spacing, indentation spaces and text quotes:
 +  NewFunction <- function(text = "default", uppercase = TRUE) {
 +    # Check uppercase parameter
 +    if (!is.logical(uppercase)) {
 +      stop("Parameter 'uppercase' should be TRUE or FALSE.")
 +    }
 +    # Only transform text if needed
 +    if (uppercase && is.character(text)) {
 +    text <- toupper(text)
 +    }
 +    return(text)
 +  }
 +
 +  # How to format line breaks to avoid long lines:
 +  my_strings <- list(one = "un",
 +                     two = "dos",
 +                     three = "tres",
 +                     four = "quatre")
 +  # or:
 +  my_strings <- list(
 +    one = "un",
 +    two = "dos",
 +    three = "tres",
 +    four = "quatre"
 +  )
 +
 +====== Other resources ======
 +
 +For more tips on writing clean and easy to read code, check the [[https://style.tidyverse.org/index.html|tidyverse style guide]]. You can use the [[https://cloud.r-project.org/web/packages/styler/index.html|styler]] R package to automatically style your code, and it can even be integrated in RStudio!
tools/style_guides/r.1432639681.txt.gz · Last modified: 2018/05/30 09:59 (external edit)