Skip to content

Style Guide

Patrick Schratz edited this page Aug 1, 2019 · 5 revisions

We have our own style guide for all projects in the mlr-org.

The sections you how you can easily use our style guide when making changes to any mlr-org repo using either RStudio or VIM.

1. RStudio

With an mlr-org project open, go to Tools -> Project Options -> Code Editing and set the following options:

  • Insert spaces for tab: 2
  • Ensure that source files end with new line: ☑
  • Strip trailing whitespace: ☑

Use the styler package

The styler package has the ability to apply the mlr_style to either the active file or the whole package.

The easiest way to apply the style is via the RStudio-addin. By default the addin will apply the "tidyverse style". To change this behavior, you need to first install the "mlr" branch via

remotes::install_github("pat-s/styler@mlr-style")

And append the following to your own .Rprofile:

if (grepl("mlr", getwd()) || grepl("paradox", getwd())) {
  options(styler.addins_style_transformer = "mlr_style()")
}

The setting options(styler.addins_style_transformer = "mlr_style()") ensures that the mlr_style will be used for this project when you run styler::style_active_file().

To make this task even more convenient, we recommend to map styler::style_active_file() to a shortcut in RStudio. Go to Tools -> Modify Keyboard Shortcuts and search for "Styler". Next, click into the "Shortcut" field and press the desired key-combination you want to use.

2. VIM

If you are a VIM user, you can use the ale plugin to use styler as a "fixer".

With your favorite plugin manager, set the following plugin options:

let g:ale_fixers = {
   \   '*': ['remove_trailing_lines', 'trim_whitespace'],
   \   'r': ['styler'],
\}
let g:ale_r_styler_options = 'styler::mlr_style'

To apply the style guide in a R file, you need to call :ALEFix. This can be mapped to a binding as follows

nmap <F8> <Plug>(ale_fix)

Note: Do not forget to install remotes::install_github("lorenzwalthert/styler@mlr-style") before.