knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
grkstyle
is an extension package for styler that holds my personal code style preferences.
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")
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()")
A few examples drawn from the tidyverse style guide.
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
text_vec <- 'fruits <- c(\n "apple",\n "banana",\n "mango"\n)' cat("\n``` r", text_vec, "```", sep = "\n")
grkstyle
text_vec <- grkstyle::grk_style_text(text_vec) cat("\n``` r", paste(text_vec, collapse = "\n"), "```", sep = "\n")
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_*()
.
unstyled
text <- ' do_something_very_complicated(something = "that", requires = many, arguments = "some of which may be long") ' cat("\n\n``` r", text, "```")
grkstyle
text_grk <- grkstyle::grk_style_text(text) cat("\n\n``` r", paste(text_grk, collapse = "\n"), "\n```")
styler::tidyverse_style
text_tidy <- styler::style_text(text) cat("\n\n``` r", paste(text_tidy, collapse = "\n"), "\n```")
unstyled
text <- ' long_function_name <- function(a = "a long argument", b = "another argument", c = "another long argument") { # As usual code is indented by two spaces. } ' cat("\n\n``` r", text, "```")
grkstyle
text_grk <- grkstyle::grk_style_text(text) cat("\n\n``` r", paste(text_grk, collapse = "\n"), "\n```")
styler::tidyverse_style
text_tidy <- styler::style_text(text) cat("\n\n``` r", paste(text_tidy, collapse = "\n"), "\n```")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.