diff --git a/DESCRIPTION b/DESCRIPTION index efb7977..9242df5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,11 @@ Package: attachment Title: Deal with Dependencies -Version: 0.4.2.9002 +Version: 0.4.2.9003 Authors@R: c( - person("Sébastien", "Rochette", , "sebastien@thinkr.fr", role = c("cre", "aut"), - comment = c(ORCID = "0000-0002-1565-9313")), - person("Vincent", "Guyader", , "vincent@thinkr.fr", role = "aut", - comment = c(ORCID = "0000-0003-0671-9270", "previous maintainer")), + person("Vincent", "Guyader", , "vincent@thinkr.fr", role = c("cre", "aut"), + comment = c(ORCID = "0000-0003-0671-9270")), + person("Sébastien", "Rochette", , "sebastien@thinkr.fr", role = c("aut"), + comment = c(ORCID = "0000-0002-1565-9313", "previous maintainer")), person("Murielle", "Delmotte", , "murielle@thinkr.fr", role = "aut", comment = c(ORCID = "0000-0002-1339-2424")), person("Swann", "Floc'hlay", , "swann@thinkr.fr", role = "aut", @@ -30,7 +30,7 @@ Config/Needs/website: ThinkR-open/thinkrtemplate Config/testthat/edition: 3 Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Language: en-US Depends: R (>= 3.4) diff --git a/NEWS.md b/NEWS.md index 601ed88..690f7b3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ ## Patch - Adding an example using suggest packages to the dummypackage +- `att_from_rmds()` and `att_from_rscript` doesn't search in 'renv' folder anymore # attachment 0.4.2 diff --git a/R/att_from_rmds.R b/R/att_from_rmds.R index dc4e2e9..196715b 100644 --- a/R/att_from_rmds.R +++ b/R/att_from_rmds.R @@ -79,6 +79,7 @@ att_from_rmd <- function(path, temp_dir = tempdir(), warn = -1, #' @param path path to directory with Rmds or vector of Rmd files #' @param pattern pattern to detect Rmd files #' @param recursive logical. Should the listing recurse into directories? +#' @param folder_to_exclude Folder to exclude during scan to detect packages 'renv' by default #' @inheritParams att_from_rmd #' #' @return Character vector of packages called with library or require. @@ -94,7 +95,7 @@ att_from_rmd <- function(path, temp_dir = tempdir(), warn = -1, att_from_rmds <- function(path = "vignettes", pattern = "*.[.](Rmd|rmd|qmd)$", recursive = TRUE, warn = -1, - inside_rmd = FALSE, inline = TRUE) { + inside_rmd = FALSE, inline = TRUE,folder_to_exclude = "renv") { if (isTRUE(all(dir.exists(path)))) { all_f <- list.files(path, full.names = TRUE, pattern = pattern, recursive = recursive) @@ -104,6 +105,17 @@ att_from_rmds <- function(path = "vignettes", stop("Some files/directories do not exist") } + if (!is.null(folder_to_exclude) && length(folder_to_exclude) > 0) { + exclude_files <- unlist(lapply(folder_to_exclude, function(folder) { + list.files(path = file.path(path, folder), full.names = TRUE, pattern = pattern, recursive = recursive) + })) + + # Exclure les fichiers + all_f <- setdiff(all_f, exclude_files) + } + + + res <- lapply(all_f, function(x) att_from_rmd( x, warn = warn, diff --git a/R/att_from_rscripts.R b/R/att_from_rscripts.R index 8180597..ce36c4c 100644 --- a/R/att_from_rscripts.R +++ b/R/att_from_rscripts.R @@ -47,7 +47,7 @@ att_from_rscript <- function(path) { #' @param path directory with R scripts inside or vector of R scripts #' @param pattern pattern to detect R script files #' @param recursive logical. Should the listing recurse into directories? -#' +#' @param folder_to_exclude Folder to exclude during scan to detect packages. 'renv' by default. #' @return vector of character of packages names found in the R script #' #' @export @@ -58,7 +58,7 @@ att_from_rscript <- function(path) { #' att_from_rscripts(path = file.path(dummypackage, "R")) #' att_from_rscripts(path = list.files(file.path(dummypackage, "R"), full.names = TRUE)) -att_from_rscripts <- function(path = "R", pattern = "*.[.](r|R)$", recursive = TRUE) { +att_from_rscripts <- function(path = "R", pattern = "*.[.](r|R)$", recursive = TRUE, folder_to_exclude = "renv") { if (isTRUE(all(dir.exists(path)))) { all_f <- list.files(path, full.names = TRUE, pattern = pattern, recursive = recursive) @@ -68,6 +68,16 @@ att_from_rscripts <- function(path = "R", pattern = "*.[.](r|R)$", recursive = T stop("Some files/directories do not exist") } + if (!is.null(folder_to_exclude) && length(folder_to_exclude) > 0) { + exclude_files <- unlist(lapply(folder_to_exclude, function(folder) { + list.files(path = file.path(path, folder), full.names = TRUE, pattern = pattern, recursive = recursive) + })) + + # Exclure les fichiers + all_f <- setdiff(all_f, exclude_files) + } + + lapply(all_f, att_from_rscript) %>% unlist() %>% unique() %>% diff --git a/R/create_renv.R b/R/create_renv.R index 0ee827b..0be12ba 100644 --- a/R/create_renv.R +++ b/R/create_renv.R @@ -23,6 +23,7 @@ extra_dev_pkg <- c( #' use the default list. Use `NULL` for no extra package. #' Use `attachment:::extra_dev_pkg` for the list. #' @param folder_to_include Folder to scan to detect development packages +#' @param folder_to_exclude Folder to exclude during scan to detect packages.'renv' by default #' @param output Path and name of the file created, default is `./renv.lock` #' @param install_if_missing Logical. Install missing packages. `TRUE` by default #' @param document Logical. Whether to run [att_amend_desc()] before @@ -47,7 +48,8 @@ extra_dev_pkg <- c( #' } create_renv_for_dev <- function(path = ".", dev_pkg = "_default", - folder_to_include = c("dev", "data-raw"), + folder_to_include = c("dev", "data-raw","renv"), + folder_to_exclude = c("renv"), output = "renv.lock", install_if_missing = TRUE, document = TRUE, @@ -116,9 +118,8 @@ create_renv_for_dev <- function(path = ".", pkg_list <- unique( c( - att_from_qmds(path = path,recursive = TRUE), - att_from_rmds(path = path,recursive = TRUE), - att_from_rscripts(path = path,recursive = TRUE), + att_from_rmds(path = path,recursive = TRUE, folder_to_exclude = folder_to_exclude), + att_from_rscripts(path = path,recursive = TRUE, folder_to_exclude = folder_to_exclude), dev_pkg ) ) @@ -144,8 +145,8 @@ create_renv_for_dev <- function(path = ".", # folder_to_include <- folder_to_include[dir.exists(file.path(path, folder_to_include))] - from_r_script <- att_from_rscripts(folder_to_include) - from_rmd <- att_from_rmds(folder_to_include) + from_r_script <- att_from_rscripts(folder_to_include,, folder_to_exclude = folder_to_exclude) + from_rmd <- att_from_rmds(folder_to_include,, folder_to_exclude = folder_to_exclude) pkg_list <- unique(c(pkg_list, from_r_script, from_rmd)) } diff --git a/man/att_from_rmds.Rd b/man/att_from_rmds.Rd index 470d679..342196f 100644 --- a/man/att_from_rmds.Rd +++ b/man/att_from_rmds.Rd @@ -11,7 +11,8 @@ att_from_rmds( recursive = TRUE, warn = -1, inside_rmd = FALSE, - inline = TRUE + inline = TRUE, + folder_to_exclude = "renv" ) att_from_qmds( @@ -20,7 +21,8 @@ att_from_qmds( recursive = TRUE, warn = -1, inside_rmd = FALSE, - inline = TRUE + inline = TRUE, + folder_to_exclude = "renv" ) } \arguments{ @@ -36,6 +38,8 @@ att_from_qmds( in case this must be executed in an external R session} \item{inline}{Logical. Default TRUE. Whether to explore inline code for dependencies.} + +\item{folder_to_exclude}{Folder to exclude during scan to detect packages 'renv' by default} } \value{ Character vector of packages called with library or require. diff --git a/man/att_from_rscripts.Rd b/man/att_from_rscripts.Rd index f6b080d..de4a583 100644 --- a/man/att_from_rscripts.Rd +++ b/man/att_from_rscripts.Rd @@ -4,7 +4,12 @@ \alias{att_from_rscripts} \title{Look for functions called with \code{::} and library/requires in folder of scripts} \usage{ -att_from_rscripts(path = "R", pattern = "*.[.](r|R)$", recursive = TRUE) +att_from_rscripts( + path = "R", + pattern = "*.[.](r|R)$", + recursive = TRUE, + folder_to_exclude = "renv" +) } \arguments{ \item{path}{directory with R scripts inside or vector of R scripts} @@ -12,6 +17,8 @@ att_from_rscripts(path = "R", pattern = "*.[.](r|R)$", recursive = TRUE) \item{pattern}{pattern to detect R script files} \item{recursive}{logical. Should the listing recurse into directories?} + +\item{folder_to_exclude}{Folder to exclude during scan to detect packages. 'renv' by default.} } \value{ vector of character of packages names found in the R script diff --git a/man/attachment-package.Rd b/man/attachment-package.Rd index 5c11039..01c66f0 100644 --- a/man/attachment-package.Rd +++ b/man/attachment-package.Rd @@ -20,11 +20,11 @@ Useful links: } \author{ -\strong{Maintainer}: Sébastien Rochette \email{sebastien@thinkr.fr} (\href{https://orcid.org/0000-0002-1565-9313}{ORCID}) +\strong{Maintainer}: Vincent Guyader \email{vincent@thinkr.fr} (\href{https://orcid.org/0000-0003-0671-9270}{ORCID}) Authors: \itemize{ - \item Vincent Guyader \email{vincent@thinkr.fr} (\href{https://orcid.org/0000-0003-0671-9270}{ORCID}) (previous maintainer) + \item Sébastien Rochette \email{sebastien@thinkr.fr} (\href{https://orcid.org/0000-0002-1565-9313}{ORCID}) (previous maintainer) \item Murielle Delmotte \email{murielle@thinkr.fr} (\href{https://orcid.org/0000-0002-1339-2424}{ORCID}) \item Swann Floc'hlay \email{swann@thinkr.fr} (\href{https://orcid.org/0000-0003-1477-830X}{ORCID}) } diff --git a/man/create_renv_for_dev.Rd b/man/create_renv_for_dev.Rd index 6c63fa7..4337d1c 100644 --- a/man/create_renv_for_dev.Rd +++ b/man/create_renv_for_dev.Rd @@ -8,7 +8,8 @@ create_renv_for_dev( path = ".", dev_pkg = "_default", - folder_to_include = c("dev", "data-raw"), + folder_to_include = c("dev", "data-raw", "renv"), + folder_to_exclude = c("renv"), output = "renv.lock", install_if_missing = TRUE, document = TRUE, @@ -35,6 +36,8 @@ Use \code{attachment:::extra_dev_pkg} for the list.} \item{folder_to_include}{Folder to scan to detect development packages} +\item{folder_to_exclude}{Folder to exclude during scan to detect packages.'renv' by default} + \item{output}{Path and name of the file created, default is \code{./renv.lock}} \item{install_if_missing}{Logical. Install missing packages. \code{TRUE} by default} diff --git a/tests/testthat/f3.R b/tests/testthat/f3.R new file mode 100644 index 0000000..9de7ed2 --- /dev/null +++ b/tests/testthat/f3.R @@ -0,0 +1 @@ +library(dont.find.me3) diff --git a/tests/testthat/f4.R b/tests/testthat/f4.R new file mode 100644 index 0000000..746257f --- /dev/null +++ b/tests/testthat/f4.R @@ -0,0 +1 @@ +library(dont.find.me4) diff --git a/tests/testthat/f5.R b/tests/testthat/f5.R new file mode 100644 index 0000000..2797be2 --- /dev/null +++ b/tests/testthat/f5.R @@ -0,0 +1 @@ +library(find.me5.from.keep) diff --git a/tests/testthat/test-rmd.R b/tests/testthat/test-rmd.R index d468fc1..9986151 100644 --- a/tests/testthat/test-rmd.R +++ b/tests/testthat/test-rmd.R @@ -108,3 +108,49 @@ test_that("rmds well parsed", { }) }) + + + +test_that("folder_to_exclude works in att_from_rmds", { + + + dir_with_Rmd <- tempfile(pattern = "rmds") + dir.create(dir_with_Rmd) + file.copy("f1.Rmd", to = file.path(dir_with_Rmd, "f1.Rmd")) + dir.create(file.path(dir_with_Rmd,"renv")) + dir.create(file.path(dir_with_Rmd,"avoid")) + dir.create(file.path(dir_with_Rmd,"keep")) + file.copy("f1.Rmd", to = file.path(dir_with_Rmd,"renv", "f3.Rmd")) #library(dont.find.me3) + file.copy("f1.Rmd", to = file.path(dir_with_Rmd,"avoid", "f4.Rmd")) #library(dont.find.me4) + file.copy("f1.Rmd", to = file.path(dir_with_Rmd,"keep", "f5.Rmd")) #library(find.me5.from.keep) + +cat(" +```{r} +library(find.me5.from.keep) +``` + ",append = TRUE,file = file.path(dir_with_Rmd,"keep", "f5.Rmd")) + + +cat(" +```{r} +library(dont.find.me3) +``` + ",append = TRUE,file = file.path(dir_with_Rmd,"renv", "f3.Rmd")) + +cat(" +```{r} +library(dont.find.me4) +``` + ",append = TRUE,file = file.path(dir_with_Rmd,"avoid", "f4.Rmd")) + + res_dir <- att_from_rmds(path = dir_with_Rmd,folder_to_exclude = c("renv","avoid"),recursive = TRUE) + + expect_true("find.me5.from.keep" %in% res_dir) + expect_false("dont.find.me3" %in% res_dir) + expect_false("dont.find.me4" %in% res_dir) + + + + +}) + diff --git a/tests/testthat/test-rscript.R b/tests/testthat/test-rscript.R index 98e670d..d8a279f 100644 --- a/tests/testthat/test-rscript.R +++ b/tests/testthat/test-rscript.R @@ -65,3 +65,30 @@ test_that("newline correctly escaped", { expect_true(!"nknitr" %in% newline_script) }) + + + +test_that("folder_to_exclude works in att_from_rscripts", { + + + dir_with_R <- tempfile(pattern = "rscripts") + dir.create(dir_with_R) + file.copy("f2.R", to = file.path(dir_with_R, "f2.R")) + dir.create(file.path(dir_with_R,"renv")) + dir.create(file.path(dir_with_R,"avoid")) + dir.create(file.path(dir_with_R,"keep")) + file.copy("f3.R", to = file.path(dir_with_R,"renv", "f3.R")) + file.copy("f4.R", to = file.path(dir_with_R,"avoid", "f4.R")) + file.copy("f5.R", to = file.path(dir_with_R,"keep", "f5.R")) + file.copy("f1.Rmd", to = file.path(dir_with_R, "f1.Rmd")) + + res_dir <- att_from_rscripts(path = dir_with_R,folder_to_exclude = c("renv","avoid")) + + expect_true("find.me5.from.keep" %in% res_dir) + expect_false("dont.find.me3" %in% res_dir) + expect_false("dont.find.me4" %in% res_dir) + + + + +})