Installation of non-CRAN package requires CRAN mirror - r

I'm trying to install the INLA package of the R-INLA project on a Linux based computing cluster. The package is not on CRAN. According to their tutorial, it suffices to use
install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
for installation, which works perfectly fine on my local machine. However, on the computing cluster, seemingly a CRAN mirror is required to run this command and I get the following error:
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: install.packages -> startsWith -> contrib.url
Execution halted
As an alternative, I tried to install the package directly from the corresponding GitHub repository using devtools. For some reason, this gives me the obviously incorrect version number INLA_99.99.9999. This prohibits me from manually adding the necessary binaries via INLA:::inla.binary.install() as the version number is not found. Any help is appreciated!

You are relying on the (R global) options() having a valid repos entry on the cluster.
Which you ... cannot as base R ships (in source from) without such as base R Core feels, rightly or wrongly, that they cannot play favourites and set one. Some of us think that is wrong (as it diminishes the user experience -- like yours here) so in the Debian (and hence Ubuntu) package I set this to the 'cloud' mirror everybody is close to as it is on a CDN:
edd#rob:~$ tail -6 /usr/lib/R/etc/Rprofile.site
## We set the cloud mirror, which is 'network-close' to everybody, as default
local({
r <- getOption("repos")
r["CRAN"] <- "https://cloud.r-project.org"
options(repos = r)
})
edd#rob:~$
I suggest you do the same, maybe in ~/.Rprofile, on the cluster.

Related

R 3.5 package ‘lattice’ was installed by an R version with different internals

I updated R 3.4.4 to R 3.5.0 today. And my package cannot pass R CMD check anymore. It fails at checking whether package can be installed ... ERROR.
In another hand my package works, I can install it and use it as long as I don't check it.
The error is the following:
Error: package or namespace load failed for ‘sp’:
package ‘lattice’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version
Error : package ‘sp’ could not be loaded
I tried to reinstall lattice these ways:
sudo apt-get --reinstall install r-cran-lattice
or
remove.packages("lattice", lib="~/R/x86_64-pc-linux-gnu-library/3.5")
install.package("lattice")
The package lattice is installed correctly in both cases. But it does not solve my problem. Also I ensured to have a single version of lattice removing either the r-cran one or the self compiled version. Nothing works.
Edit By the way I can do library(sp) or library(lattice) it works. But not with R CMD check
The error message tells you that the binary you are attempting to install comes from an insufficient / incompatible R version.
Two fixes:
Install from source as you did. After that check with AP <- available.packages() that you really only have one.
Use the correct binarie: read this README at CRAN and switch to Michael's 'R 3.5' repos. Many of us have been doing that for weeks.
Lastly, the r-sig-debian list is a friendly place for these questions and more. Subscribe first so that you can post.

Having issues installing an R package from Github

