Skip to content

Commit

Permalink
Scan deps from parsnip::set_engine()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 4, 2024
1 parent 6aead87 commit b08b14b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
8 changes: 5 additions & 3 deletions R/scan-deps-queries.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ q_library_0 <- function() {
"module"
"tar_option_set"
"glue"
"ggsave"))',
"ggsave"
"set_engine"))',
'((call function:
(namespace_operator
lhs: (identifier) @ns-name
Expand All @@ -20,7 +21,7 @@ q_library_0 <- function() {
) @dep-code
(#any-of? @ns-name
"base" "xfun" "pacman" "modules" "import" "box" "targets" "glue"
"ggplot2")
"ggplot2" "parsnip")
(#any-of? @fn-name
"library" "require" "loadNamespace" "requireNamespace"
"pkg_attach" "pkg_attach2"
Expand All @@ -30,7 +31,8 @@ q_library_0 <- function() {
"use"
"tar_option_set"
"glue"
"ggsave"))'
"ggsave"
"set_engine"))'
), names = rep("q_library_0", 2))
}

Expand Down
43 changes: 39 additions & 4 deletions R/scan-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ re_r_dep <- paste0(collapse = "|", c(
"box",
"tar_option_set",
"glue",
"ggsave"
"ggsave",
"set_engine"
))

scan_path_deps <- function(path) {
Expand Down Expand Up @@ -161,7 +162,7 @@ scan_path_deps_do_gen_hits <- function(hits, path) {
}
)
pkgs <- lapply(seq_along(code), function(i) {
parse_pkg_from_call(ns[i], fn[i], code[i])
safe_parse_pkg_from_call(ns[i], fn[i], code[i])
})
pkgs_count <- lengths(pkgs)
data_frame(
Expand Down Expand Up @@ -193,6 +194,7 @@ prot_glue_glue <- function(
..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}") {
}
prot_ggplot2_ggsave <- function(filename, ...) { }
prot_parsnip_set_engine <- function(object, engine, ...) { }

safe_parse_pkg_from_call <- function(ns, fn, code) {
tryCatch(
Expand All @@ -219,7 +221,8 @@ parse_pkg_from_call <- function(ns, fn, code) {
"use" = prot_box_use,
"tar_option_set" = prot_targets_tar_option_set,
"glue" = prot_glue_glue,
"ggsave" = prot_ggplot2_ggsave
"ggsave" = prot_ggplot2_ggsave,
"set_engine" = prot_parsnip_set_engine
)
matched <- match.call(fun, expr, expand.dots = FALSE)
switch(fn,
Expand All @@ -244,7 +247,9 @@ parse_pkg_from_call <- function(ns, fn, code) {
"glue" =
parse_pkg_from_call_glue(ns, fn, matched),
"ggsave" =
parse_pkg_from_call_ggplot2(ns, fn, matched)
parse_pkg_from_call_ggplot2(ns, fn, matched),
"set_engine" =
parse_pkg_from_call_parsnip(ns, fn, matched)
)
}

Expand Down Expand Up @@ -426,6 +431,36 @@ parse_pkg_from_call_ggplot2 <- function(ns, fn, matched) {
NULL
}

parse_pkg_from_call_parsnip <- function(ns, fn, matched) {
if (!is.na(ns) && ns != "parsnip") return(NULL)
engine <- matched[["engine"]]
if (!is.character(engine) || length(engine) != 1L) {
return(NULL)
}

map <- getOption("renv.parsnip.engines", default = list(
glm = "stats",
glmnet = "glmnet",
keras = "keras",
kknn = "kknn",
nnet = "nnet",
rpart = "rpart",
spark = "sparklyr",
stan = "rstanarm"
))

pkgs <- if (is.function(map)) {
map(engine)
} else {
map[[engine]]
}

if (length(pkgs) > 0) {
return(pkgs)
}
NULL
}

# -------------------------------------------------------------------------

scan_path_deps_do_rmd <- function(code, path) {
Expand Down

0 comments on commit b08b14b

Please sign in to comment.