How to track outdated methods? #1273
-
Help
DescriptionIf I define methods within my custom functions, how can I make targets know the corresponding dependencies? Moreover, if there is an option to do that, are the generic's dependencies outdated only when the corresponding outdated methods are used, or is the whole generic outdated every time one of its methods is outdated (so all the subsequent targets will be re-evaluated)? library(targets)
tar_dir({
tar_script({
f <- function(x) {
UseMethod("f", x)
}
f.numeric <- function(x) 2
f.character <- function(x) 0
list(
tar_target(x, f(3)),
tar_target(y, f("3"))
)
}, ask = FALSE)
tar_make()
print(paste0("x: ", tar_read(x)))
print(paste0("y: ", tar_read(y)))
tar_script({
f <- function(x) {
UseMethod("f", x)
}
f.numeric <- function(x) 22 # outdated function
f.character <- function(x) 0 # unchanged function
list(
tar_target(x, f(3)), # this should be updated
tar_target(y, f("3")) # this shouldn't be updated
)
}, ask = FALSE)
print(tar_visnetwork())
tar_make()
print(paste0("x: ", tar_read(x)))
print(paste0("y: ", tar_read(y)))
})
#> ▶ dispatched target x
#> ● completed target x [0 seconds]
#> ▶ dispatched target y
#> ● completed target y [0 seconds]
#> ▶ ended pipeline [0.094 seconds]
#> [1] "x: 2"
#> [1] "y: 0"
#> ✔ skipped target x
#> ✔ skipped target y
#> ✔ skipped pipeline [0.094 seconds]
#> [1] "x: 2"
#> [1] "y: 0" Created on 2024-04-26 with reprex v2.1.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
targets
operates by static code analysis, and there is no reliable static way to detect what's going on with S3 methods. For example, isdata.frame()
an ordinary function, or isdata()
an S3 generic with a method for class"frame"
? It's impossible to tell just from a static analysis of which global variables are used where, and even if it were, static analysis cannot tell which method will apply to a given object. S3 methods and other OOP are usually for package development, and it would not be feasible fortargets
to specifically accommodate them.