|
| 1 | +--- |
| 2 | +title: "An introduction to styler" |
| 3 | +author: "Lorenz Walthert" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +output: rmarkdown::html_vignette |
| 6 | +vignette: > |
| 7 | + %\VignetteIndexEntry{An introduction to styler} |
| 8 | + %\VignetteEngine{knitr::rmarkdown} |
| 9 | + %\VignetteEncoding{UTF-8} |
| 10 | +--- |
| 11 | + |
| 12 | +This vignette introduces the basic functionality of styler and showcases |
| 13 | +how styler applies a few rules of the |
| 14 | +[tidyverse style guide](http://style.tidyverse.org/index.html) to example code. |
| 15 | +Note that you can create your own style guide and customize styler even further, |
| 16 | +as described in the vignette "Customizing styler". |
| 17 | + |
| 18 | +```{r, echo = FALSE} |
| 19 | +knitr::opts_chunk$set(echo = TRUE, comment = "") |
| 20 | +knitr::knit_engines$set(list( |
| 21 | + styler = function(options) { |
| 22 | + options$comment <- "" |
| 23 | + knitr::engine_output( |
| 24 | + options, |
| 25 | + |
| 26 | + c("# Before", options$code), |
| 27 | + c("# After", styler::style_text(options$code)) |
| 28 | + ) |
| 29 | + } |
| 30 | +)) |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +It's possible to use different levels of 'invasiveness', as described in the |
| 35 | +help file for the only style guide implemented so far, which is the |
| 36 | +[tidyverse style guide](http://style.tidyverse.org/index.html). The style guide |
| 37 | +in use is passed to the styling function (i.e `style_text()` and friends) via |
| 38 | +the `style` argument, which defaults to `tidyverse_style`. In addition to this |
| 39 | +argument, there are further customization options. For example, we can limit |
| 40 | +ourselves to styling just spacing information by indicating this with the |
| 41 | +`scope` argument: |
| 42 | + |
| 43 | +```{r} |
| 44 | +library("styler") |
| 45 | +library("magrittr") |
| 46 | +style_text("a=3; 2", scope = "spaces") |
| 47 | +``` |
| 48 | + |
| 49 | +Or, on the other extreme of the scale, styling spaces, indention, line breaks |
| 50 | +and tokens (which is the default): |
| 51 | + |
| 52 | +```{r} |
| 53 | +style_text("a=3; 2", scope = "tokens") |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +`scope` always includes less-invasive styling than the option chosen, |
| 58 | +e.g. `spaces = "line_breaks"` includes styling spaces and indention in addition |
| 59 | +to line breaks. |
| 60 | + |
| 61 | + |
| 62 | +We can also choose to style line breaks but not tokens: |
| 63 | +```{r} |
| 64 | +style_text("if(x) {66 } else {a=3}", scope = "line_breaks") |
| 65 | +``` |
| 66 | + |
| 67 | +Note that `scope = "spaces"` does not touch indention |
| 68 | +```{r} |
| 69 | +code <- c( |
| 70 | + "a <- function() { ", |
| 71 | + " a=3", |
| 72 | + "}" |
| 73 | +) |
| 74 | +
|
| 75 | +style_text(code, scope = "spaces") |
| 76 | +``` |
| 77 | + |
| 78 | +But `scope = "indention"` - as the name says - does. |
| 79 | +```{r} |
| 80 | +style_text(code, scope = "indention") |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +Another option that is helpful to determine the level of 'invasiveness' is |
| 85 | +`strict`. If set to `TRUE`, spaces and line breaks before or after tokens are |
| 86 | +set to either zero or one. However, in some situations this might be undesirable, |
| 87 | +as the following example shows: |
| 88 | + |
| 89 | +```{r} |
| 90 | +style_text( |
| 91 | + "data_frame( |
| 92 | + small = 2 , |
| 93 | + medium = 4,#comment without space |
| 94 | + large = 6 |
| 95 | + )", strict = FALSE |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +We prefer to keep the equal sign after "small", "medium" and large aligned, |
| 100 | +so we set `strict = FALSE` to set spacing to *at least* one around `=`. |
| 101 | + |
| 102 | +Also, spaces before comments are preserved with that option. |
| 103 | + |
| 104 | +```{r} |
| 105 | +style_text( |
| 106 | + "a <- 'one' #just one |
| 107 | + abc <- 'three' # three", |
| 108 | + strict = FALSE |
| 109 | +) |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +Though simple, hopefully the above examples convey some of the flexibility of |
| 114 | +the configuration options available in `styler`. Let us for now focus on a |
| 115 | +configuration with `strict = TRUE` and `scope = "tokens"` and illustrate a few |
| 116 | +more examples of code before and after styling. |
| 117 | + |
| 118 | +`styler` can identify and handle unary operators and other math tokens: |
| 119 | + |
| 120 | +```{styler} |
| 121 | +1++1-1-1/2 |
| 122 | +``` |
| 123 | + |
| 124 | +It can also format complicated expressions that involve line breaking and |
| 125 | +indention based on both brace expressions and operators: |
| 126 | + |
| 127 | +```{styler} |
| 128 | +if (x >3) {stop("this is an error")} else { |
| 129 | +c(there_are_fairly_long, |
| 130 | +1 / 33 * |
| 131 | +2 * long_long_variable_names)%>% k( |
| 132 | +
|
| 133 | +) } |
| 134 | +``` |
| 135 | + |
| 136 | +Lines are broken after `(` if a function call spans multiple lines: |
| 137 | + |
| 138 | +```{styler} |
| 139 | +do_a_long_and_complicated_fun_cal("which", has, way, to, |
| 140 | + "and longer then lorem ipsum in its full length" |
| 141 | + ) |
| 142 | +``` |
| 143 | + |
| 144 | +`styler` replaces `=` with `<-` for assignment: |
| 145 | +```{styler} |
| 146 | +one = "one string" |
| 147 | +``` |
| 148 | + |
| 149 | +It converts single quotes within strings if necessary: |
| 150 | +```{styler} |
| 151 | +one <- 'one string' |
| 152 | +two <- "one string in a 'string'" |
| 153 | +``` |
| 154 | + |
| 155 | +And adds braces to function calls in pipes: |
| 156 | + |
| 157 | +```{styler} |
| 158 | +a %>% |
| 159 | + b %>% |
| 160 | + c |
| 161 | +``` |
| 162 | + |
| 163 | +Function declarations are indented if multi-line: |
| 164 | + |
| 165 | +```{styler} |
| 166 | +my_fun <- function(x, |
| 167 | +y, |
| 168 | +z) { |
| 169 | + just(z) |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +`styler` can also deal with tidyeval syntax: |
| 174 | + |
| 175 | +```{styler} |
| 176 | +mtcars %>% |
| 177 | + group_by( !!my_vars ) |
| 178 | +``` |
0 commit comments