Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
Merge commit 'c842edddd49d3b97bbcedfd160a831fdb3aef66b'

#Conflicts:
#	R/outline.R
  • Loading branch information
olivroy committed Jun 26, 2024
2 parents 5f665cb + c842edd commit 322e5d0
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: reuseme
Title: Collections of Utility Functions to Work Across Projects
Version: 0.0.2.9004
Version: 0.0.2.9005
Authors@R:
person("Olivier", "Roy", , "olivierroy71@hotmail.com", role = c("aut", "cre"))
Description: Allows you to browse current projects, rename files safely,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(dir_outline)
export(distinct_identity)
export(extract_cell_value)
export(file_outline)
export(filter_detect)
export(filter_identity)
export(filter_if_any)
export(filter_if_any_identity)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ that will passed on to `proj_list()`

## Fixes

* `rename_files2()` now looks in `.Rbuildignore` to see if some files should be replaced.
* Improved regex in `link_gh_issue()`.

* `file_outline()` now recognize `describe()` test calls.
Expand Down
2 changes: 2 additions & 0 deletions R/browse-pkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ browse_pkg <- function(package = NULL,
if (length(pkgdown) > 1) {
pkgdown <- stringr::str_subset(pkgdown, package)
}
# Remove trailing index.html from site
pkgdown <- sub("index.html$", "", pkgdown)
}

cran_home <- suppressMessages(usethis::browse_cran(package))
Expand Down
39 changes: 39 additions & 0 deletions R/dplyr-plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,45 @@ filter_if_any <- function(.data, ..., .by = NULL, .keep_new_var = FALSE) {
i = "See {.help dplyr::filter} for more information on how it works."
))
}

#' Filter rows by pattern
#'
#' Shortcut for [dplyr::filter()] and [stringr::str_detect()]
#'
#' @inheritParams dplyr::mutate
#' @inheritParams dplyr::if_any
#' @inheritParams base::grepl
#'
#' @return A data frame with relocated columns at first
#' @export
#'
#' @examples
#' # don't specify column
#' dplyr::band_members |>
#' filter_detect("Beatles")
#' # specify columns
#' dplyr::band_members |>
#' filter_detect("Beatles", band)
filter_detect <- function(.data, pattern, .cols = dplyr::everything(), ignore.case = TRUE) {
rlang::check_required(pattern)
res <- dplyr::filter(
.data,
dplyr::if_any(
.cols = {{ .cols }},
\(x) grepl(pattern, x, ignore.case = ignore.case)
)
)
n_matches <- purrr::map_int(res, \(x) sum(grepl(pattern, x, ignore.case = ignore.case)))
n_matches <- n_matches[n_matches > 0]

if (length(n_matches) > 0L) {
n_matches <- sort(names(n_matches))
res <- res |>
dplyr::relocate(dplyr::all_of(n_matches))
}
res
}

