Package 'grkstyle'

Title: A Tidy R Code Style
Description: A tidy code style building on the tidyverse code style guidelines but with a small changes for consistent line breaks and indentation. Enforces line breaks, if there are any initially between arguments in function calls and left-indents arguments in function definitions. Defaults to tabs, not spaces.
Authors: Garrick Aden-Buie [aut, cre]
Maintainer: Garrick Aden-Buie <[email protected]>
License: MIT + file LICENSE
Version: 0.2.1
Built: 2024-11-16 03:33:54 UTC
Source: https://github.com/gadenbuie/grkstyle

Help Index


Re-indent R code using tabs or spaces

Description

Quickly convert from spaces to tabs or from tabs to spaces in R code throught a file, directory or package.

Usage

grk_reindent_auto_text(text, ...)

grk_reindent_auto_file(path, ...)

grk_reindent_auto_dir(path, ...)

grk_reindent_auto_pkg(pkg = ".", ...)

grk_reindent_tabs_text(text, ...)

grk_reindent_tabs_file(path, ...)

grk_reindent_tabs_dir(path, ...)

grk_reindent_tabs_pkg(pkg = ".", ...)

grk_reindent_spaces_text(text, ..., indent_by = 2L)

grk_reindent_spaces_file(path, ..., indent_by = 2L)

grk_reindent_spaces_dir(path, ..., indent_by = 2L)

grk_reindent_spaces_pkg(pkg = ".", ..., indent_by = 2L)

Arguments

text

A character vector with text to style.

...

Arguments passed on to the style function.

path

A character vector with paths to files to style.

pkg

Path to a (subdirectory of an) R package.

indent_by

The number of spaces by which to indent the code.

Functions

  • grk_reindent_auto_text: Re-indent text using tabs or spaces according to the RStudio project settings or the grkstyle.use_tabs option.

  • grk_reindent_auto_file: Re-indent a file using tabs or spaces according to the RStudio project settings or the grkstyle.use_tabs option.

  • grk_reindent_auto_dir: Re-indent a directory using tabs or spaces according to the RStudio project settings or the grkstyle.use_tabs option.

  • grk_reindent_auto_pkg: Re-indent a package using tabs or spaces according to the RStudio project settings or the grkstyle.use_tabs option.

  • grk_reindent_tabs_text: Re-indent text using tabs

  • grk_reindent_tabs_file: Re-indent a file using tabs

  • grk_reindent_tabs_dir: Re-indent a directory using tabs

  • grk_reindent_tabs_pkg: Re-indent a package using tabs

  • grk_reindent_spaces_text: Re-indent text using spaces

  • grk_reindent_spaces_file: Re-indent a file using spaces

  • grk_reindent_spaces_dir: Re-indent a directory using spaces

  • grk_reindent_spaces_pkg: Re-indent a package using spaces


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)