From f9699e992387ef0ee59bf890318f0a22d043b6b3 Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Mon, 9 Mar 2020 11:49:11 +0000 Subject: [PATCH 1/7] parallelise Styling --- DESCRIPTION | 3 ++- R/transform-files.R | 11 ++++++++--- R/ui-styling.R | 7 ++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 674d57a3b..a730c8f44 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,8 @@ Imports: tibble (>= 1.4.2), tools, withr (>= 1.0.0), - xfun (>= 0.1) + xfun (>= 0.1), + furrr Suggests: data.tree (>= 0.1.6), digest, diff --git a/R/transform-files.R b/R/transform-files.R index 68e44f428..8c8600c7f 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -9,7 +9,10 @@ #' Invisibly returns a data frame that indicates for each file considered for #' styling whether or not it was actually changed. #' @keywords internal -transform_files <- function(files, transformers, include_roxygen_examples) { +transform_files <- function(files, + transformers, + include_roxygen_examples, + pkg_root = ".") { transformer <- make_transformer(transformers, include_roxygen_examples) max_char <- min(max(nchar(files), 0), getOption("width")) len_files <- length(files) @@ -17,8 +20,8 @@ transform_files <- function(files, transformers, include_roxygen_examples) { cat("Styling ", len_files, " files:\n") } - changed <- map_lgl(files, transform_file, - fun = transformer, max_char_path = max_char + changed <- furrr::future_map_lgl(files, transform_file, + fun = transformer, max_char_path = max_char, pkg_root = pkg_root ) communicate_summary(changed, max_char) communicate_warning(changed, transformers) @@ -43,6 +46,7 @@ transform_file <- function(path, message_before = "", message_after = " [DONE]", message_after_if_changed = " *", + pkg_root = ".", ...) { char_after_path <- nchar(message_before) + nchar(path) + 1 max_char_after_message_path <- nchar(message_before) + max_char_path + 1 @@ -53,6 +57,7 @@ transform_file <- function(path, rep_char(" ", max(0L, n_spaces_before_message_after)), append = FALSE ) + setwd(pkg_root) changed <- transform_code(path, fun = fun, ...) bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick")) diff --git a/R/ui-styling.R b/R/ui-styling.R index 3f255f404..f8da61aae 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -82,7 +82,7 @@ style_pkg <- function(pkg = ".", pkg_root <- rprojroot::find_package_root_file(path = pkg) changed <- withr::with_dir(pkg_root, prettify_pkg( transformers, - filetype, exclude_files, exclude_dirs, include_roxygen_examples + filetype, exclude_files, exclude_dirs, include_roxygen_examples, pkg_root )) invisible(changed) } @@ -91,7 +91,8 @@ prettify_pkg <- function(transformers, filetype, exclude_files, exclude_dirs, - include_roxygen_examples) { + include_roxygen_examples, + pkg_root = ".") { filetype <- set_and_assert_arg_filetype(filetype) r_files <- rprofile_files <- vignette_files <- readme <- NULL exclude_files <- set_arg_paths(exclude_files) @@ -137,7 +138,7 @@ prettify_pkg <- function(transformers, c(r_files, rprofile_files, vignette_files, readme), exclude_files ) - transform_files(files, transformers, include_roxygen_examples) + transform_files(files, transformers, include_roxygen_examples, pkg_root) } #' Style a string From 1f7e39b8f3ff37e2a1c809345d049097c11d1aca Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Mon, 9 Mar 2020 12:06:48 +0000 Subject: [PATCH 2/7] style_dir parallel path --- R/ui-styling.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/R/ui-styling.R b/R/ui-styling.R index f8da61aae..6484b3ff5 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -199,7 +199,8 @@ style_dir <- function(path = ".", changed <- withr::with_dir( path, prettify_any( transformers, - filetype, recursive, exclude_files, exclude_dirs, include_roxygen_examples + filetype, recursive, exclude_files, exclude_dirs, + include_roxygen_examples, path ) ) invisible(changed) @@ -217,7 +218,8 @@ prettify_any <- function(transformers, recursive, exclude_files, exclude_dirs, - include_roxygen_examples) { + include_roxygen_examples, + path = ".") { exclude_files <- set_arg_paths(exclude_files) exclude_dirs <- set_arg_paths(exclude_dirs) files_root <- dir( @@ -237,7 +239,7 @@ prettify_any <- function(transformers, } transform_files( setdiff(c(files_root, files_other), exclude_files), - transformers, include_roxygen_examples + transformers, include_roxygen_examples, path ) } From 4a28e70617aaafbe9713c465983e562d9da8ee5f Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Mon, 9 Mar 2020 12:09:55 +0000 Subject: [PATCH 3/7] rename pkg_root to root in general case --- R/transform-files.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/transform-files.R b/R/transform-files.R index 8c8600c7f..ef553aebd 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -12,7 +12,7 @@ transform_files <- function(files, transformers, include_roxygen_examples, - pkg_root = ".") { + root = ".") { transformer <- make_transformer(transformers, include_roxygen_examples) max_char <- min(max(nchar(files), 0), getOption("width")) len_files <- length(files) @@ -21,7 +21,7 @@ transform_files <- function(files, } changed <- furrr::future_map_lgl(files, transform_file, - fun = transformer, max_char_path = max_char, pkg_root = pkg_root + fun = transformer, max_char_path = max_char, root = root ) communicate_summary(changed, max_char) communicate_warning(changed, transformers) @@ -46,7 +46,7 @@ transform_file <- function(path, message_before = "", message_after = " [DONE]", message_after_if_changed = " *", - pkg_root = ".", + root = ".", ...) { char_after_path <- nchar(message_before) + nchar(path) + 1 max_char_after_message_path <- nchar(message_before) + max_char_path + 1 @@ -57,7 +57,7 @@ transform_file <- function(path, rep_char(" ", max(0L, n_spaces_before_message_after)), append = FALSE ) - setwd(pkg_root) + setwd(root) changed <- transform_code(path, fun = fun, ...) bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick")) From 41d1b893648d0754aff134d71465e5713e84a5f4 Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Mon, 9 Mar 2020 14:06:30 +0000 Subject: [PATCH 4/7] furrr to future.apply --- DESCRIPTION | 2 +- R/transform-files.R | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a730c8f44..8fd15e7b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,7 @@ Imports: tools, withr (>= 1.0.0), xfun (>= 0.1), - furrr + future.apply Suggests: data.tree (>= 0.1.6), digest, diff --git a/R/transform-files.R b/R/transform-files.R index ef553aebd..9011684e1 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -20,9 +20,11 @@ transform_files <- function(files, cat("Styling ", len_files, " files:\n") } - changed <- furrr::future_map_lgl(files, transform_file, + changed <- future.apply::future_sapply( + files, transform_file, fun = transformer, max_char_path = max_char, root = root ) + communicate_summary(changed, max_char) communicate_warning(changed, transformers) new_tibble(list(file = files, changed = changed), nrow = len_files) From 116cc039650945559ebb1b81c1095f24a6a1fa73 Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Tue, 10 Mar 2020 16:27:03 +0000 Subject: [PATCH 5/7] Added a note in the News --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS.md b/NEWS.md index b1b3d9d7c..22d7acb12 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,11 @@ Release upon request by the CRAN team. - skip timing tests on CRAN as requested by CRAN team because they did not pass on all machines (#603). +## New features + +* `style_pkg()` and `style_dir()` gain the ability to run in parallel using + `future` backends. + # styler 1.3.1 Emergency release. In case multiple expressions are on one line and only From 377458d1db4417db8bb496e182d27fef91df3fd1 Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Tue, 10 Mar 2020 16:27:47 +0000 Subject: [PATCH 6/7] Documentation updates - parallel examples --- R/ui-styling.R | 8 ++++++++ man/prettify_any.Rd | 3 ++- man/style_dir.Rd | 4 ++++ man/style_pkg.Rd | 4 ++++ man/transform_file.Rd | 1 + man/transform_files.Rd | 2 +- 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/R/ui-styling.R b/R/ui-styling.R index 6484b3ff5..5f847a03d 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -69,6 +69,10 @@ NULL #' scope = "line_breaks", #' math_token_spacing = specify_math_token_spacing(zero = "'+'") #' ) +#' # For parallel operation: +#' library(future) +#' plan(multisession, workers = 4) +#' style_pkg() #' } #' @export style_pkg <- function(pkg = ".", @@ -185,6 +189,10 @@ style_text <- function(text, #' @examples #' \dontrun{ #' style_dir(file_type = "r") +#' # For parallel operation: +#' library(future) +#' plan(multisession, workers = 4) +#' style_dir() #' } #' @export style_dir <- function(path = ".", diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index 16ffa3c5d..e3b46edb0 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -10,7 +10,8 @@ prettify_any( recursive, exclude_files, exclude_dirs, - include_roxygen_examples + include_roxygen_examples, + path = "." ) } \arguments{ diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 53159cb59..617c81560 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -95,6 +95,10 @@ See section 'Warning' for a good strategy to apply styling safely. \examples{ \dontrun{ style_dir(file_type = "r") +# For parallel operation: +library(future) +plan(multisession, workers = 4) +style_dir() } } \seealso{ diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index a50f95513..5e422d90d 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -99,6 +99,10 @@ style_pkg( scope = "line_breaks", math_token_spacing = specify_math_token_spacing(zero = "'+'") ) +# For parallel operation: +library(future) +plan(multisession, workers = 4) +style_pkg() } } \seealso{ diff --git a/man/transform_file.Rd b/man/transform_file.Rd index 04d57a3f1..a4e0a8bea 100644 --- a/man/transform_file.Rd +++ b/man/transform_file.Rd @@ -11,6 +11,7 @@ transform_file( message_before = "", message_after = " [DONE]", message_after_if_changed = " *", + root = ".", ... ) } diff --git a/man/transform_files.Rd b/man/transform_files.Rd index fcd1caf5b..668dcfff9 100644 --- a/man/transform_files.Rd +++ b/man/transform_files.Rd @@ -4,7 +4,7 @@ \alias{transform_files} \title{Transform files with transformer functions} \usage{ -transform_files(files, transformers, include_roxygen_examples) +transform_files(files, transformers, include_roxygen_examples, root = ".") } \arguments{ \item{files}{A character vector with paths to the file that should be From 2a9646fa4bb27bdce35796703b5c17a7f5d25a7f Mon Sep 17 00:00:00 2001 From: RichardJActon Date: Tue, 10 Mar 2020 16:29:02 +0000 Subject: [PATCH 7/7] added issue number (#277) to news entry --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 22d7acb12..38e9dd3db 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,7 +13,7 @@ Release upon request by the CRAN team. ## New features * `style_pkg()` and `style_dir()` gain the ability to run in parallel using - `future` backends. + `future` backends (#277). # styler 1.3.1