Skip to content

Commit 4e8d386

Browse files
Merge pull request #589 from lorenzwalthert/improve-caching
- make R.cache an import (#589).
2 parents a958e3e + 825a4ad commit 4e8d386

13 files changed

+162
-201
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Usually you shouldn't need to change the first part of the file
44

55
# DO NOT CHANGE THE CODE BELOW
6-
before_install: R -q -e 'install.packages(c("remotes", "curl", "knitr", "rmarkdown")); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages(); remotes::install_deps(dependencies = TRUE); if (isTRUE(as.logical(toupper(Sys.getenv("R_REMOVE_RCACHE"))))) remove.packages("R.cache"); tic::before_install()'
6+
before_install: R -q -e 'install.packages(c("remotes", "curl", "knitr", "rmarkdown")); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages(); remotes::install_deps(dependencies = TRUE); tic::before_install()'
77
install: R -q -e 'tic::install()'
88
after_install: R -q -e 'tic::after_install()'
99
before_script: R -q -e 'tic::before_script()'
@@ -38,9 +38,6 @@ matrix:
3838
- r: release
3939
env:
4040
- BUILD_PKGDOWN: true
41-
- r: release
42-
env:
43-
- R_REMOVE_RCACHE: true
4441
- r: devel
4542

4643
#env

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Imports:
2121
cli (>= 1.1.0),
2222
magrittr (>= 1.0.1),
2323
purrr (>= 0.2.3),
24+
R.cache (>= 0.14.0),
2425
rematch2 (>= 2.0.1),
2526
rlang (>= 0.1.1),
2627
rprojroot (>= 1.1),
@@ -35,7 +36,6 @@ Suggests:
3536
here,
3637
knitr,
3738
prettycode,
38-
R.cache (>= 0.14.0),
3939
rmarkdown,
4040
rstudioapi (>= 0.7),
4141
testthat (>= 2.1.0)

NEWS.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1-
2-
# styler 1.2.0.9000
1+
# styler 1.2.9001
32

43
## Breaking changes
54

6-
* `style_pkg()` and `style_dir()` gain a new argument `exclude_dirs` to exclude
7-
directories from styling, by default `renv` and `packrat`. Note that the
8-
defaults won't change the behavior of `style_pkg()` because it does anyways
5+
* `style_pkg()` and `style_dir()` gain a new argument `exclude_dirs` to exclude
6+
directories from styling, by default `renv` and `packrat`. Note that the
7+
defaults won't change the behavior of `style_pkg()` because it does anyways
98
does not style these directories and they were set for consistency.
10-
11-
* `style_file()` and friends now strip `./` in file paths returned invisibly,
9+
10+
* `style_file()` and friends now strip `./` in file paths returned invisibly,
1211
i.e. `./script.R` becomes `script.R` (#568).
1312

1413
## New features
1514

16-
* ignore certain lines using `# styler: off` and `#styler: on` or custom
17-
markers, see `help("stylerignore")` (#560).
15+
* ignore certain lines using `# styler: off` and `#styler: on` or custom
16+
markers, see `?stylerignore` (#560).
1817

1918
* styler caches results of styling, so applying styler to code it has styled
2019
before will be instantaneous. This brings large speed boosts in many
2120
situations, e.g. when `style_pkg()` is run but only a few files have changed
2221
since the last styling or when using the [styler pre-commit
23-
hook](https://github.com/lorenzwalthert/precommit). Because styler caches
24-
by expression, you will also get speed boosts in large files with many
25-
expressions when you only change a few o them. See `help("caching")` for
26-
details (#538, #578).
27-
28-
* `create_style_guide()` gains two arguments `style_guide_name` and
29-
`style_guide_version` that are carried as meta data, in particular to version
30-
third-party style guides and ensure the proper functioning of caching. This
22+
hook](https://github.com/lorenzwalthert/precommit). Because styler caches by
23+
expression, you will also get speed boosts in large files with many
24+
expressions when you only change a few of them. See `?caching` for details
25+
(#538, #578).
26+
27+
* `create_style_guide()` gains two arguments `style_guide_name` and
28+
`style_guide_version` that are carried as meta data, in particular to version
29+
third-party style guides and ensure the proper functioning of caching. This
3130
change is completely invisible to users who don't create and distribute their
3231
own style guide like `tidyverse_style()` (#572).
3332

34-
3533
## Minor changes and fixes
3634

3735
* lines are now broken after `+` in `ggplot2` calls for `strict = TRUE` (#569).
3836

3937
* function documentation now contains many more line breaks due to roxygen2
4038
update to version 7.0.1 (#566).
41-
39+
4240
* spaces next to the braces in subsetting expressions `[` and `[[` are now
4341
removed (#580).
4442

43+
* Adapt to changes in the R parser to make styler pass R CMD check again.
44+
(#583).
45+
4546
# styler 1.2.0
4647

4748
## Breaking changes
@@ -51,33 +52,33 @@
5152
This is also reflected in the invisible return value of the function (#522).
5253

5354
* `style_file()` and friends do not write content back to a file when styling
54-
does not cause any changes in the file. This means the modification date of
55+
does not cause any changes in the file. This means the modification date of
5556
styled files is only changed when the content is changed (#532).
5657

5758
## New features
5859

59-
* Aligned function calls are detected and remain unchanged if they match the styler
60-
[definition for aligned function
60+
* Aligned function calls are detected and remain unchanged if they match the
61+
styler [definition for aligned function
6162
calls](https://styler.r-lib.org/articles/detect-alignment.html) (#537).
6263

63-
* curly-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now
64-
explicitly handled, where previously it was just treated as two consecutive
64+
* curly-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now
65+
explicitly handled, where previously it was just treated as two consecutive
6566
curly braces (#528).
6667

6768
* `style_pkg()`, `style_dir()` and the Addins can now style `.Rprofile`, and
6869
hidden files are now also styled (#530).
6970

7071
## Minor improvements and fixes
7172

72-
* Brace expressions in function calls are formatted in a less compact way to
73+
* Brace expressions in function calls are formatted in a less compact way to
7374
improve readability. Typical use case: `tryCatch()` (#543).
7475

75-
* Arguments in function declarations in a context which is indented multiple
76+
* Arguments in function declarations in a context which is indented multiple
7677
times should now be correct. This typically affects `R6::R6Class()` (#546).
7778

7879
* Escape characters in roxygen code examples are now correctly escaped (#512).
7980

80-
* Special characters such as `\n` in strings are now preserved in text and not
81+
* Special characters such as `\n` in strings are now preserved in text and not
8182
turned into literal values like a line break (#554).
8283

8384
* Style selection Addin now preserves line break when the last line selected is

R/communicate.R

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,3 @@ assert_data.tree_installation <- function() {
3434
abort("The package data.tree needs to be installed for this functionality.")
3535
}
3636
}
37-
38-
#' Assert the R.cache installation in conjunction with the cache config
39-
#'
40-
#' R.cache needs to be installed if caching functionality is enabled
41-
#' @param installation_only Whether or not to only check if R.cache is
42-
#' installed.
43-
#' @keywords internal
44-
assert_R.cache_installation <- function(installation_only = FALSE,
45-
action = "abort") {
46-
# fail if R.cache is not installed but feature is actiavted.
47-
if (!rlang::is_installed("R.cache") &&
48-
ifelse(installation_only, TRUE, cache_is_activated())
49-
) {
50-
msg_basic <- paste(
51-
"R package R.cache is not installed, which is needed when the caching ",
52-
"feature is activated. Please install the package with ",
53-
"`install.packages('R.cache')` and then restart R to enable the ",
54-
"caching feature of styler or permanently deactivate the feature by ",
55-
"adding `styler::cache_deactivate()` to your .Rprofile, e.g. via ",
56-
"`usethis::edit_r_profile()`.",
57-
sep = ""
58-
)
59-
60-
if (action == "abort") {
61-
rlang::abort(msg_basic)
62-
} else {
63-
rlang::warn(paste0(
64-
msg_basic, " ",
65-
"Deactivating the caching feature for the current session."
66-
))
67-
cache_deactivate(verbose = FALSE)
68-
}
69-
}
70-
}

R/transform-files.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,9 @@ make_transformer <- function(transformers,
7878
warn_empty = TRUE) {
7979
force(transformers)
8080
assert_transformers(transformers)
81-
assert_R.cache_installation(action = "warn")
82-
83-
is_R.cache_installed <- rlang::is_installed("R.cache")
8481

8582
function(text) {
86-
should_use_cache <- is_R.cache_installed && cache_is_activated()
83+
should_use_cache <- cache_is_activated()
8784

8885
if (should_use_cache) {
8986
use_cache <- is_cached(text, transformers)

R/ui-caching.R

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#' @family cache managers
1515
#' @export
1616
cache_clear <- function(cache_name = NULL, ask = TRUE) {
17-
assert_R.cache_installation(installation_only = TRUE)
1817
path_cache <- cache_find_path(cache_name)
1918
R.cache::clearCache(path_cache, prompt = ask)
2019
cache_deactivate(verbose = FALSE)
@@ -23,15 +22,45 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) {
2322

2423
#' Remember the past to be quicker in the future
2524
#'
26-
#' styler by default uses caching. It may prompt you to install the R package
27-
#' `R.cache` the first time you want to use it. R.cache will also ask you to let
28-
#' it create a permanent cache on your file system that styler will use.
29-
#' This is needed if you want to cache across R sessions and not just within.
25+
#' Caching makes styler faster on repeated styling. It does not cache input
26+
#' but output code. That means if you style code that already complies to a
27+
#' style guide and you have previously styled that code, it will be quicker.
28+
#' Code is cached by expression and the cache is shared across all APIs (e.g.
29+
#' `style_text()` and Addin.
30+
#'
31+
#' @section Setup:
32+
#' styler by default uses caching, via the `{R.cache}` package. You will be
33+
#' asked you to let it create a permanent cache on your file system that styler
34+
#' will use in case it is not set that up already for another tool that
35+
#' uses `{R.cache}`. We encourage users to let `{R.cache}` create a permanent
36+
#' directory for caching, because otherwise, the cache is lost at restart of R.
37+
#'
38+
#' @section Non-interactive use:
39+
#' Note that if you have never authorised `{R.cache}` to create the cache in a
40+
#' permenent directory, it wil build the cache in a temporary directory. To
41+
#' create a permenent cache, just open an interactive R session and type
42+
#' `cache_info()`. You can see under `Location:` if a permanent directory is
43+
#' used and if not, `{R.cache}` will ask you to create one the first time you
44+
#' use `{R.cache}` in an R session.
45+
#'
46+
#' @section Invalidation:
3047
#' The cache is specific to a version of styler by default, because different
3148
#' versions potentially format code differently. This means after upgrading
3249
#' styler or a style guide you use, the cache will be re-built.
50+
#'
51+
#' @section Manage the cache:
3352
#' See [cache_info()],
34-
#' [cache_activate()], [cache_clear()] for utilities to manage the cache.
53+
#' [cache_activate()], [cache_clear()] for utilities to manage the cache. Since
54+
#' we leverage `{R.cache}` to manage the cache, you can also use any `{R.cache}`
55+
#' functionality to manipulate it.
56+
#'
57+
#' @section Using a cache for styler in CI/CD:
58+
#' If you want to set up caching in a CI/CD pipeline, we suggest to set the
59+
#' `{R.cache}` root path to a directory for which you have the cache enabled.
60+
#' The former can be done with `R.cache::setCacheRootPath("/path/to/cache")`,
61+
#' the latter can often be set in config files of CI/CD tools, e.g. see the
62+
#' the [Travis documentation on caching](https://docs.travis-ci.com/user/caching).
63+
#'
3564
#' @name caching
3665
NULL
3766

@@ -50,7 +79,6 @@ NULL
5079
#' @family cache managers
5180
#' @export
5281
cache_info <- function(cache_name = NULL, format = "both") {
53-
assert_R.cache_installation(installation_only = TRUE)
5482
rlang::arg_match(format, c("tabular", "lucid", "both"))
5583
path_cache <- cache_find_path(cache_name)
5684
files <- list.files(path_cache, full.names = TRUE)
@@ -92,7 +120,6 @@ cache_info <- function(cache_name = NULL, format = "both") {
92120
#' @family cache managers
93121
#' @export
94122
cache_activate <- function(cache_name = NULL, verbose = TRUE) {
95-
assert_R.cache_installation(installation_only = TRUE)
96123
if (!is.null(cache_name)) {
97124
options("styler.cache_name" = cache_name)
98125
} else {

README.Rmd

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@ version](https://www.r-pkg.org/badges/version/styler)](https://cran.r-project.or
2727

2828
The goal of styler is to provide non-invasive pretty-printing of R source code
2929
while adhering to the [tidyverse](https://style.tidyverse.org) formatting rules.
30-
styler can be customized to format code according to other style guides too.
30+
styler can be customized to format code according to other style guides too.
3131

3232
The following online docs are available:
3333

3434
- [latest CRAN release](https://styler.r-lib.org).
3535

3636
- [GitHub development version](https://styler.r-lib.org/dev).
3737

38-
3938
## Installation
4039

41-
You can install the package from CRAN:
40+
You can install the package from CRAN.
4241

4342
```{r, eval = FALSE}
4443
install.packages("styler")
4544
```
4645

46+
If you don't use styler interactively (i.e. not from the R prompt or Rstudio
47+
Addin), make sure you authorise `{R.cache}` once to set up a permanent cache.
48+
If you use it interactively, you will be asked to grant this permission once.
49+
See `?caching` for details.
50+
51+
4752
Or get the development version from GitHub:
4853

4954
```{r, eval = FALSE}
@@ -76,20 +81,6 @@ There are a few variants of `style_text()`:
7681
knitr::include_graphics("https://raw.githubusercontent.com/lorenzwalthert/some_raw_data/master/styler_0.1.gif")
7782
```
7883

79-
## Features
80-
81-
* style roxygen2 code examples.
82-
83-
* do not re-style [deliberate code
84-
alignment](https://styler.r-lib.org/articles/detect-alignment.html).
85-
86-
* [ignore some lines](https://styler.r-lib.org/reference/stylerignore.html) for
87-
styling (GitHub development version).
88-
89-
* [cache styled expressions](https://styler.r-lib.org/reference/caching.html)
90-
for speed (GitHub development version).
91-
92-
9384
## Configuration
9485

9586
You can decide on the level of invasiveness with the scope argument. You can
@@ -116,11 +107,24 @@ Note that compared to the default used above `scope = "tokens"`:
116107

117108
While spaces still got styled (around `=` in `(x)`).
118109

119-
This was just the tip of the iceberg. To learn more about customization options
120-
with the tidyverse style guide, see the [help file for `tidyverse_style](https://styler.r-lib.org/reference/tidyverse_style.html) for a
121-
quick overview or the
122-
[introductory vignette](https://styler.r-lib.org/articles/introducing_styler.html).
110+
This was just the tip of the iceberg. To learn more about customization options
111+
with the tidyverse style guide, see the [help file for
112+
`tidyverse_style](https://styler.r-lib.org/reference/tidyverse_style.html) for a
113+
quick overview or the [introductory
114+
vignette](https://styler.r-lib.org/articles/introducing_styler.html).
115+
116+
## Features
117+
118+
* style roxygen2 code examples.
119+
120+
* do not re-style [deliberate code
121+
alignment](https://styler.r-lib.org/articles/detect-alignment.html).
122+
123+
* [ignore some lines](https://styler.r-lib.org/dev/reference/stylerignore.html)
124+
for styling.
123125

126+
* [cache styled
127+
expressions](https://styler.r-lib.org/dev/reference/caching.html) for speed.
124128

125129
## Adaption of styler
126130

0 commit comments

Comments
 (0)