This shows you the differences between two versions of the page.
| 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 | + | R code that is submitted |
| - | * Do not use dots in variable | + | * Use `<-` for variable |
| - | * You can use underscores in variable names. | + | * Include spaces between operators (e.g. `+`, `-`, `&`), before `{`, and after `for`, `if`, `while`, `,` and `)`. |
| - | * | + | * When possible, maximum line length should be 100 characters (soft limit of 80 characters). |
| - | * | + | * Number |
| - | * The functions do not need to have a detailed explanation | + | * Double quotes |
| - | * 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 |
| - | 0.1 - 2011-03 | + | |
| - | | + | |
| - | Attention: These rules do not apply for the documentation .Rd files in ' | ||
| + | ====== Examples ====== | ||
| + | |||
| + | # Proper spacing, indentation spaces and text quotes: | ||
| + | NewFunction <- function(text = " | ||
| + | # Check uppercase parameter | ||
| + | if (!is.logical(uppercase)) { | ||
| + | stop(" | ||
| + | } | ||
| + | # 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 = " | ||
| + | two = " | ||
| + | three = " | ||
| + | four = " | ||
| + | # or: | ||
| + | my_strings <- list( | ||
| + | one = " | ||
| + | two = " | ||
| + | three = " | ||
| + | four = " | ||
| + | ) | ||
| + | |||
| + | ====== Other resources ====== | ||
| + | |||
| + | For more tips on writing clean and easy to read code, check the [[https:// | ||