Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gadenbuie/grkstyle
A Tidy R Code Style
https://github.com/gadenbuie/grkstyle
Last synced: 17 days ago
JSON representation
A Tidy R Code Style
- Host: GitHub
- URL: https://github.com/gadenbuie/grkstyle
- Owner: gadenbuie
- License: other
- Created: 2019-08-06T01:55:49.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-10-23T14:57:12.000Z (about 1 year ago)
- Last Synced: 2024-10-14T22:29:34.345Z (30 days ago)
- Language: R
- Size: 58.6 KB
- Stars: 89
- Watchers: 5
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - gadenbuie/grkstyle - A Tidy R Code Style (R)
README
---
output:
github_document:
pandoc_args: "--preserve-tabs"
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# grkstyle[![grkstyle status badge](https://gadenbuie.r-universe.dev/badges/grkstyle)](https://gadenbuie.r-universe.dev)
[![R-CMD-check](https://github.com/gadenbuie/grkstyle/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/gadenbuie/grkstyle/actions/workflows/R-CMD-check.yaml)[styler]: https://styler.r-lib.org
`grkstyle` is an extension package for [styler] that holds my personal code style preferences.
## Installation
You can install grkstyle from my [r-universe](https://gadenbuie.r-universe.dev):
``` r
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:
``` r
# install.packages("remotes")
remotes::install_github("gadenbuie/grkstyle")
```## Usage
To use `grkstyle` by default in styler functions and addins
```{r eval=FALSE}
# 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](https://style.tidyverse.org).
### 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](https://alexandersandberg.com/articles/default-to-tabs-instead-of-spaces-for-an-accessible-first-environment/).
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](https://github.com/prettier/prettier/issues/7475#issuecomment-668544890) by [MarcoZehe](https://github.com/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**
```{r echo = FALSE, results = "asis"}
text_vec <- 'fruits <- c(\n "apple",\n "banana",\n "mango"\n)'
cat("\n``` r", text_vec, "```", sep = "\n")
```**grkstyle**
```{r, echo=FALSE, results="asis"}
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.```r
# 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**
```{r, echo=FALSE, results="asis"}
text <- '
do_something_very_complicated(something = "that", requires = many,
arguments = "some of which may be long")
'cat("\n\n``` r", text, "```")
```**grkstyle**
```{r, echo=FALSE, results="asis"}
text_grk <- grkstyle::grk_style_text(text)cat("\n\n``` r", paste(text_grk, collapse = "\n"), "\n```")
```**styler::tidyverse_style**
```{r, echo=FALSE, results="asis"}
text_tidy <- styler::style_text(text)cat("\n\n``` r", paste(text_tidy, collapse = "\n"), "\n```")
```### Indentation of Function Arguments
**unstyled**
```{r, echo=FALSE, results="asis"}
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**
```{r, echo=FALSE, results="asis"}
text_grk <- grkstyle::grk_style_text(text)cat("\n\n``` r", paste(text_grk, collapse = "\n"), "\n```")
```**styler::tidyverse_style**
```{r, echo=FALSE, results="asis"}
text_tidy <- styler::style_text(text)cat("\n\n``` r", paste(text_tidy, collapse = "\n"), "\n```")
```