Skip to content

Commit

Permalink
Merge pull request #1131 from r-lib/rm-dplyr
Browse files Browse the repository at this point in the history
Don't require dplyr anywhere
  • Loading branch information
lorenzwalthert authored Jun 12, 2023
2 parents 68977f3 + eec8c06 commit acd0b46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Imports:
Suggests:
data.tree (>= 0.1.6),
digest,
dplyr,
here,
knitr,
prettycode,
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-cache-low-level-api.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
test_that("caching utils make right blocks with semi-colon", {
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = FALSE) %>%
base::transform(is_cached = FALSE) %>%
cache_find_block()
expect_equal(blocks_simple_uncached, c(1, 1, 1, 1))

blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = TRUE) %>%
base::transform(is_cached = TRUE) %>%
cache_find_block()
expect_equal(blocks_simple_cached, c(1, 1, 1, 1))

blocks_edge <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>%
base::transform(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>%
cache_find_block()
expect_equal(blocks_edge, c(1, 2, 2, 2))
})
Expand All @@ -30,7 +30,7 @@ test_that("caching utils make right blocks with comments", {


blocks_simple_uncached <- compute_parse_data_nested(text) %>%
dplyr::mutate(is_cached = c(
base::transform(is_cached = c(
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
TRUE, FALSE, FALSE, FALSE
)) %>%
Expand All @@ -48,7 +48,7 @@ test_that("caching utils make right blocks with comments", {
tau1 = 1 # here?
"
blocks_simple_cached <- compute_parse_data_nested(text) %>%
dplyr::mutate(is_cached = c(
base::transform(is_cached = c(
FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE
)) %>%
cache_find_block()
Expand Down Expand Up @@ -101,17 +101,17 @@ test_that("blank lines are correctly identified", {

test_that("caching utils make right blocks with comments", {
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2 # comment")) %>%
dplyr::mutate(is_cached = FALSE) %>%
base::transform(is_cached = FALSE) %>%
cache_find_block()
expect_equal(blocks_simple_uncached, c(1, 1, 1))

blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2 # comment2")) %>%
dplyr::mutate(is_cached = TRUE) %>%
base::transform(is_cached = TRUE) %>%
cache_find_block()
expect_equal(blocks_simple_cached, c(1, 1, 1))

blocks_edge <- compute_parse_data_nested(c("1 + 1", "2 # 1+1")) %>%
dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE)) %>%
base::transform(is_cached = c(TRUE, TRUE, FALSE)) %>%
cache_find_block()
expect_equal(blocks_edge, c(1, 2, 2))
})
Expand Down
10 changes: 5 additions & 5 deletions vignettes/customizing_styler.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The `transformers` argument is, apart from the code to style, the key argument o

```{r, message = FALSE}
library("styler")
library("magrittr")
cache_deactivate()
library("dplyr")
names(tidyverse_style())
str(tidyverse_style(), give.attr = FALSE, list.len = 3)
```
Expand All @@ -50,17 +50,17 @@ As the name says, this function removes spaces after the opening parenthesis. Bu
string_to_format <- "call( 3)"
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
styler:::pre_visit_one(default_style_guide_attributes)
pd$child[[1]] %>%
select(token, terminal, text, newlines, spaces)
cols <- c('token', 'terminal', 'text', 'newlines', 'spaces')
pd$child[[1]][, cols]
```

`default_style_guide_attributes()` is called to initialize some variables, it does not actually transform the parse table.

All the function `remove_space_after_opening_paren()` now does is to look for the opening bracket and set the column `spaces` of the token to zero. Note that it is very important to check whether there is also a line break following after that token. If so, `spaces` should not be touched because of the way `spaces` and `newlines` are defined. `spaces` are the number of spaces after a token and `newlines`. Hence, if a line break follows, spaces are not EOL spaces, but rather the spaces directly before the next token. If there was a line break after the token and the rule did not check for that, indention for the token following `(` would be removed. This would be unwanted for example if `use_raw_indention` is set to `TRUE` (which means indention should not be touched). If we apply the rule to our parse table, we can see that the column `spaces` changes and is now zero for all tokens:

```{r}
styler:::remove_space_after_opening_paren(pd$child[[1]]) %>%
select(token, terminal, text, newlines, spaces)
styler:::remove_space_after_opening_paren(pd$child[[1]])[, cols]
```

All top-level styling functions have a `style` argument (which defaults to `tidyverse_style`). If you check out the help file, you can see that the argument `style` is only used to create the default `transformers` argument, which defaults to `style(...)`. This allows for the styling options to be set without having to specify them inside the function passed to `transformers`.
Expand Down

0 comments on commit acd0b46

Please sign in to comment.