grk_style: The grkstyle Code Style

grk_styleR Documentation

The grkstyle Code Style

Description

Use styler::style_text() to format code according to the unofficial grkstyle style guide. Follows the tidyverse style guide as implemented by styler::tidyverse_style(), but with additional style rules that enforce consistent line breaks inside function calls and left-justification of function arguments in function definitions.

Usage

use_grk_style()

grk_style_transformer(..., use_tabs = getOption("grkstyle.use_tabs", NULL))

grk_style_text(text, ...)

grk_style_file(path, ...)

grk_style_dir(path, ...)

grk_style_pkg(pkg = ".", ...)

Arguments

...

Arguments passed to underling styler functions (identified by removing the grk_ prefix), except for transformers, which is set to the grk_style_transformer() internally.

use_tabs

Should a single tab be used for indentation? grkstyle functions will by default follow the grkstyle.use_tabs option, if set, or will attempt to look up the project settings in the current RStudio project. You can choose to use tabs or spaces for a project by changing the Insert spaces for tab option in the Code Editing panel of the Project Options settings.

If not otherwise set via the R option or the RStudio project option, tabs are used. Tabs are recommended because they are more accessible.

You can opt into indentation by two spaces by setting the grkstyle.use_tabs option:

# two spaces
options(grkstyle.use_tabs = FALSE)

# equivalently, but more precise
options(grkstyle.use_tabs = list(indent_by = 2, indent_character = " "))
text

A character vector with text to style.

path

A character vector with paths to files to style.

pkg

Path to a (subdirectory of an) R package.

Functions

  • use_grk_style: Set the grkstyle style as the default style for styler addins.

  • grk_style_transformer: A code transformer for use with styler::style_text()

  • grk_style_text: Style text using the grkstyle code style

  • grk_style_file: Style a file using the grkstyle code style

  • grk_style_dir: Style a directory using the grkstyle code style

  • grk_style_pkg: Style a package using the grkstyle code style

Using the grkstyle code style

You can set the grkstyle code style as the default code style for styler (and its associated RStudio addins, like "Style active file" and "Style selection") by calling grkstyle::use_grk_style(). If you would rather set this option globally for all session, you can add the following to your .Rprofile:

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

Examples

## Not run: 
use_grk_style()
# Use styler addins
styler:::style_selection()

## End(Not run)

ex_code <- "mtcars %>% mutate(mpg = mpg * 2,\n\t\t     cyl = paste(cyl)) %>% head()"
cat(ex_code)

grk_style_text(ex_code)

gadenbuie/grkstyle documentation built on Oct. 25, 2023, 4:53 p.m.