-
Notifications
You must be signed in to change notification settings - Fork 6
/
infer_deps.R
96 lines (95 loc) Β· 3.46 KB
/
infer_deps.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#' Infer dependencies
#'
#' Infers the R packages that your R package depends on.
#' @param which Which types of dependencies to return.
#' @param imports_thresh The minimum number of times that a package has to be
#' called within your package to assign it as an Import.
#' If is called less times than this threshold,
#' it will instead be assigned as a Suggest,
#' which means it will not be installed by default.
#' @param imports R packages that are exempt from the \code{suggests_thresh}
#' rule and are instead automatically assigned as Imports.
#' @param suggests R packages that are exempt from the \code{suggests_thresh}
#' rule and are instead automatically assigned as Suggests.
#' @param add_newlines Prefix each package name with a newline character
#' and two spaces. This is useful for formatting \emph{DESCRIPTION} files.
#' @inheritParams renv::dependencies
#' @returns A character vector of R package names.
#'
#' @export
#' @importFrom desc desc
#' @importFrom renv dependencies
#' @examples
#' #### Get example DESCRIPTION file ####
#' url <- "https://github.com/neurogenomics/templateR/raw/master/DESCRIPTION"
#' path <- tempfile(fileext = "DESCRIPTION")
#' utils::download.file(url,path)
#'
#' deps <- infer_deps(path = path)
infer_deps <- function(path = here::here("DESCRIPTION"),
which = c("Imports","Suggests"),
imports_thresh = 2,
imports = NULL,
suggests = c("testthat","rmarkdown","markdown",
"knitr","remotes","knitr","covr"),
errors = c("reported", "fatal", "ignored"),
dev = FALSE,
progress = TRUE,
add_newlines = FALSE){
# devoptera::args2vars(infer_deps, reassign = TRUE)
Package <- NULL;
#### Check for packages that appear in multiple fields ####
overlap <- intersect(imports, suggests)
if(length(overlap)>0){
stp <- paste(
"R packages cannot be in both Imports and Suggests",
"Please ensure the following packages only appear",
"in one of the two fields:",
paste("\n - ",shQuote(overlap))
)
stop(stp)
}
#### Search for deps ####
dat <- renv::dependencies(path = path,
progress = progress,
errors = errors,
dev = dev)
#### Remove "R" ####
dat <- subset(dat, Package!="R")
dfile <- grep("DESCRIPTION$",dat$Source, value = TRUE)[1]
#### Remove the names of the package itself ####
if(length(dfile)>0){
if(file.exists(dfile)){
pkg <- get_description(paths = dfile,
use_repos = FALSE)[[1]]$get_field(key = "Package")
} else {
pkg <- basename(dirname(dfile))
}
dat <- subset(dat, Package!=pkg)
}
counts <- sort(table(dat$Package), decreasing = TRUE)
#### Get Imports ####
imports_all <- unique(
c(names(counts)[counts>=imports_thresh],
imports)
)
## Remove user-specified suggests
imports_all <- imports_all[!imports_all %in% suggests]
#### Get Suggests ####
suggests_all<- unique(
c(names(counts)[!names(counts) %in% imports_all],
suggests
)
)
deps <- list(Imports=imports_all,
Suggests=suggests_all)
if(isTRUE(add_newlines)){
deps <- lapply(deps, function(x){paste0("\n ",x)})
}
#### Return ####
if(length(which)==1){
return(deps[[which]])
} else {
return(deps[which])
}
}