Is there a way to detect the closest R repository? This doesn’t have to be perfect just reasonably close.
I’ll discuss the big picture as there may be a better way of going about what I’m doing. I’m writing a package that uses available.packages().
This throws up an error when I check the package saying:
Error in contrib.url(getOption("repos"), type):
trying to use CRAN without setting a mirror.
Calls: p_cran -> available.packages -> contrib.url
Execution is halted
My thoughts are to set the mirror temporary using something like:
x <- getOption("repos")
#SOME HOW TEST IF x IS NOT SET BUT NOT SURE HOW
if (is.not.set(x)){
y <- get.local.mirror.function()
options(repos=structure(c(CRAN=y)))
on.exit(options(repos=structure(c(CRAN=x))))
}
So I guess I have two problems to overcome if this route is reasonable:
- Determine if the
reposhas been set - Find closest or some default to put in as the repos
Thank you in advance for help figuring this out.
The tricky part of this problem isn’t getting the locations of the R repositories – it should be pretty easy to keep a constant that records where the major ones are. The tricky part is finding the user’s own location.
What I’d suggest is to examine timezone settings. You can read
Sys.timezone()directly and apply to a lookup table, but that’s a string and it might be a pain. I’d instead try to find the UTC offset, by, e.g.Then you can look up the offset against some kind of table, and match each band to some repository. Be sure to consider the case where a repository is down, or something…
To be fair, you can probably get pretty reasonable results by just selecting a random repository. (Maybe excluding some of the ones out in the middle of nowhere…)
Determining whether repos have been set is a simple matter of looking up
options("repos")orgetOption("repos"). From?options:So
(getOption("repos")[1] == "@CRAN@")should work.