#' Elegant wrapper around filter and pull
#'
#' It can be very useful when trying to extract a value from somewhere, and you
Expand Down
7 changes: 4 additions & 3 deletions R/files-conflicts.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
check_referenced_files <- function(path = ".", quiet = FALSE) {
# TODO insert in either proj_outline, or rename_file
if (path == "." || fs::is_dir(path)) {
path <- fs::dir_ls(path = path, recurse = TRUE, regexp = "\\.(R|md|ml)$")
path <- fs::path_filter(path = path, regexp = "_files|tests/testthat|_book|_freeze", invert = TRUE) # need to do this in 2 places
} else if (fs::path_ext(path) %in% c("R", "yml", "yaml", "Rmd", "md", "qmd", "Rmarkdown")) {
# FIXME in Rbuilignore, change `^_pkgdown\.yml$` to `_pkgdown.yml` to make sure it works
path <- fs::dir_ls(path = path, recurse = TRUE, regexp = "\\.(R|md|ml|builignore)$", all = TRUE, type = "file")
path <- fs::path_filter(path = path, regexp = "_files|tests/testthat|_book|_freeze|.github", invert = TRUE) # need to do this in 2 places
} else if (fs::path_ext(path) %in% c("R", "yml", "yaml", "Rmd", "md", "qmd", "Rmarkdown", "gitignore", "Rbuildignore")) {
path <- path
} else {
cli::cli_abort("Wrong specification.")
Expand Down
6 changes: 5 additions & 1 deletion R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ file_outline <- function(path = active_rs_doc(),
"i" = "Run {.run [{.fn proj_file}](reuseme::proj_file(\"{pattern}\"))} to search in file names too."
)
} else {
msg <- "Empty outline."
msg <- c("{.path {path}}","Empty outline.")
}
cli::cli_inform(msg)
return(invisible())
Expand Down Expand Up @@ -261,16 +261,20 @@ dir_outline <- function(path = ".", pattern = NULL, exclude_tests = FALSE, exclu
# examples don't help understand a project.
file_list_to_outline <- exclude_example_files(file_list_to_outline)
}

if (exclude_tests) {
file_list_to_outline <- fs::path_filter(
file_list_to_outline,
regexp = "tests/",
invert = TRUE
)
}

# TODO expand this to apply to most generated files
if (any(grepl("README.Rmd", file_list_to_outline))) {
file_list_to_outline <- stringr::str_subset(file_list_to_outline, "README.md", negate = TRUE)
}

if (dir_tree) {
cli::cli_h2("Here are the non-R files of {.file {path}}")
regexp_exclude <- paste(
Expand Down
2 changes: 1 addition & 1 deletion R/proj-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ proj_file <- function(file = NULL, path = active_rs_proj(), pattern = NULL) {
}
file_exts <- c("R", "qmd", "Rmd", "md", "Rmarkdown")
file_exts_regex <- paste0("*.", file_exts, "$", collapse = "|")
possible_files <- fs::dir_ls(proj_path, regexp = file_exts_regex, recurse = TRUE)
possible_files <- fs::dir_ls(proj_path, regexp = file_exts_regex, recurse = TRUE, type = "file")
possible_files <- fs::path_filter(possible_files, regexp = file)
if (length(possible_files) > 1L) {
# exclude these files if multiple matches
Expand Down
5 changes: 3 additions & 2 deletions R/rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ rename_files2 <- function(old,
related_files <- fs::dir_ls(regexp = paste0(basename_remove_ext(old), "\\."), recurse = TRUE)
related_files <- fs::path_filter(related_files, "_snaps/|_book/|_files|_freeze|renv/", invert = TRUE)
related_files <- setdiff(related_files, old)

# remove project name from conflicts.
related_files <- stringr::str_subset(related_files, proj_name, negate = TRUE)
if (length(related_files) > 0) {
Expand Down Expand Up @@ -132,8 +133,8 @@ rename_files2 <- function(old,

regex_friendly <- paste0("to {.val ", regex_friendly, "}")
# avoid searching in generated files and tests/testthat files
files_to_look_into <- fs::dir_ls(regexp = "ya?ml$|md$|R$", type = "file", recurse = TRUE)
files_to_look_into <- fs::path_filter(files_to_look_into, regexp = "_files|tests/testthat|_book/|_freeze/|renv/", invert = TRUE) # need to do elsewhere too
files_to_look_into <- fs::dir_ls(regexp = "ya?ml$|md$|R$|gitignore|Rbuildignore", type = "file", recurse = TRUE, all = TRUE)
files_to_look_into <- fs::path_filter(files_to_look_into, regexp = "_files|tests/testthat|_book/|_freeze/|renv/|.github/|.git/", invert = TRUE) # need to do elsewhere too
n_file_names_conflicts <- solve_file_name_conflict(
files = files_to_look_into,
regex = regexp_to_search_for_in_files,
Expand Down
1 change: 0 additions & 1 deletion TODO.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# TODO [outline] todos in qmd file inside html comment
# TODO reframe more than one issue. nw drive
# TODO [delete] generated files
# TODO [proj_file] to accesss data (return the path in this case?)
# TODO [check_referenced_files] doesn't check for {.file R/file.R}
# TODO browse_pkg should open by default if no vignettes are found, because there is not much to do in the R-session.
# TODO exclude _files from `proj_list()`
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ reference:
- count_pct
- case_if_any
- filter_if_any
- filter_detect
- extract_cell_value
- slice_group_sample
- na_if2
Expand Down
38 changes: 38 additions & 0 deletions man/filter_detect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion tests/testthat/test-dplyr-plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_that("slice_group_sample() works as expected", {
)
})

test_that("`filter_if_any()` works as expected", {
test_that("filter_if_any() works as expected", {
expect_equal(
dplyr::starwars |>
dplyr::mutate(v1 = birth_year > 10, .by = gender) |>
Expand All @@ -42,6 +42,27 @@ test_that("`filter_if_any()` works as expected", {
)
})

test_that("filter_detect() works as expected", {
expect_gt(
dplyr::band_members |>
filter_detect("Beatles") |>
nrow(),
0
)
# Reorder based on number of matches.
expect_named(
dplyr::band_members |>
filter_detect("Beatles"),
c("band", "name")
)
# no match
expect_equal(
mtcars |>
filter_detect("Beatles") |>
nrow(),
0
)
})

test_that("filter_if_any() errors correctly when using `by` instead of `.by`", {
skip("Not ready")
Expand Down

0 comments on commit 322e5d0

Please sign in to comment.