Skip to content

Commit bfedf7b

Browse files
authored
Add tool for querying multiple installed package versions (#871)
* add tool for querying multiple installed package versions * code review feedback * handle base package * don't try to get namespace info for base
1 parent 7175d83 commit bfedf7b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

crates/ark/src/modules/positron/llm_tools.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@
5353
res_list
5454
}
5555

56+
#' Get the version of installed packages
57+
#'
58+
#' This function retrieves the versions of specified packages in R.
59+
#'
60+
#' It returns a named list where the names are the package names and the values
61+
#' are the corresponding package versions, or "Not installed" if the package is
62+
#' not found.
63+
#'
64+
#' @export
65+
.ps.rpc.get_package_versions <- function(package_names, ...) {
66+
lapply(set_names(package_names), function(pkg) {
67+
if (is_on_disk(pkg)) {
68+
as.character(utils::packageVersion(pkg))
69+
} else {
70+
"Not installed"
71+
}
72+
})
73+
}
74+
5675
#' Get available vignettes for a package
5776
#'
5877
#' This function retrieves the vignettes available for a specified package in R.

crates/ark/src/modules/positron/utils.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,32 @@ obj_address <- function(x) {
152152
paste_line <- function(x) {
153153
paste(x, collapse = "\n")
154154
}
155+
156+
set_names <- function(x, names = x) {
157+
names(x) <- x
158+
x
159+
}
160+
161+
# From rlang
162+
is_on_disk <- function(pkg) {
163+
system_path(pkg) != ""
164+
}
165+
166+
system_path <- function(pkg) {
167+
# Important for this to be first because packages loaded with pkgload
168+
# will have a different path than the one in `.libPaths()` (if any).
169+
#
170+
# Note that this will not work for the base package, since we can't call
171+
# getNamespaceInfo on it.
172+
if (isNamespaceLoaded(pkg) && !identical(pkg, "base")) {
173+
return(.getNamespaceInfo(asNamespace(pkg), "path"))
174+
}
175+
176+
for (path in file.path(.libPaths(), pkg)) {
177+
if (file.exists(path)) {
178+
return(path)
179+
}
180+
}
181+
182+
""
183+
}

0 commit comments

Comments
 (0)