Skip to content

Commit bb717f7

Browse files
author
maechler
committed
new option cache_user_dir for both available.packages() and install.packages()
git-svn-id: https://svn.r-project.org/R/trunk@87274 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 7c8a9c5 commit bb717f7

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

doc/NEWS.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
\item More R level messages use a common format containing
115115
\code{"character string"} for more consistency and less translation
116116
work.
117+
118+
\item \code{available.packages()} and \code{install.packages()} get
119+
an optional switch \code{cache_user_dir}, somewhat experimentally.
117120
}
118121
}
119122

src/library/utils/R/packages.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function(contriburl = contrib.url(repos, type), method,
2222
type = getOption("pkgType"), filters = NULL,
2323
repos = getOption("repos"),
2424
ignore_repo_cache = FALSE, max_repo_cache_age,
25-
quiet = TRUE, ...)
25+
cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)),
26+
quiet = TRUE, verbose = FALSE, ...)
2627
{
2728
if (!is.character(type))
2829
stop(gettextf("'%s' must be a character string", "type"), domain = NA)
@@ -67,7 +68,8 @@ function(contriburl = contrib.url(repos, type), method,
6768
if(ignore_repo_cache) {
6869
dest <- tempfile()
6970
} else {
70-
dest <- file.path(tempdir(),
71+
dest <- file.path(if(cache_user_dir) tools::R_user_dir("base", "cache")
72+
else tempdir(),
7173
paste0("repos_", URLencode(repos, TRUE), ".rds"))
7274
if(file.exists(dest)) {
7375
age <- difftime(timestamp, file.mtime(dest), units = "secs")
@@ -169,6 +171,8 @@ function(contriburl = contrib.url(repos, type), method,
169171
res0 <- cbind(res0[, fields, drop = FALSE], Repository = rp)
170172
res <- rbind(res, res0, deparse.level = 0L)
171173
}
174+
if(verbose) cat("added", NROW(res0), "packages, from repos", sQuote(repos),
175+
"to a total of", NROW(res), "\n")
172176
} ## end for(repos in *)
173177

174178
if(!length(res)) return(res)
@@ -628,6 +632,7 @@ new.packages <- function(lib.loc = NULL, repos = getOption("repos"),
628632

629633
installed.packages <-
630634
function(lib.loc = NULL, priority = NULL, noCache = FALSE,
635+
cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)),
631636
fields = NULL, subarch = .Platform$r_arch, ...)
632637
{
633638
if(is.null(lib.loc))
@@ -652,7 +657,9 @@ installed.packages <-
652657
## add length and 64-bit CRC in hex (in theory, seems
653658
## it is actually 32-bit on some systems)
654659
enc <- sprintf("%d_%s", nchar(base), .Call(C_crc64, base))
655-
dest <- file.path(tempdir(), paste0("libloc_", enc, ".rds"))
660+
dest <- file.path(if(cache_user_dir) tools::R_user_dir("base", "cache")
661+
else tempdir(),
662+
paste0("libloc_", enc, ".rds"))
656663
test <- file.exists(dest) &&
657664
file.mtime(dest) > file.mtime(lib) &&
658665
(val <- readRDS(dest))$base == base
@@ -664,6 +671,8 @@ installed.packages <-
664671
if(length(ret0)) {
665672
retval <- rbind(retval, ret0, deparse.level = 0L)
666673
## save the cache file
674+
dir.create(dirname(dest), recursive = TRUE,
675+
showWarnings = FALSE)
667676
saveRDS(list(base = base, value = ret0), dest)
668677
} else unlink(dest)
669678
}

src/library/utils/man/available.packages.Rd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ available.packages(contriburl = contrib.url(repos, type), method,
2020
type = getOption("pkgType"), filters = NULL,
2121
repos = getOption("repos"),
2222
ignore_repo_cache = FALSE, max_repo_cache_age,
23-
quiet = TRUE, \dots)
23+
cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)),
24+
quiet = TRUE, verbose = FALSE, \dots)
2425
}
2526
\arguments{
2627
\item{contriburl}{
@@ -55,8 +56,14 @@ available.packages(contriburl = contrib.url(repos, type), method,
5556
\item{max_repo_cache_age}{any cached values older than this in seconds
5657
will be ignored. See \sQuote{Details}.
5758
}
59+
\item{cache_user_dir}{\code{\link{logical}} indicating if caching should
60+
happen in \pkg{tools}' \code{\link[tools]{R_user_dir}("base", "cache")}
61+
instead of \code{\link{tempdir}()}.}
5862
\item{quiet}{logical, passed to \code{\link{download.file}()}; change
5963
only if you know what you are doing.}
64+
\item{verbose}{logical indicating if a \dQuote{progress report} line
65+
should be printed about the number of packages found in each repos.}
66+
% before filtering (TODO: *during* filt.)
6067
\item{\dots}{
6168
allow additional arguments to be passed from callers (which might be
6269
arguments to future versions of this function). Currently these are

src/library/utils/man/installed.packages.Rd

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/utils/man/installed.packages.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2022 R Core Team
3+
% Copyright 1995-2024 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{installed.packages}
@@ -12,7 +12,9 @@
1212
}
1313
\usage{
1414
installed.packages(lib.loc = NULL, priority = NULL,
15-
noCache = FALSE, fields = NULL,
15+
noCache = FALSE,
16+
cache_user_dir = str2logical(Sys.getenv("R_PACKAGES_CACHE_USER_DIR", FALSE)),
17+
fields = NULL,
1618
subarch = .Platform$r_arch, \dots)
1719
}
1820
\arguments{
@@ -27,7 +29,10 @@ installed.packages(lib.loc = NULL, priority = NULL,
2729
\code{c("base", "recommended")}. To select all packages without an
2830
assigned priority use \code{priority = NA_character_}.
2931
}
30-
\item{noCache}{Do not use cached information, nor cache it.}
32+
\item{noCache}{do not use cached information, nor cache it.}
33+
\item{cache_user_dir}{\code{\link{logical}} indicating if caching should
34+
happen in \pkg{tools}' \code{\link[tools]{R_user_dir}("base", "cache")}
35+
instead of \code{\link{tempdir}()}.}
3136
3237
\item{fields}{a character vector giving the fields to extract from
3338
each package's \file{DESCRIPTION} file in addition to the default

0 commit comments

Comments
 (0)