Giter Site home page Giter Site logo

grkstyle's Introduction

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",
	getOptions("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.
} 

grkstyle's People

Contributors

gadenbuie avatar colinfay avatar tonyelhabr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.