Please be patient with me as I'm a total noob, but I'm really trying to learn.
I'm trying to make a choropleth map for my country, and found an R package on Github that handles it excellently. However, I'm working on a university computer and I don't have write privileges on any drive but M://, so whenever the package tries to install on C:// it obviously throws an error. This hasn't been a problem since I can just specify a libpath as an argument on install.packages, but devtools::install_github does not seem to have such an argument.
I tried using
with_libpaths(new = "M:\R\win-library\3.2", install_github('diegovalle/mxmaps'))
But I got an error message saying
with_libpaths' is deprecated. Use 'withr::with_libpaths' instead.
I take this to mean that I need to install the "withr" package in order to use that? However, I keep getting errors when trying to install that package. First, I got
Warning in install.packages : installation of package ‘withr’ had
non-zero exit status
because of the not having access to C:// issue. I usually bypass this by installing directly from the binaries, but when I try that it tells me
"Warning in install.packages : package ‘withr’ is not available (for R
version 3.2.2)".
Other than updating my version of R (which will be a nighmare since I don't have installation privileges on this machine), how else can I either install withr or find another way to specify the directory to install the package from github?
I would suggest you to go with latest version of 3.4.All above mentioned packages are available under latest version.
Two ways to set local library paths (at least on Linux running R 3.5.3):
(1) At the beginning of your script, set the the .libPaths option to your local library path, i.e.: .libPaths("M:\R\win-library\3.2")
(2) Add to your .Renviron file a line that specifies your local library path: i.e.: R_LIBS="M:\R\win-library\3.2"
Note: For (1) You will need to manually run every time you start a new R session, whereas (2) will be set automatically when R starts.

Problems with installation R packages

I'm a Windows user. A few weeks ago I installed R and Rstudio along with many packages. Today there was a message that new packages were not installed.
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/src/contrib:
cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/src/contrib/PACKAGES'
I reinstalled R but this did not solve the problem.
Warning in install.packages :
InternetOpenUrl failed: 'Can not connect to server'
This is something that pops up in R and RStudio only once in a while. RStudio changes quite a few settings, and the option "repos" is one of them. On Windows, the following is added
EDIT: It's not RStudio adding this extra repository. The repository is kindly provided by Dr. Brian Ripley for packages that for some reason can't be made available on CRAN (license, not building out of the box, requiring additional software, ...). This is called "CRANextra" in the settings:
> getOption("repos")
CRAN CRANextra
"https://cran.rstudio.com/" "http://www.stats.ox.ac.uk/pub/RWin"
attr(,"RStudio")
[1] TRUE
So RStudio tries to access a specific repository when run on Windows, but that repository has had some connection issues in the past; it isn't always reachable, and when it's not, the warnings you report are issued.
You can get this warning to stop by resetting this option:
options(repos = "https://cran.rstudio.com") # or a repo of your choice.
Which allows you to install packages without the warning:
> install.packages("fortunes")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/fortunes_1.5-4.zip'
Content type 'application/zip' length 202721 bytes (197 KB)
downloaded 197 KB
package ‘fortunes’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Joris\AppData\Local\Temp\Rtmpu0febg\downloaded_packages
Even when this warning is displayed, packages still get installed from the rstudio CRAN mirror. The warning is reported as a bug, and RStudio has promised to tackle it soon.
EDIT: More information on the CRANextra repository in R FAQ (last paragraph):
Some CRAN packages that do not build out of the box on Windows,
require additional software, or are shipping third party libraries for
Windows cannot be made available on CRAN in form of a Windows binary
packages. Nevertheless, some of these packages are available at the
“CRAN extras” repository at https://www.stats.ox.ac.uk/pub/RWin/
kindly provided by Brian D. Ripley. Note that this repository is a
default repository for recent versions of R for Windows.
In the mean while R Studio fixes the bug, a temporal solution for not having to especifie the CRAN repository every time we start an R session, is to edit your "Rprofile.site" file and add this line
options(repos = getOption("repos")["CRAN"])
That way every time an R session is started the CRANextra repository ("http://www.stats.ox.ac.uk/pub/RWin") is removed automatically. Just remember to delete or comment with # this line after the bug is solved.
I was facing similar issue and the fix that worked for me is that, in RStudio I've opened tools -> Global Options -> Packages -> Primary CRAN Repository -> Set Global.
Also make sure you're not using installed.packages this happens to a lot of people because of RStudio typing suggestions.
You need to use install.packages("<package_name>")

install.packages fails in knitr document: "trying to use CRAN without setting a mirror"

Using the following code I got the data I wanted, but for some reason I can't figure out knitr doesn't let me compile a PDF document, as shown further below:
My code:
install.packages("weatherData")
library(weatherData)
istanbul <- getWeatherForDate("Istanbul",
start_date = Sys.Date() - 41,
end_date = Sys.Date())
Works out with no problem but I get the following message trying compile the PDF:
Quitting from lines 3-31 (ist_weather.spin.Rmd)
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... eval -> eval -> install.packages -> grep -> contrib.url
Execution halted
Knitr produces a R session, without a default cran mirror unless you specifically asked for one. We tend to forget we need to set up CRAN for every R session when we use Rstudio because it takes care of it, but only for interactive use, not for knitr.
You could try specifying a mirror as a install.packages argument:
install.packages("weatherData",repos = "http://cran.us.r-project.org")
Alternatively, you could set up your default CRAN mirror in your .Rprofile. See this answer.
That said, it is not a good idea to install packages through a knitr document that you will probably compile several times. You should assume people know how to install a missing package if needed, or at least test whether the package is installed before installing it again
if(!require(weatherData)) install.packages("weatherData",repos = "http://cran.us.r-project.org")
You must set the CRAN repository in your R. To do so, launch R or RStudio. in the R terminal run following codes.
r = getOption("repos")
r["CRAN"] = "http://cran.us.r-project.org"
options(repos = r)
install.packages("weatherData")
Above code defines CRAN repository in the R and in next package installation no need to define again.
Alternative way is to simply run install.packages("weatherData", repos="http://cran.us.r-project.org"). However, with the second solution the repository not set and you must pass it as a parameter in every package installation.
Honestly,
It would not work for me because I installed packages, and these lines of code were interrupting the knit function. When I removed all lines containing installing packages (and used the most updated R and R Markdown available) this error went away.

Install a local R package with dependencies from CRAN mirror

I have built an R package, i.e. I have the mypackage.tar.gz file. This package depends on several other packages, all downloadable and installable from any CRAN mirror.
Now I want to install this package on a system where the dependencies are not yet installed, and I would like that the dependencies will be downloaded and installed automatically when I install my package.
I tried:
install.packages("mypackage.tar.gz",type="source",dependencies=TRUE,repos="http://a.cran.mirror")
but it searches for mypackage.tar.gz on the mirror (and obviously it does not find), while if I set repos=NULL it correctly tries to install the local package file (as documented), but obviously it does not find the dependencies packages.
So my question is: is there a way to perform a 'mixed' installation (local package with online dependencies) or the only way to do is to manually install all the dependencies?
You could use install from the devtools package. Just run install("<directory of your package>", dependencies = TRUE). Its help states:
Uses R CMD INSTALL to install the package. Will also try to install dependencies of the package from CRAN, if they're not already installed.
Here, I'm using untar() with devtools::install() and passing in a directory to which the source tarball has been extracted.
d <- tempdir()
untar("mypackage.tar.gz", compressed="gzip", exdir=d)
devtools::install(file.path(d, "mypackage"), dependencies=TRUE,
repos="https://cloud.r-project.org/")
If you want to install from multiple repos, you can provide a list of them. For example, to use both Bioconductor and CRAN, you could run:
devtools::install(file.path(d, "mypackage"), dependencies=TRUE,
repos=BiocManager::repositories())
NOTE: I can't figure out how to directly pass the tarball to install(), but this solution works in the meantime and leaves no clutter because we extract to a temp directory. It seems install_local() should be able to take a tarball, but I am getting an error when attempting to do so.
If you already have installed your local package, you should be able to use a couple functions in tools to install the dependencies from CRAN:
library('tools')
installFoundDepends(pkgDepends('mypackage', local = FALSE)$Found)
Note: You can pass args (like repos) through installFoundDepends to install.packages.
You can also use the Depends element from the pkgDepends output to pass directly to install.packages:
install.packages(pkgDepends('mypackage')$Depends)
UPDATE: Apparently it is not possible to install a local package with dependencies=FALSE. This seems odd, since you can do that for a remote package from a repository. The reason (looking at the source code) is that if(is.null(repos) & missing(contriburl)), installation is handled via system calls to R CMD INSTALL, which has no dependency-related arguments.
So old question and so many answers but unfortunately none of them presents the canonical way to address the problem.
R was designed to handle situations like this, no extra packages are needed. One has to create local repository, and then use it, together with CRAN url, as a repository source when installing.
Below is code that present complete process.
## double check our dependency is not yet installed
## remove.packages("data.table")
"data.table" %in% rownames(installed.packages())
#[1] FALSE
## create our pkg
hello = function() "world"
package.skeleton(name="pkg", list="hello")
#...
cat("Imports: data.table\n", file="pkg/DESCRIPTION", append=TRUE)
unlink(c("pkg/Read-and-delete-me", "pkg/man"), recursive=TRUE)
rm(hello)
## publish our pkg in current working directory
system("R CMD build pkg")
#...
dir.create("src/contrib", recursive=TRUE)
file.rename("pkg_1.0.tar.gz", "src/contrib/pkg_1.0.tar.gz")
#[1] TRUE
tools::write_PACKAGES("src/contrib")
## install pkg and its dependencies automatically
install.packages("pkg", repos=c(
paste0("file://", getwd()),
"https://cloud.r-project.org"
))
#Installing package into '/home/jan/R/x86_64-pc-linux-gnu-library/4.2'
#(as 'lib' is unspecified)
#also installing the dependency 'data.table'
#...
## test
library(pkg)
hello()
#[1] "world
"data.table" %in% rownames(installed.packages())
#[1] TRUE
On windows one may need to specify type="source" and amend paths.
If you are not opposed to using another package who manages this for you, this can nowadays be easily achieved with the {remotes} package.
install.packages("remotes")
remotes::install_local("mypackage.tar.gz")
You can specify some further options which dependencies you want (e.g. also those in 'Suggests') etc.:
?remotes::install_local
{remotes} itself does not have dependencies afaik, so it does not add too much clutter to your environment.

Resources