Skip to content

Commit

Permalink
Merge pull request #118 from ThinkR-open/att_from_data
Browse files Browse the repository at this point in the history
Att from data
  • Loading branch information
MurielleDelmotte authored Aug 5, 2024
2 parents fe48132 + 22fa437 commit 6565c35
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 48 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: attachment
Title: Deal with Dependencies
Version: 0.4.2.9000
Version: 0.4.2.9001
Authors@R: c(
person("Sébastien", "Rochette", , "[email protected]", role = c("cre", "aut"),
comment = c(ORCID = "0000-0002-1565-9313")),
Expand All @@ -24,9 +24,8 @@ License: GPL-3
URL: https://thinkr-open.github.io/attachment/,
https://github.com/ThinkR-open/attachment
BugReports: https://github.com/ThinkR-open/attachment/issues
VignetteBuilder:
knitr
Config/fusen/version: 0.5.0.9006
VignetteBuilder: knitr
Config/fusen/version: 0.6.0
Config/Needs/website: ThinkR-open/thinkrtemplate
Config/testthat/edition: 3
Encoding: UTF-8
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(att_amend_desc)
export(att_from_data)
export(att_from_description)
export(att_from_examples)
export(att_from_namespace)
Expand All @@ -27,6 +28,8 @@ importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(knitr,purl)
importFrom(magrittr,"%>%")
importFrom(roxygen2,block_get_tag_value)
importFrom(roxygen2,parse_file)
importFrom(roxygen2,roxygenise)
importFrom(stats,na.omit)
importFrom(stats,setNames)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
## New features

- Add `att_from_examples()` to get all packages called in examples from R files
- Add `att_from_data()` to look for functions called in data loading code
- `att_amend_desc` amend package DESCRIPTION file (Suggests) with the list of dependencies extracted from examples in R files.

## Patch

- Adding an example using a suggest package to the dummypackage
- Adding an example using suggest packages to the dummypackage

# attachment 0.4.2

Expand Down
11 changes: 9 additions & 2 deletions R/add_from_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param dir.r path to directory with R scripts.
#'
#' @return Character vector of packages called with library or require.
#'
#' @importFrom roxygen2 parse_file block_get_tag_value
#' @examples
#' dummypackage <- system.file("dummypackage",package = "attachment")
#'
Expand All @@ -28,7 +28,14 @@ att_from_examples <- function(dir.r = "R") {
all_examples_clean <-
gsub(pattern = "\\\\dontrun\\s*\\{|\\\\donttest\\s*\\{", replacement = "#ICI\n{", x = all_examples)
cat(all_examples_clean, file = roxy_file, sep = "\n")

all_deps_examples_data <- attachment::att_from_data(all_examples_clean)

all_deps_examples <- attachment::att_from_rscript(roxy_file)

file.remove(roxy_file)
return(all_deps_examples)

all_deps <- unique(c(all_deps_examples, all_deps_examples_data))

return(all_deps)
}
20 changes: 20 additions & 0 deletions R/att_from_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Look for functions called in data loading code
#'
#' @param chr A character vector containing the code as a string. The code should follow the pattern used for loading data with `data()`, specifying the dataset and package.
#' @return A character vector containing the names of the packages from which datasets are being loaded.
#' @importFrom stringr str_extract_all
#' @export
#' @examples
#'
#' vec_char <- 'data("starwars", package = "dplyr")'
#' att_from_data(vec_char)
att_from_data <- function(chr) {
pkg <-
str_extract_all(
chr,
'(?<=data\\(\\".{1,100}\\"\\,\\s{0,5}package\\s{0,5}\\=\\s{0,5}\\\")[[:alnum:]\\.]+(?=\\\"\\))'
) |>
unlist()
return(pkg)

}
6 changes: 3 additions & 3 deletions R/create_dependencies_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
#' file.copy(system.file("dummypackage",package = "attachment"), tmpdir,
#' recursive = TRUE)
#' dummypackage <- file.path(tmpdir, "dummypackage")
#'
#'
#' # Create the dependencies commands but no file
#' create_dependencies_file(
#' path = file.path(dummypackage,"DESCRIPTION"),
#' to = NULL,
#' open_file = FALSE)
#'
#'
#' # Create the dependencies files in the package
#' create_dependencies_file(
#' path = file.path(dummypackage,"DESCRIPTION"),
#' to = file.path(dummypackage, "inst/dependencies.R"),
#' open_file = FALSE)
#' list.files(file.path(dummypackage, "inst"))
#' # browseURL(dummypackage)
#'
#'
#' # Clean temp files after this example
#' unlink(tmpdir, recursive = TRUE)
create_dependencies_file <- function(path = "DESCRIPTION",
Expand Down
8 changes: 4 additions & 4 deletions R/set_remotes.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#' # Find from Description file
#' dummypackage <- system.file("dummypackage", package = "attachment")
#' att_from_description(
#' path = file.path(dummypackage, "DESCRIPTION")) %>%
#' path = file.path(dummypackage, "DESCRIPTION")) |>
#' find_remotes()
#'
#' \dontrun{
#' # For the current package directory
#' att_from_description() %>% find_remotes()
#' att_from_description() |> find_remotes()
#' }
#'
#' \donttest{
Expand Down Expand Up @@ -69,15 +69,15 @@ find_remotes <- function(pkg) {
#' recursive = TRUE)
#' dummypackage <- file.path(tmpdir, "dummypackage")
#' # Add remotes field if there are Remotes locally
#' att_amend_desc(dummypackage) %>%
#' att_amend_desc(dummypackage) |>
#' set_remotes_to_desc()
#'
#' # Clean temp files after this example
#' unlink(tmpdir, recursive = TRUE)
#'
#' \dontrun{
#' # For your current package
#' att_amend_desc() %>%
#' att_amend_desc() |>
#' set_remotes_to_desc()
#' }
set_remotes_to_desc <- function(path.d = "DESCRIPTION", stop.local = FALSE,
Expand Down
3 changes: 3 additions & 0 deletions dev/config_attachment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pkg_ignore:
- svn
- pkgload
- bookdown
- pkgfake
- fakepkg
- dplyr
document: yes
normalize: no
inside_rmd: no
Expand Down
8 changes: 8 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ flat_create_dependencies_file.Rmd:
- tests/testthat/test-dependencies_file_text.R
- tests/testthat/test-create_dependencies_file.R
vignettes: vignettes/create-dependencies-file.Rmd
inflate:
flat_file: dev/flat_create_dependencies_file.Rmd
vignette_name: Create dependencies file
open_vignette: false
check: false
document: true
overwrite: 'yes'
clean: ask
flat_save_att_params.Rmd:
path: dev/flat_save_att_params.Rmd
state: active
Expand Down
6 changes: 3 additions & 3 deletions dev/flat_create_dependencies_file.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ dep_file_without_remotes <- readLines(file.path(dummypackage, "inst", "dependenc
test_that("create-dependencies-file works without remotes", {
expect_equal(dep_file_without_remotes[1], "# No Remotes ----")
expect_equal(dep_file_without_remotes[3], "to_install <- c(\"knitr\", \"magrittr\", \"rmarkdown\", \"testthat\")")
expect_equal(dep_file_without_remotes[3], "to_install <- c(\"glue\", \"knitr\", \"magrittr\", \"rmarkdown\", \"stringr\", \"testthat\")")
})
Expand Down Expand Up @@ -326,7 +326,7 @@ test_that("create-dependencies-file works with remotes", {
expect_equal(dep_file_with_remotes[7], "remotes::install_git('https://github.com/fakepkggit.git')")
expect_equal(dep_file_with_remotes[8], "remotes::install_git('https://MyForge.com/fakepkggit2r')")
expect_equal(dep_file_with_remotes[9], "remotes::install_github('ThinkR-open/fusen')")
expect_equal(dep_file_with_remotes[11], "to_install <- c(\"knitr\", \"magrittr\", \"rmarkdown\", \"testthat\")")
expect_equal(dep_file_with_remotes[11], "to_install <- c(\"glue\", \"knitr\", \"magrittr\", \"rmarkdown\", \"stringr\", \"testthat\")")
})
Expand All @@ -347,7 +347,7 @@ test_that("create-dependencies-file works with remotes install_only_if_missing",
expect_equal(dep_file_with_remotes_install_only_if_missing[7], "if(isFALSE(requireNamespace('fakepkggit', quietly = TRUE))) {message('installation of fakepkggit');remotes::install_git('https://github.com/fakepkggit.git')}")
expect_equal(dep_file_with_remotes_install_only_if_missing[8], "if(isFALSE(requireNamespace('fakepkggit2r', quietly = TRUE))) {message('installation of fakepkggit2r');remotes::install_git('https://MyForge.com/fakepkggit2r')}")
expect_equal(dep_file_with_remotes_install_only_if_missing[9], "if(isFALSE(requireNamespace('fusen', quietly = TRUE))) {message('installation of fusen');remotes::install_github('ThinkR-open/fusen')}")
expect_equal(dep_file_with_remotes_install_only_if_missing[11], "to_install <- c(\"knitr\", \"magrittr\", \"rmarkdown\", \"testthat\")")
expect_equal(dep_file_with_remotes_install_only_if_missing[11], "to_install <- c(\"glue\", \"knitr\", \"magrittr\", \"rmarkdown\", \"stringr\", \"testthat\")")
})
# Clean temp files after this example
Expand Down
18 changes: 11 additions & 7 deletions inst/dummypackage/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ Depends:
R (>= 3.5.0)
Imports:
magrittr,
utils
stats
Suggests:
glue,
knitr,
rmarkdown,
testthat,
utils,
stringr
LinkingTo:
Rcpp
VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.0
Suggests:
knitr,
rmarkdown,
testthat
VignetteBuilder: knitr
RoxygenNote: 7.3.1
1 change: 1 addition & 0 deletions inst/dummypackage/R/my_mean.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @examples
#' # example code
#' library(utils)
#' data("fruit", package = "stringr")
my_mean <- function(x){
x <- x %>% stats::na.omit()
1+1
Expand Down
5 changes: 5 additions & 0 deletions inst/dummypackage/man/my_mean.Rd

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

22 changes: 22 additions & 0 deletions man/att_from_data.Rd

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

4 changes: 2 additions & 2 deletions man/find_remotes.Rd

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

4 changes: 2 additions & 2 deletions man/set_remotes_to_desc.Rd

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

32 changes: 18 additions & 14 deletions tests/testthat/test-amend-description.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ test_that("att_amend_desc updates description", {
expect_equal(desc_file[w.depends + 6], " glue,")
expect_equal(desc_file[w.depends + 7], " knitr,")
expect_equal(desc_file[w.depends + 8], " rmarkdown,")
expect_equal(desc_file[w.depends + 9], " testthat,")
expect_equal(desc_file[w.depends + 10], " utils")
expect_equal(desc_file[w.depends + 11], "LinkingTo:" )
expect_equal(desc_file[w.depends + 12], " Rcpp")
expect_equal(desc_file[w.depends + 9], " stringr,")
expect_equal(desc_file[w.depends + 10], " testthat,")
expect_equal(desc_file[w.depends + 11], " utils")
expect_equal(desc_file[w.depends + 12], "LinkingTo:" )
expect_equal(desc_file[w.depends + 13], " Rcpp")
# base does not appear
expect_false(all(grepl("base", desc_file)))
# utils is removed
Expand Down Expand Up @@ -486,10 +487,11 @@ library(ggplot3)
expect_equal(desc_file[w.depends + 7], " glue,")
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " rmarkdown,")
expect_equal(desc_file[w.depends + 10], " testthat,")
expect_equal(desc_file[w.depends + 11], " utils")
expect_equal(desc_file[w.depends + 12], "LinkingTo:" )
expect_equal(desc_file[w.depends + 13], " Rcpp")
expect_equal(desc_file[w.depends + 10], " stringr,")
expect_equal(desc_file[w.depends + 11], " testthat,")
expect_equal(desc_file[w.depends + 12], " utils")
expect_equal(desc_file[w.depends + 13], "LinkingTo:" )
expect_equal(desc_file[w.depends + 14], " Rcpp")


# Clean after
Expand Down Expand Up @@ -533,8 +535,9 @@ my_length <- function(x) {
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " pkgfake,")
expect_equal(desc_file[w.depends + 10], " rmarkdown,")
expect_equal(desc_file[w.depends + 11], " testthat,")
expect_equal(desc_file[w.depends + 12], " utils")
expect_equal(desc_file[w.depends + 11], " stringr,")
expect_equal(desc_file[w.depends + 12], " testthat,")
expect_equal(desc_file[w.depends + 13], " utils")
# Clean after
unlink(dummypackage, recursive = TRUE)

Expand Down Expand Up @@ -579,10 +582,11 @@ my_length <- function(x) {
expect_equal(desc_file[w.depends + 7], " glue,")
expect_equal(desc_file[w.depends + 8], " knitr,")
expect_equal(desc_file[w.depends + 9], " rmarkdown,")
expect_equal(desc_file[w.depends + 10], " testthat,")
expect_equal(desc_file[w.depends + 11], " utils")
expect_equal(desc_file[w.depends + 12], "LinkingTo:" )
expect_equal(desc_file[w.depends + 13], " Rcpp")
expect_equal(desc_file[w.depends + 10], " stringr,")
expect_equal(desc_file[w.depends + 11], " testthat,")
expect_equal(desc_file[w.depends + 12], " utils")
expect_equal(desc_file[w.depends + 13], "LinkingTo:" )
expect_equal(desc_file[w.depends + 14], " Rcpp")

# Clean after
unlink(dummypackage, recursive = TRUE)
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-att_from_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("att_from_data works with a single package", {

input1 <- 'data("starwars", package = "dplyr")'
expect_equal(att_from_data(input1), "dplyr")

})

test_that("att_from_data works with multi package", {

input1 <- 'data("starwars", package = "dplyr")data("dataset", package = "pkgfake")'
expect_equal(att_from_data(input1), c("dplyr", "pkgfake"))

})


test_that("att_from_data works with extra spaces", {

input1 <- 'data("starwars",package = "dplyr")'
expect_equal(att_from_data(input1), "dplyr")

})
Loading

0 comments on commit 6565c35

Please sign in to comment.