From 6b4fc02bfd19aaab64137da2e7baac459455aa2b Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 18:49:33 +0100 Subject: [PATCH 01/10] actually remove R.cache in the right place. --- .travis.yml | 2 +- tic.R | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b807f9d6e..9ca389c5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ # Usually you shouldn't need to change the first part of the file # DO NOT CHANGE THE CODE BELOW -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()' +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()' install: R -q -e 'tic::install()' after_install: R -q -e 'tic::after_install()' before_script: R -q -e 'tic::before_script()' diff --git a/tic.R b/tic.R index c9af2aacf..446b47242 100644 --- a/tic.R +++ b/tic.R @@ -1,3 +1,11 @@ +get_stage("after_install") %>% + add_code_step( + { + if (isTRUE(as.logical(toupper(Sys.getenv("R_REMOVE_RCACHE"))))) { + remove.packages("R.cache") + } + } + ) do_package_checks(error_on = ifelse(getRversion() >= "3.2", "note", "error")) if (Sys.getenv("id_rsa") != "" && ci()$get_branch() == "master" && Sys.getenv("BUILD_PKGDOWN") != "") { From c4f79d26b1b902969db5badeed45d0986f911d84 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 18:54:45 +0100 Subject: [PATCH 02/10] make sure that when cache is deactivated and R.cache is installed but there is no R.cache premanent directory, the user is never asked about setting up a permanent directory. --- R/communicate.R | 9 ++------ R/transform-files.R | 6 ++---- R/ui-caching.R | 6 +++--- man/assert_R.cache_installation.Rd | 6 +----- tests/testthat/test-cache-without-r-cache.R | 23 ++++++++++++++++----- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/R/communicate.R b/R/communicate.R index 344673f99..ca9d54eb8 100644 --- a/R/communicate.R +++ b/R/communicate.R @@ -38,15 +38,10 @@ assert_data.tree_installation <- function() { #' Assert the R.cache installation in conjunction with the cache config #' #' R.cache needs to be installed if caching functionality is enabled -#' @param installation_only Whether or not to only check if R.cache is -#' installed. #' @keywords internal -assert_R.cache_installation <- function(installation_only = FALSE, - action = "abort") { +assert_R.cache_installation <- function(action = "abort") { # fail if R.cache is not installed but feature is actiavted. - if (!rlang::is_installed("R.cache") && - ifelse(installation_only, TRUE, cache_is_activated()) - ) { + if (!rlang::is_installed("R.cache")) { msg_basic <- paste( "R package R.cache is not installed, which is needed when the caching ", "feature is activated. Please install the package with ", diff --git a/R/transform-files.R b/R/transform-files.R index bbb305de8..7a19a7383 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -78,12 +78,10 @@ make_transformer <- function(transformers, warn_empty = TRUE) { force(transformers) assert_transformers(transformers) - assert_R.cache_installation(action = "warn") - - is_R.cache_installed <- rlang::is_installed("R.cache") + if (cache_is_activated()) assert_R.cache_installation(action = "warn") function(text) { - should_use_cache <- is_R.cache_installed && cache_is_activated() + should_use_cache <- cache_is_activated() && rlang::is_installed("R.cache") if (should_use_cache) { use_cache <- is_cached(text, transformers) diff --git a/R/ui-caching.R b/R/ui-caching.R index dabd5ad41..379152727 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -14,7 +14,7 @@ #' @family cache managers #' @export cache_clear <- function(cache_name = NULL, ask = TRUE) { - assert_R.cache_installation(installation_only = TRUE) + assert_R.cache_installation() path_cache <- cache_find_path(cache_name) R.cache::clearCache(path_cache, prompt = ask) cache_deactivate(verbose = FALSE) @@ -50,7 +50,7 @@ NULL #' @family cache managers #' @export cache_info <- function(cache_name = NULL, format = "both") { - assert_R.cache_installation(installation_only = TRUE) + assert_R.cache_installation() rlang::arg_match(format, c("tabular", "lucid", "both")) path_cache <- cache_find_path(cache_name) files <- list.files(path_cache, full.names = TRUE) @@ -92,7 +92,7 @@ cache_info <- function(cache_name = NULL, format = "both") { #' @family cache managers #' @export cache_activate <- function(cache_name = NULL, verbose = TRUE) { - assert_R.cache_installation(installation_only = TRUE) + assert_R.cache_installation() if (!is.null(cache_name)) { options("styler.cache_name" = cache_name) } else { diff --git a/man/assert_R.cache_installation.Rd b/man/assert_R.cache_installation.Rd index 61f812c95..f06c89b05 100644 --- a/man/assert_R.cache_installation.Rd +++ b/man/assert_R.cache_installation.Rd @@ -4,11 +4,7 @@ \alias{assert_R.cache_installation} \title{Assert the R.cache installation in conjunction with the cache config} \usage{ -assert_R.cache_installation(installation_only = FALSE, action = "abort") -} -\arguments{ -\item{installation_only}{Whether or not to only check if R.cache is -installed.} +assert_R.cache_installation(action = "abort") } \description{ R.cache needs to be installed if caching functionality is enabled diff --git a/tests/testthat/test-cache-without-r-cache.R b/tests/testthat/test-cache-without-r-cache.R index 7a70ce928..07831f11a 100644 --- a/tests/testthat/test-cache-without-r-cache.R +++ b/tests/testthat/test-cache-without-r-cache.R @@ -4,9 +4,8 @@ test_that("Cache management fails mostly when R.cache is not installed", { expect_error(activate_testthat_cache(), "is needed when the caching feature is activated") expect_error(cache_clear("testthat"), "is needed when the caching feature is activated") expect_error(capture.output(cache_deactivate()), NA) - expect_silent(assert_R.cache_installation()) expect_error( - assert_R.cache_installation(installation_only = TRUE), + assert_R.cache_installation(), "is needed when the caching feature is activated" ) }) @@ -14,23 +13,37 @@ test_that("Cache management fails mostly when R.cache is not installed", { test_that("styling works when R.cache is not installed", { skip_if(rlang::is_installed("R.cache")) + # no warning when cache is disabled manually + expect_output( + withr::with_options( + # simulate .onLoad() in fresh R session + list(styler.cache_name = styler_version), + { + cache_deactivate() + style_text("1+1") + } + ), + "Deactivated cache." + ) + + # warning if cache is not disabled manually # warning for first time expect_warning( capture.output( withr::with_options( # simulate .onLoad() in fresh R session - list(styler.cache_name = cache_derive_name()), + list(styler.cache_name = styler_version), style_text("1+1") ) ), - "Deactivating the caching feature for the current session" + "R package R.cache .*Deactivating the caching feature for the current session" ) # No warnings subsequently expect_warning( capture.output( withr::with_options( - list(styler.cache_name = cache_derive_name()), { + list(styler.cache_name = styler_version), { suppressWarnings(style_text("1+1")) style_text("1+1") } From 74d7b342d41a9c54fae703df4a4657b0ec39029d Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 19:10:51 +0100 Subject: [PATCH 03/10] fix test --- tests/testthat/test-cache-with-r-cache.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-cache-with-r-cache.R b/tests/testthat/test-cache-with-r-cache.R index 1a35f3658..1ceceff6e 100644 --- a/tests/testthat/test-cache-with-r-cache.R +++ b/tests/testthat/test-cache-with-r-cache.R @@ -2,12 +2,10 @@ capture.output(test_that("No warnings are issued when R.cache is installed", { skip_if_not_installed("R.cache") on.exit(clear_testthat_cache()) - expect_silent(assert_R.cache_installation(installation_only = TRUE)) expect_silent(assert_R.cache_installation()) expect_warning(style_text("1+1"), NA) expect_warning(activate_testthat_cache(), NA) expect_warning(style_text("1+1"), NA) - expect_silent(assert_R.cache_installation(installation_only = TRUE)) expect_silent(assert_R.cache_installation()) })) From 34c9c0a271dc626d8204a07f38914358a253e312 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 19:53:27 +0100 Subject: [PATCH 04/10] better documentation --- R/ui-caching.R | 29 ++++++++++++++++++++++++++++- man/caching.Rd | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/R/ui-caching.R b/R/ui-caching.R index 379152727..8a2abc947 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -23,15 +23,42 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' Remember the past to be quicker in the future #' +#' Caching makes styler faster on repeated styling. It does not cache input +#' but output code. That means if you style code that already complies to a +#' style guide and you have previously styled that code, it will be quicker. +#' Code is cached by expression and the cache is shared across all APIs. +#' +#' @section Setup: #' styler by default uses caching. It may prompt you to install the R package #' `R.cache` the first time you want to use it. R.cache will also ask you to let #' it create a permanent cache on your file system that styler will use. #' This is needed if you want to cache across R sessions and not just within. +#' +#' @section Non-interactive use: +#' Note that if you have never authorized `{R.cache}` to create the cache in a +#' permenent directory, it wil build the cache in a temporary directory. To +#' create a permenent cache, just open an interactive R session and type +#' `cache_info()`. You can see under `Location:` if a permanent directory is +#' used and if not, `{R.cache}` will ask you to create one. +#' +#' @section Invalidation: #' The cache is specific to a version of styler by default, because different #' versions potentially format code differently. This means after upgrading #' styler or a style guide you use, the cache will be re-built. +#' +#' @section Manage the cache: #' See [cache_info()], -#' [cache_activate()], [cache_clear()] for utilities to manage the cache. +#' [cache_activate()], [cache_clear()] for utilities to manage the cache. Since +#' we leverage `{R.cache}` to manage the cache, you can also use any `{R.cache}` +#' functionality to manipulate it. +#' +#' @section Using a cache for styler in CI/CD: +#' If you want to set up caching in a CI/CD pipeline, we suggest to set the +#' `{R.cache}` root path to a directory for which you have the cache enabled. +#' The former can be done with `R.cache::setCacheRootPath("/path/to/cache")`, +#' the latter can often be set in config files of CI/CD tools, e.g. see the +#' the [Travis documentation on caching](https://docs.travis-ci.com/user/caching). +#' #' @name caching NULL diff --git a/man/caching.Rd b/man/caching.Rd index 161be2076..579af50c7 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -4,13 +4,49 @@ \alias{caching} \title{Remember the past to be quicker in the future} \description{ +Caching makes styler faster on repeated styling. It does not cache input +but output code. That means if you style code that already complies to a +style guide and you have previously styled that code, it will be quicker. +Code is cached by expression and the cache is shared across all APIs. +} +\section{Setup}{ + styler by default uses caching. It may prompt you to install the R package \code{R.cache} the first time you want to use it. R.cache will also ask you to let it create a permanent cache on your file system that styler will use. This is needed if you want to cache across R sessions and not just within. +} + +\section{Non-interactive use}{ + +Note that if you have never authorized \code{{R.cache}} to create the cache in a +permenent directory, it wil build the cache in a temporary directory. To +create a permenent cache, just open an interactive R session and type +\code{cache_info()}. You can see under \verb{Location:} if a permanent directory is +used and if not, \code{{R.cache}} will ask you to create one. +} + +\section{Invalidation}{ + The cache is specific to a version of styler by default, because different versions potentially format code differently. This means after upgrading styler or a style guide you use, the cache will be re-built. +} + +\section{Manage the cache}{ + See \code{\link[=cache_info]{cache_info()}}, -\code{\link[=cache_activate]{cache_activate()}}, \code{\link[=cache_clear]{cache_clear()}} for utilities to manage the cache. +\code{\link[=cache_activate]{cache_activate()}}, \code{\link[=cache_clear]{cache_clear()}} for utilities to manage the cache. Since +we leverage \code{{R.cache}} to manage the cache, you can also use any \code{{R.cache}} +functionality to manipulate it. +} + +\section{Using a cache for styler in CI/CD}{ + +If you want to set up caching in a CI/CD pipeline, we suggest to set the +\code{{R.cache}} root path to a directory for which you have the cache enabled. +The former can be done with \code{R.cache::setCacheRootPath("/path/to/cache")}, +the latter can often be set in config files of CI/CD tools, e.g. see the +the \href{https://docs.travis-ci.com/user/caching}{Travis documentation on caching}. } + From 6fbf794f76c17f6c5b9dd7dd3d5390aaa59af5aa Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 20:00:27 +0100 Subject: [PATCH 05/10] udpate readme doc --- README.Rmd | 42 +++++++++++++++++++++++++++--------------- README.md | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/README.Rmd b/README.Rmd index 4a5ca3b39..921c6b285 100644 --- a/README.Rmd +++ b/README.Rmd @@ -38,12 +38,22 @@ The following online docs are available: ## Installation -You can install the package from CRAN: +You can install the package from CRAN. ```{r, eval = FALSE} install.packages("styler") + +# if you want to benefit from caching, styler will ask you to install R.cache +# if you don't have it already +install.packages("R.cache") + +# If you just installed R.cache, you need to authorise it to create a permanent +# caching directory on your computer once. +cache_info() # then type 'y' for Yes. ``` + + Or get the development version from GitHub: ```{r, eval = FALSE} @@ -51,6 +61,9 @@ Or get the development version from GitHub: remotes::install_github("r-lib/styler") ``` +And set up caching the same way as for the CRAN version. + + ## API You can style a simple character vector of code with `style_text()`: @@ -76,20 +89,6 @@ There are a few variants of `style_text()`: knitr::include_graphics("https://raw.githubusercontent.com/lorenzwalthert/some_raw_data/master/styler_0.1.gif") ``` -## Features - -* style roxygen2 code examples. - -* do not re-style [deliberate code - alignment](https://styler.r-lib.org/articles/detect-alignment.html). - -* [ignore some lines](https://styler.r-lib.org/reference/stylerignore.html) for - styling (GitHub development version). - -* [cache styled expressions](https://styler.r-lib.org/reference/caching.html) - for speed (GitHub development version). - - ## Configuration You can decide on the level of invasiveness with the scope argument. You can @@ -121,6 +120,19 @@ with the tidyverse style guide, see the [help file for `tidyverse_style](https:/ quick overview or the [introductory vignette](https://styler.r-lib.org/articles/introducing_styler.html). +## Features + +* style roxygen2 code examples. + +* do not re-style [deliberate code + alignment](https://styler.r-lib.org/articles/detect-alignment.html). + +* [ignore some lines](https://styler.r-lib.org/dev/reference/stylerignore.html) for + styling. + +* [cache styled expressions](https://styler.r-lib.org/dev/reference/caching.html) + for speed. + ## Adaption of styler diff --git a/README.md b/README.md index 4816e8e9e..31d9404be 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,18 @@ The following online docs are available: ## Installation -You can install the package from CRAN: +You can install the package from CRAN. ``` r install.packages("styler") + +# if you want to benefit from caching, styler will ask you to install R.cache +# if you don't have it already +install.packages("R.cache") + +# If you just installed R.cache, you need to authorise it to create a permanent +# caching directory on your computer once. +cache_info() # then type 'y' for Yes. ``` Or get the development version from GitHub: @@ -38,6 +46,8 @@ Or get the development version from GitHub: remotes::install_github("r-lib/styler") ``` +And set up caching the same way as for the CRAN version. + ## API You can style a simple character vector of code with `style_text()`: @@ -65,21 +75,6 @@ region. -## Features - - - style roxygen2 code examples. - - - do not re-style [deliberate code - alignment](https://styler.r-lib.org/articles/detect-alignment.html). - - - [ignore some - lines](https://styler.r-lib.org/reference/stylerignore.html) for - styling (GitHub development version). - - - [cache styled - expressions](https://styler.r-lib.org/reference/caching.html) for - speed (GitHub development version). - ## Configuration You can decide on the level of invasiveness with the scope argument. You @@ -115,6 +110,21 @@ options with the tidyverse style guide, see the [help file for for a quick overview or the [introductory vignette](https://styler.r-lib.org/articles/introducing_styler.html). +## Features + + - style roxygen2 code examples. + + - do not re-style [deliberate code + alignment](https://styler.r-lib.org/articles/detect-alignment.html). + + - [ignore some + lines](https://styler.r-lib.org/dev/reference/stylerignore.html) for + styling. + + - [cache styled + expressions](https://styler.r-lib.org/dev/reference/caching.html) + for speed. + ## Adaption of styler styler functionality is made available through other tools, most notably From 02bf622cfb38f84ef357c6bef22bbb3b05e6e436 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 20:03:45 +0100 Subject: [PATCH 06/10] style NEWS, add missing parts --- NEWS.md | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1ce334118..17132626a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,47 +1,48 @@ - -# styler 1.2.0.9000 +# styler 1.2.9001 ## Breaking changes -* `style_pkg()` and `style_dir()` gain a new argument `exclude_dirs` to exclude - directories from styling, by default `renv` and `packrat`. Note that the - defaults won't change the behavior of `style_pkg()` because it does anyways +* `style_pkg()` and `style_dir()` gain a new argument `exclude_dirs` to exclude + directories from styling, by default `renv` and `packrat`. Note that the + defaults won't change the behavior of `style_pkg()` because it does anyways does not style these directories and they were set for consistency. - -* `style_file()` and friends now strip `./` in file paths returned invisibly, + +* `style_file()` and friends now strip `./` in file paths returned invisibly, i.e. `./script.R` becomes `script.R` (#568). ## New features -* ignore certain lines using `# styler: off` and `#styler: on` or custom - markers, see `help("stylerignore")` (#560). +* ignore certain lines using `# styler: off` and `#styler: on` or custom + markers, see `?stylerignore` (#560). * styler caches results of styling, so applying styler to code it has styled before will be instantaneous. This brings large speed boosts in many situations, e.g. when `style_pkg()` is run but only a few files have changed since the last styling or when using the [styler pre-commit - hook](https://github.com/lorenzwalthert/precommit). Because styler caches - by expression, you will also get speed boosts in large files with many - expressions when you only change a few o them. See `help("caching")` for - details (#538, #578). - -* `create_style_guide()` gains two arguments `style_guide_name` and - `style_guide_version` that are carried as meta data, in particular to version - third-party style guides and ensure the proper functioning of caching. This + hook](https://github.com/lorenzwalthert/precommit). Because styler caches by + expression, you will also get speed boosts in large files with many + expressions when you only change a few of them. See `?caching` for details + (#538, #578). + +* `create_style_guide()` gains two arguments `style_guide_name` and + `style_guide_version` that are carried as meta data, in particular to version + third-party style guides and ensure the proper functioning of caching. This change is completely invisible to users who don't create and distribute their own style guide like `tidyverse_style()` (#572). - ## Minor changes and fixes * lines are now broken after `+` in `ggplot2` calls for `strict = TRUE` (#569). * function documentation now contains many more line breaks due to roxygen2 update to version 7.0.1 (#566). - + * spaces next to the braces in subsetting expressions `[` and `[[` are now removed (#580). +* Adapt to changes in the R parser to make styler pass R CMD check again. + (#583). + # styler 1.2.0 ## Breaking changes @@ -51,17 +52,17 @@ This is also reflected in the invisible return value of the function (#522). * `style_file()` and friends do not write content back to a file when styling - does not cause any changes in the file. This means the modification date of + does not cause any changes in the file. This means the modification date of styled files is only changed when the content is changed (#532). ## New features -* Aligned function calls are detected and remain unchanged if they match the styler - [definition for aligned function +* Aligned function calls are detected and remain unchanged if they match the + styler [definition for aligned function calls](https://styler.r-lib.org/articles/detect-alignment.html) (#537). -* curly-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now - explicitly handled, where previously it was just treated as two consecutive +* curly-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now + explicitly handled, where previously it was just treated as two consecutive curly braces (#528). * `style_pkg()`, `style_dir()` and the Addins can now style `.Rprofile`, and @@ -69,15 +70,15 @@ ## Minor improvements and fixes -* Brace expressions in function calls are formatted in a less compact way to +* Brace expressions in function calls are formatted in a less compact way to improve readability. Typical use case: `tryCatch()` (#543). -* Arguments in function declarations in a context which is indented multiple +* Arguments in function declarations in a context which is indented multiple times should now be correct. This typically affects `R6::R6Class()` (#546). * Escape characters in roxygen code examples are now correctly escaped (#512). -* Special characters such as `\n` in strings are now preserved in text and not +* Special characters such as `\n` in strings are now preserved in text and not turned into literal values like a line break (#554). * Style selection Addin now preserves line break when the last line selected is From 8d856a59e604d3b5b42949237d47685a4d2316ee Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 9 Feb 2020 20:24:59 +0100 Subject: [PATCH 07/10] simplify things, just let the user follow the instructions on the prompt. --- README.Rmd | 37 ++++++++++++++----------------------- README.md | 15 +++++---------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/README.Rmd b/README.Rmd index 921c6b285..1970e036c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -27,7 +27,7 @@ version](https://www.r-pkg.org/badges/version/styler)](https://cran.r-project.or The goal of styler is to provide non-invasive pretty-printing of R source code while adhering to the [tidyverse](https://style.tidyverse.org) formatting rules. -styler can be customized to format code according to other style guides too. +styler can be customized to format code according to other style guides too. The following online docs are available: @@ -35,24 +35,18 @@ The following online docs are available: - [GitHub development version](https://styler.r-lib.org/dev). - ## Installation You can install the package from CRAN. ```{r, eval = FALSE} install.packages("styler") - -# if you want to benefit from caching, styler will ask you to install R.cache -# if you don't have it already -install.packages("R.cache") - -# If you just installed R.cache, you need to authorise it to create a permanent -# caching directory on your computer once. -cache_info() # then type 'y' for Yes. ``` - +You will be prompted to decide on how to use the [caching +feature](https://styler.r-lib.org/dev/reference/caching.html). See `?caching` +for details, in particular when you don't use styler interactively (i.e. not +from the R prompt or Rstudio Addin, ) Or get the development version from GitHub: @@ -61,9 +55,6 @@ Or get the development version from GitHub: remotes::install_github("r-lib/styler") ``` -And set up caching the same way as for the CRAN version. - - ## API You can style a simple character vector of code with `style_text()`: @@ -115,10 +106,11 @@ Note that compared to the default used above `scope = "tokens"`: While spaces still got styled (around `=` in `(x)`). -This was just the tip of the iceberg. To learn more about customization options -with the tidyverse style guide, see the [help file for `tidyverse_style](https://styler.r-lib.org/reference/tidyverse_style.html) for a -quick overview or the -[introductory vignette](https://styler.r-lib.org/articles/introducing_styler.html). +This was just the tip of the iceberg. To learn more about customization options +with the tidyverse style guide, see the [help file for +`tidyverse_style](https://styler.r-lib.org/reference/tidyverse_style.html) for a +quick overview or the [introductory +vignette](https://styler.r-lib.org/articles/introducing_styler.html). ## Features @@ -127,12 +119,11 @@ quick overview or the * do not re-style [deliberate code alignment](https://styler.r-lib.org/articles/detect-alignment.html). -* [ignore some lines](https://styler.r-lib.org/dev/reference/stylerignore.html) for - styling. - -* [cache styled expressions](https://styler.r-lib.org/dev/reference/caching.html) - for speed. +* [ignore some lines](https://styler.r-lib.org/dev/reference/stylerignore.html) + for styling. +* [cache styled + expressions](https://styler.r-lib.org/dev/reference/caching.html) for speed. ## Adaption of styler diff --git a/README.md b/README.md index 31d9404be..a383dd06c 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,13 @@ You can install the package from CRAN. ``` r install.packages("styler") - -# if you want to benefit from caching, styler will ask you to install R.cache -# if you don't have it already -install.packages("R.cache") - -# If you just installed R.cache, you need to authorise it to create a permanent -# caching directory on your computer once. -cache_info() # then type 'y' for Yes. ``` +You will be prompted to decide on how to use the [caching +feature](https://styler.r-lib.org/dev/reference/caching.html). See +`?caching` for details, in particular when you don’t use styler +interactively (i.e. not from the R prompt or Rstudio Addin, ) + Or get the development version from GitHub: ``` r @@ -46,8 +43,6 @@ Or get the development version from GitHub: remotes::install_github("r-lib/styler") ``` -And set up caching the same way as for the CRAN version. - ## API You can style a simple character vector of code with `style_text()`: From 7332ac53c303abd4442b9126079e16ee51636c9f Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Mon, 10 Feb 2020 20:44:29 +0100 Subject: [PATCH 08/10] make R.cache an import This remove one step in the styler setup for caching. Now, the only thing an interactive user has to do is to allow R.cache to set up a permanent cache. I believe this is worth the three extra recursive dependencies because there is actually no reason why you are strictly against using caching, even on CI/CD it will speed up things. --- .travis.yml | 3 -- DESCRIPTION | 2 +- R/communicate.R | 29 ----------- R/transform-files.R | 3 +- R/ui-caching.R | 16 +++--- man/assert_R.cache_installation.Rd | 12 ----- man/caching.Rd | 13 +++-- tests/testthat/test-cache-high-level-api.R | 12 ++--- tests/testthat/test-cache-with-r-cache.R | 16 +----- tests/testthat/test-cache-without-r-cache.R | 54 --------------------- tic.R | 8 --- 11 files changed, 24 insertions(+), 144 deletions(-) delete mode 100644 man/assert_R.cache_installation.Rd delete mode 100644 tests/testthat/test-cache-without-r-cache.R diff --git a/.travis.yml b/.travis.yml index 9ca389c5a..68273b8ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,9 +38,6 @@ matrix: - r: release env: - BUILD_PKGDOWN: true - - r: release - env: - - R_REMOVE_RCACHE: true - r: devel #env diff --git a/DESCRIPTION b/DESCRIPTION index ce13fc8b2..0c3a819e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,6 +21,7 @@ Imports: cli (>= 1.1.0), magrittr (>= 1.0.1), purrr (>= 0.2.3), + R.cache (>= 0.14.0), rematch2 (>= 2.0.1), rlang (>= 0.1.1), rprojroot (>= 1.1), @@ -35,7 +36,6 @@ Suggests: here, knitr, prettycode, - R.cache (>= 0.14.0), rmarkdown, rstudioapi (>= 0.7), testthat (>= 2.1.0) diff --git a/R/communicate.R b/R/communicate.R index ca9d54eb8..3e9328b8a 100644 --- a/R/communicate.R +++ b/R/communicate.R @@ -34,32 +34,3 @@ assert_data.tree_installation <- function() { abort("The package data.tree needs to be installed for this functionality.") } } - -#' Assert the R.cache installation in conjunction with the cache config -#' -#' R.cache needs to be installed if caching functionality is enabled -#' @keywords internal -assert_R.cache_installation <- function(action = "abort") { - # fail if R.cache is not installed but feature is actiavted. - if (!rlang::is_installed("R.cache")) { - msg_basic <- paste( - "R package R.cache is not installed, which is needed when the caching ", - "feature is activated. Please install the package with ", - "`install.packages('R.cache')` and then restart R to enable the ", - "caching feature of styler or permanently deactivate the feature by ", - "adding `styler::cache_deactivate()` to your .Rprofile, e.g. via ", - "`usethis::edit_r_profile()`.", - sep = "" - ) - - if (action == "abort") { - rlang::abort(msg_basic) - } else { - rlang::warn(paste0( - msg_basic, " ", - "Deactivating the caching feature for the current session." - )) - cache_deactivate(verbose = FALSE) - } - } -} diff --git a/R/transform-files.R b/R/transform-files.R index 7a19a7383..68e44f428 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -78,10 +78,9 @@ make_transformer <- function(transformers, warn_empty = TRUE) { force(transformers) assert_transformers(transformers) - if (cache_is_activated()) assert_R.cache_installation(action = "warn") function(text) { - should_use_cache <- cache_is_activated() && rlang::is_installed("R.cache") + should_use_cache <- cache_is_activated() if (should_use_cache) { use_cache <- is_cached(text, transformers) diff --git a/R/ui-caching.R b/R/ui-caching.R index 8a2abc947..8201da5c4 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -14,7 +14,6 @@ #' @family cache managers #' @export cache_clear <- function(cache_name = NULL, ask = TRUE) { - assert_R.cache_installation() path_cache <- cache_find_path(cache_name) R.cache::clearCache(path_cache, prompt = ask) cache_deactivate(verbose = FALSE) @@ -26,12 +25,14 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' Caching makes styler faster on repeated styling. It does not cache input #' but output code. That means if you style code that already complies to a #' style guide and you have previously styled that code, it will be quicker. -#' Code is cached by expression and the cache is shared across all APIs. +#' Code is cached by expression and the cache is shared across all APIs (e.g. +#' `style_text()` and Addin. #' #' @section Setup: -#' styler by default uses caching. It may prompt you to install the R package -#' `R.cache` the first time you want to use it. R.cache will also ask you to let -#' it create a permanent cache on your file system that styler will use. +#' styler by default uses caching, via the `{R.cache}` package. You will be +#' asked you to let it create a permanent cache on your file system that styler +#' will use in case you have not set that up already for another tool that +#' uses `{R.cache}`. #' This is needed if you want to cache across R sessions and not just within. #' #' @section Non-interactive use: @@ -39,7 +40,8 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' permenent directory, it wil build the cache in a temporary directory. To #' create a permenent cache, just open an interactive R session and type #' `cache_info()`. You can see under `Location:` if a permanent directory is -#' used and if not, `{R.cache}` will ask you to create one. +#' used and if not, `{R.cache}` will ask you to create one the first time you +#' use `{R.cache}` in an R session. #' #' @section Invalidation: #' The cache is specific to a version of styler by default, because different @@ -77,7 +79,6 @@ NULL #' @family cache managers #' @export cache_info <- function(cache_name = NULL, format = "both") { - assert_R.cache_installation() rlang::arg_match(format, c("tabular", "lucid", "both")) path_cache <- cache_find_path(cache_name) files <- list.files(path_cache, full.names = TRUE) @@ -119,7 +120,6 @@ cache_info <- function(cache_name = NULL, format = "both") { #' @family cache managers #' @export cache_activate <- function(cache_name = NULL, verbose = TRUE) { - assert_R.cache_installation() if (!is.null(cache_name)) { options("styler.cache_name" = cache_name) } else { diff --git a/man/assert_R.cache_installation.Rd b/man/assert_R.cache_installation.Rd deleted file mode 100644 index f06c89b05..000000000 --- a/man/assert_R.cache_installation.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/communicate.R -\name{assert_R.cache_installation} -\alias{assert_R.cache_installation} -\title{Assert the R.cache installation in conjunction with the cache config} -\usage{ -assert_R.cache_installation(action = "abort") -} -\description{ -R.cache needs to be installed if caching functionality is enabled -} -\keyword{internal} diff --git a/man/caching.Rd b/man/caching.Rd index 579af50c7..0a445af3a 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -7,13 +7,15 @@ Caching makes styler faster on repeated styling. It does not cache input but output code. That means if you style code that already complies to a style guide and you have previously styled that code, it will be quicker. -Code is cached by expression and the cache is shared across all APIs. +Code is cached by expression and the cache is shared across all APIs (e.g. +\code{style_text()} and Addin. } \section{Setup}{ -styler by default uses caching. It may prompt you to install the R package -\code{R.cache} the first time you want to use it. R.cache will also ask you to let -it create a permanent cache on your file system that styler will use. +styler by default uses caching, via the \code{{R.cache}} package. You will be +asked you to let it create a permanent cache on your file system that styler +will use in case you have not set that up already for another tool that +uses \code{{R.cache}}. This is needed if you want to cache across R sessions and not just within. } @@ -23,7 +25,8 @@ Note that if you have never authorized \code{{R.cache}} to create the cache in a permenent directory, it wil build the cache in a temporary directory. To create a permenent cache, just open an interactive R session and type \code{cache_info()}. You can see under \verb{Location:} if a permanent directory is -used and if not, \code{{R.cache}} will ask you to create one. +used and if not, \code{{R.cache}} will ask you to create one the first time you +use \code{{R.cache}} in an R session. } \section{Invalidation}{ diff --git a/tests/testthat/test-cache-high-level-api.R b/tests/testthat/test-cache-high-level-api.R index 5e9dd9eb2..3daaf1091 100644 --- a/tests/testthat/test-cache-high-level-api.R +++ b/tests/testthat/test-cache-high-level-api.R @@ -1,5 +1,4 @@ capture.output(test_that("activated cache brings speedup on style_file() API", { - skip_if_not_installed("R.cache") on.exit(clear_testthat_cache()) clear_testthat_cache() activate_testthat_cache() @@ -25,7 +24,7 @@ text <- c( rep(10) capture.output(test_that("activated cache brings speedup on style_text() API on character vector", { - skip_if_not_installed("R.cache") + activate_testthat_cache() on.exit(clear_testthat_cache()) clear_testthat_cache() @@ -37,7 +36,7 @@ capture.output(test_that("activated cache brings speedup on style_text() API on })) capture.output(test_that("activated cache brings speedup on style_text() API on character scalar", { - skip_if_not_installed("R.cache") + activate_testthat_cache() on.exit(clear_testthat_cache()) clear_testthat_cache() @@ -50,7 +49,7 @@ capture.output(test_that("activated cache brings speedup on style_text() API on capture.output(test_that("no speedup when tranformer changes", { - skip_if_not_installed("R.cache") + activate_testthat_cache() on.exit(clear_testthat_cache()) clear_testthat_cache() @@ -68,7 +67,6 @@ capture.output( "activated cache brings speedup on style_text() API on ", "character scalar and character vector (mixed)" ), { - skip_if_not_installed("R.cache") activate_testthat_cache() on.exit(clear_testthat_cache()) clear_testthat_cache() @@ -81,7 +79,7 @@ capture.output( capture.output(test_that("unactivated cache does not bring speedup", { - skip_if_not_installed("R.cache") + on.exit(clear_testthat_cache()) clear_testthat_cache() cache_deactivate() @@ -93,7 +91,6 @@ capture.output(test_that("unactivated cache does not bring speedup", { capture.output(test_that("avoid deleting comments #584 (see commit messages)", { - skip_if_not_installed("R.cache") on.exit(clear_testthat_cache()) clear_testthat_cache() activate_testthat_cache() @@ -119,7 +116,6 @@ capture.output(test_that("avoid deleting comments #584 (see commit messages)", { capture.output(test_that("avoid removing roxygen mask (see commit messages in #584)", { - skip_if_not_installed("R.cache") on.exit(clear_testthat_cache()) clear_testthat_cache() activate_testthat_cache() diff --git a/tests/testthat/test-cache-with-r-cache.R b/tests/testthat/test-cache-with-r-cache.R index 1ceceff6e..9f67e3781 100644 --- a/tests/testthat/test-cache-with-r-cache.R +++ b/tests/testthat/test-cache-with-r-cache.R @@ -1,22 +1,10 @@ - -capture.output(test_that("No warnings are issued when R.cache is installed", { - skip_if_not_installed("R.cache") - on.exit(clear_testthat_cache()) - expect_silent(assert_R.cache_installation()) - expect_warning(style_text("1+1"), NA) - expect_warning(activate_testthat_cache(), NA) - expect_warning(style_text("1+1"), NA) - expect_silent(assert_R.cache_installation()) -})) - -capture.output(test_that("Cache management works when R.cache is installed", { - skip_if_not_installed("R.cache") +capture.output(test_that("Cache management works", { on.exit(clear_testthat_cache()) clear_testthat_cache() # clearing a cache inactivates the caching functionality. expect_false(cache_info(format = "tabular")$activated) activate_testthat_cache() - # at fresh startup, with R.cache installed + # at fresh startup expect_s3_class(cache_info(format = "tabular"), "tbl_df") expect_error(cache_info(), NA) expect_equal(basename(cache_activate()), styler_version) diff --git a/tests/testthat/test-cache-without-r-cache.R b/tests/testthat/test-cache-without-r-cache.R deleted file mode 100644 index 07831f11a..000000000 --- a/tests/testthat/test-cache-without-r-cache.R +++ /dev/null @@ -1,54 +0,0 @@ -test_that("Cache management fails mostly when R.cache is not installed", { - skip_if(rlang::is_installed("R.cache")) - expect_error(cache_info(), "is needed when the caching feature is activated") - expect_error(activate_testthat_cache(), "is needed when the caching feature is activated") - expect_error(cache_clear("testthat"), "is needed when the caching feature is activated") - expect_error(capture.output(cache_deactivate()), NA) - expect_error( - assert_R.cache_installation(), - "is needed when the caching feature is activated" - ) -}) - - -test_that("styling works when R.cache is not installed", { - skip_if(rlang::is_installed("R.cache")) - # no warning when cache is disabled manually - expect_output( - withr::with_options( - # simulate .onLoad() in fresh R session - list(styler.cache_name = styler_version), - { - cache_deactivate() - style_text("1+1") - } - ), - "Deactivated cache." - ) - - # warning if cache is not disabled manually - # warning for first time - expect_warning( - capture.output( - withr::with_options( - # simulate .onLoad() in fresh R session - list(styler.cache_name = styler_version), - style_text("1+1") - ) - ), - "R package R.cache .*Deactivating the caching feature for the current session" - ) - - # No warnings subsequently - expect_warning( - capture.output( - withr::with_options( - list(styler.cache_name = styler_version), { - suppressWarnings(style_text("1+1")) - style_text("1+1") - } - ) - ), - NA - ) -}) diff --git a/tic.R b/tic.R index 446b47242..c9af2aacf 100644 --- a/tic.R +++ b/tic.R @@ -1,11 +1,3 @@ -get_stage("after_install") %>% - add_code_step( - { - if (isTRUE(as.logical(toupper(Sys.getenv("R_REMOVE_RCACHE"))))) { - remove.packages("R.cache") - } - } - ) do_package_checks(error_on = ifelse(getRversion() >= "3.2", "note", "error")) if (Sys.getenv("id_rsa") != "" && ci()$get_branch() == "master" && Sys.getenv("BUILD_PKGDOWN") != "") { From 045034378fa39da6a96bd572b1643acf9777cec8 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Mon, 10 Feb 2020 20:56:44 +0100 Subject: [PATCH 09/10] document --- R/ui-caching.R | 6 +++--- README.Rmd | 9 +++++---- README.md | 8 ++++---- man/caching.Rd | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/R/ui-caching.R b/R/ui-caching.R index 8201da5c4..80f85652b 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -31,9 +31,9 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' @section Setup: #' styler by default uses caching, via the `{R.cache}` package. You will be #' asked you to let it create a permanent cache on your file system that styler -#' will use in case you have not set that up already for another tool that -#' uses `{R.cache}`. -#' This is needed if you want to cache across R sessions and not just within. +#' will use in case it is not set that up already for another tool that +#' uses `{R.cache}`. We encourage users to let `{R.cache}` create a permanent +#' directory for caching, because otherwise, the cache is lost at restart of R. #' #' @section Non-interactive use: #' Note that if you have never authorized `{R.cache}` to create the cache in a diff --git a/README.Rmd b/README.Rmd index 1970e036c..4c4319cc7 100644 --- a/README.Rmd +++ b/README.Rmd @@ -43,10 +43,11 @@ You can install the package from CRAN. install.packages("styler") ``` -You will be prompted to decide on how to use the [caching -feature](https://styler.r-lib.org/dev/reference/caching.html). See `?caching` -for details, in particular when you don't use styler interactively (i.e. not -from the R prompt or Rstudio Addin, ) +If you don't use styler interactively (i.e. not from the R prompt or Rstudio +Addin), make sure you authorise `{R.cache}` once to set up a permanent cache. +If you use it interactively, you will be asked to grant this permission once. +See `?caching` for details. + Or get the development version from GitHub: diff --git a/README.md b/README.md index a383dd06c..53bf3882b 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,10 @@ You can install the package from CRAN. install.packages("styler") ``` -You will be prompted to decide on how to use the [caching -feature](https://styler.r-lib.org/dev/reference/caching.html). See -`?caching` for details, in particular when you don’t use styler -interactively (i.e. not from the R prompt or Rstudio Addin, ) +If you don’t use styler interactively (i.e. not from the R prompt or +Rstudio Addin), make sure you authorise `{R.cache}` once to set up a +permanent cache. If you use it interactively, you will be asked to grant +this permission once. See `?caching` for details. Or get the development version from GitHub: diff --git a/man/caching.Rd b/man/caching.Rd index 0a445af3a..e8f19c83c 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -14,9 +14,9 @@ Code is cached by expression and the cache is shared across all APIs (e.g. styler by default uses caching, via the \code{{R.cache}} package. You will be asked you to let it create a permanent cache on your file system that styler -will use in case you have not set that up already for another tool that -uses \code{{R.cache}}. -This is needed if you want to cache across R sessions and not just within. +will use in case it is not set that up already for another tool that +uses \code{{R.cache}}. We encourage users to let \code{{R.cache}} create a permanent +directory for caching, because otherwise, the cache is lost at restart of R. } \section{Non-interactive use}{ From 825a4ad2621eadc37f150ed2d09fd079c090d811 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Mon, 10 Feb 2020 21:03:26 +0100 Subject: [PATCH 10/10] document --- R/ui-caching.R | 2 +- man/caching.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/ui-caching.R b/R/ui-caching.R index 80f85652b..90aec6d1a 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -36,7 +36,7 @@ cache_clear <- function(cache_name = NULL, ask = TRUE) { #' directory for caching, because otherwise, the cache is lost at restart of R. #' #' @section Non-interactive use: -#' Note that if you have never authorized `{R.cache}` to create the cache in a +#' Note that if you have never authorised `{R.cache}` to create the cache in a #' permenent directory, it wil build the cache in a temporary directory. To #' create a permenent cache, just open an interactive R session and type #' `cache_info()`. You can see under `Location:` if a permanent directory is diff --git a/man/caching.Rd b/man/caching.Rd index e8f19c83c..c978e168e 100644 --- a/man/caching.Rd +++ b/man/caching.Rd @@ -21,7 +21,7 @@ directory for caching, because otherwise, the cache is lost at restart of R. \section{Non-interactive use}{ -Note that if you have never authorized \code{{R.cache}} to create the cache in a +Note that if you have never authorised \code{{R.cache}} to create the cache in a permenent directory, it wil build the cache in a temporary directory. To create a permenent cache, just open an interactive R session and type \code{cache_info()}. You can see under \verb{Location:} if a permanent directory is