Closed krlmlr closed 1 year ago
I had thought about it but didn't find a satisfying solution at the time so skipped it.
A few ways to do it :
grep(pattern, getNamespaceExports("pkg"), value = TRUE)
, have an example to show the use case. This limits to a specific package, but is most likely not really a limitation in practice?stringr::regex()
, so we can use the class to sort things out, force user to use lists and not vectors so we don't lose the attributes by mistake. Adds a dependency (though maybe only Suggests
). We can't specify the package though. We can use the comments
arg to feed a package, e.g. stringr::regex("_tbl$", comments = "dm")
but it's a hack.fun_find(pattern, pkg)
(with a better name hopefully) which does more or less what grep(pattern, getNamespaceExports("pkg"), value = TRUE)
does.I think we can have pkg_subset(pkg, subset = c("all", "exports", "imports", "reexports", ...), pattern = NULL, predicate = NULL, value = FALSE, prefix = TRUE)
which would return a character vector of "pkg::fun"
and "pkg:::fun"
strings (or just "fun" if prefix is FALSE
, or a named list if value
is TRUE
).
This would be a fairly useful utility in general too.
predicate
is applied on the packaged object so we can restrict to functions, to S3 methods, to functions that have a given class etc.
subset
would use getNamespaceExports
, getNamespaceImports
, ls(asNamespace(pkg))
etc so we can easily be restricted to a category of functions. Maybe S3 and S4 methods, low level language wrappers, runtime defined functions etc could be there too.
pkg_subset()
would be a useful function, but better in its own package, one that would define package objects and utilities to manipulate them ideally. Not really in scope here.
I think the easiest, since we don't use names yet, it to treat differently unnamed element and element named "pattern", the latter define a regex, no name means "fixed".
I would also like to support promoting/demoting full packages, so we can use the package
name too.
An issue is that with 3 ways of selecting we might have an intersection between demote and promote for instance, the result is ambiguous in this case.
It makes sense that explicitly provided names have priority over regex, but what about packages ?
Also what about restricting a pattern to a given package.
@krlmlr see PR description for a way to handle it from now on
Should
trim
,promote
,demote
andhide
support regular expressions? How to distinguish regexes from length-one character vectors?