Skip to content

A Tidy R Code Style

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

gadenbuie/grkstyle

Repository files navigation

grkstyle

grkstyle status badge R-CMD-check

grkstyle is an extension package for styler that holds my personal code style preferences.

Installation

You can install grkstyle from my r-universe:

options(repos = c(
	gadenbuie = "https://gadenbuie.r-universe.dev",
	getOption("repos")
))

# Download and install grkstyle in R
install.packages("grkstyle")

Or you can install grkstyle directly from Github:

# install.packages("remotes")
remotes::install_github("gadenbuie/grkstyle")

Usage

To use grkstyle by default in styler functions and addins

# Set default code style for {styler} functions
grkstyle::use_grk_style()

Or add the following to your ~/.Rprofile

options(styler.addins_style_transformer = "grkstyle::grk_style_transformer()")

Examples

A few examples drawn from the tidyverse style guide.

Tabs vs Spaces

I’ve been staunchly committed to indentation by two spaces, but I’ve recently come to realize that indentation with tabs is objectively better. Primarily it’s about accessibility. Using tabs allows others to choose their preferred indentation levels, it accommodates more code authors in a wider variety of scenarios, and it’s better for Braille code readers:

The main reason I would like to see this change is for refreshable braille displays that are used by blind programmers a lot. Each space wastes one braille cell and takes away valuable braille realestate. So if the default indentation of a project is 4 spaces per level, a 3rd level indentation wastes 12 braille cells before code starts.
Comment by MarcoZehe

All of the grk_style_text(), grk_style_file(), grk_style_dir() and grk_style_pkg() functions will by default automatically detect the UseTabsForSpaces setting the RStudio project file. You can switch to tabs by updating the RStudio project settings to disable “Insert spaces for tab” (Tools > Project Options > Code Editing > Insert spaces for tab) and then running one of the above functions. Alternatively, you can set grkstyle.use_tabs = TRUE in the .Rprofile file in your home directory or your project directory.

unstyled

fruits <- c(
  "apple",
  "banana",
  "mango"
)

grkstyle

fruits <- c(
	"apple",
	"banana",
	"mango"
)

If you’d like to quickly transition to tabs throughout your package code, you can use the grk_reindent_tabs_*() helper functions. These function apply only the styler indentation rules and should only affect the indentation of your code.

# re-indent your package code using tabs
grk_reindent_tabs_pkg()

There are equivalent helper functions to standardize around spaces, e.g. grk_reindent_spaces_*(), or to use the RStudio project option, e.g. grk_reindent_auto_*().

Line Breaks Inside Function Calls

unstyled

do_something_very_complicated(something = "that", requires = many,
                              arguments = "some of which may be long")

grkstyle

do_something_very_complicated(
	something = "that",
	requires = many,
	arguments = "some of which may be long"
) 

styler::tidyverse_style

do_something_very_complicated(
  something = "that", requires = many,
  arguments = "some of which may be long"
) 

Indentation of Function Arguments

unstyled

long_function_name <- function(a = "a long argument",
                               b = "another argument",
                               c = "another long argument") {
  # As usual code is indented by two spaces.
}

grkstyle

long_function_name <- function(
	a = "a long argument",
	b = "another argument",
	c = "another long argument"
) {
	# As usual code is indented by two spaces.
} 

styler::tidyverse_style

long_function_name <- function(a = "a long argument",
                               b = "another argument",
                               c = "another long argument") {
  # As usual code is indented by two spaces.
} 

About

A Tidy R Code Style

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages