R: apt-get install r-cran-foo vs. install.packages( foo )
It's not as easy as it seems.
apt-get update
is good if and whenpackages exist -- but there are only around 150 or so
r-cran-*
packages out of a pool of 2100+ packages on CRAN, so rather sparse coveragepackages are maintained, bug free and current
you are happy enough with the bi-annual releases by Ubuntu
install.packages()
and laterupdate.packages()
is good if and whenyou know what it takes to have built-time dependencies (besides
r-base-dev
) installedyou don't mind running
update.packages()
by hand as well as theapt-get
updates.
On my Ubuntu machine at work, I go with the second solution. But because the first one is better if you have enough coverage, we have built cran2deb which provides 2050+ binary deb packages for amd64 and i386 --- but only for Debian testing. That is what I use at home.
As for last question of whether you 'should you expect trouble': No, because R_LIBS_SITE
is set in /etc/R/Renvironment
to be
# edd Apr 2003 Allow local install in /usr/local, also add a directory for
# Debian packaged CRAN packages, and finally the default dir
# edd Jul 2007 Now use R_LIBS_SITE, not R_LIBS
R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:\
/usr/lib/R/site-library:/usr/lib/R/library'}
which means that your packages go into /usr/local/lib/R/site-library
whereas those managed by apt
go into /usr/lib/R/site-library
and (in the case of base packages) /usr/lib/R/library
.
Hope that clarifies matters. The r-sig-debian mailing list is a more informed place for questions like this.
R, install package from source vs. use install.package() and CRAN mirror
You hit an important overall question -- "should I install from a binary repository (where available) or should I install from source" -- but I fear you are a little confused about other aspects:
You rightly point to the Debian and Ubuntu READMEs. Ubuntu is particularly useful as Michael's (off-CRAN) PPA repos provide about 3200 package that are directly installable as binaries. Nothing is faster.
But that is for Ubuntu and you run CentOS. No soup for you.
So source installation it is.
And source installation is always from a .tar.gz and the various methods you list (command-line,
install.packages()
, ...) and alternative (Package tab in RStudio, say) are all equivalent as the all call the same underlying function.
It is really just different veneer for convenience.
Errors during installation of R dependencies after new R installation on Ubuntu 18.04
After trying the following (purging and reinstalling R between each step):
apt purge r-base
- Added/removed several different PPAs before realizing none of them supported Bionic Beaver. Result:
Release not found
. - Found Michael Rutter's PPA and attempted to use that. Result: same error message from above (
'CURL_OPENSSL_3' not found
). - Attempting several solutions to this SO article. Result: same error message from above (
'CURL_OPENSSL_3' not found
). Read this article and performed the following in R:
> remove.packages("curl")
> install.packages("curl")
> install.packages(c("dplyr", "lubridate", "qcc", "forecast", "stringi", "curl"), repos='http://cran.us.r-project.org')
And everything worked as intended.
How should I deal with package 'xxx' is not available (for R version x.y.z) warning?
1. You can't spell
The first thing to test is have you spelled the name of the package correctly? Package names are case sensitive in R.
2. You didn't look in the right repository
Next, you should check to see if the package is available. Type
setRepositories()
See also ?setRepositories.
To see which repositories R will look in for your package, and optionally select some additional ones. At the very least, you will usually want CRAN
to be selected, and CRAN (extras)
if you use Windows, and the Bioc*
repositories if you do any biological analyses.
To permanently change this, add a line like setRepositories(ind = c(1:6, 8))
to your Rprofile.site
file.
3. The package is not in the repositories you selected
Return all the available packages using
ap <- available.packages()
See also Names of R's available packages, ?available.packages.
Since this is a large matrix, you may wish to use the data viewer to examine it. Alternatively, you can quickly check to see if the package is available by testing against the row names.
View(ap)
"foobarbaz" %in% rownames(ap)
Alternatively, the list of available packages can be seen in a browser for CRAN, CRAN (extras), Bioconductor, R-forge, RForge, and GitHub.
Another possible warnings message you may get when interacting with CRAN mirrors is:
Warning: unable to access index for repository
Which may indicate the selected CRAN repository is currently be unavailable. You can select a different mirror with chooseCRANmirror()
and try the installation again.
There are several reasons why a package may not be available.
4. You don't want a package
Perhaps you don't really want a package. It is common to be confused about the difference between a package and a library, or a package and a dataset.
A package is a standardized collection of material extending R, e.g. providing code, data, or documentation. A library is a place (directory) where R knows to find packages it can use
To see available datasets, type
data()
5. R or Bioconductor is out of date
It may have a dependency on a more recent version of R (or one of the packages that it imports/depends upon does). Look at
ap["foobarbaz", "Depends"]
and consider updating your R installation to the current version. On Windows, this is most easily done via the installr
package.
library(installr)
updateR()
(Of course, you may need to install.packages("installr")
first.)
Equivalently for Bioconductor packages, you may need to update your Bioconductor installation.
source("http://bioconductor.org/biocLite.R")
biocLite("BiocUpgrade")
6. The package is out of date
It may have been archived (if it is no longer maintained and no longer passes R CMD check
tests).
In this case, you can load an old version of the package using install_version()
library(remotes)
install_version("foobarbaz", "0.1.2")
An alternative is to install from the GitHub CRAN mirror.
library(remotes)
install_github("cran/foobarbaz")
7. There is no Windows/OS X/Linux binary
It may not have a Windows binary due to requiring additional software that CRAN does not have. Additionally, some packages are available only via the sources for some or all platforms. In this case, there may be a version in the CRAN (extras)
repository (see setRepositories
above).
If the package requires compiling code (e.g. C, C++, FORTRAN) then on Windows install Rtools or on OS X install the developer tools accompanying XCode, and install the source version of the package via:
install.packages("foobarbaz", type = "source")
# Or equivalently, for Bioconductor packages:
source("http://bioconductor.org/biocLite.R")
biocLite("foobarbaz", type = "source")
On CRAN, you can tell if you'll need special tools to build the package from source by looking at the NeedsCompilation
flag in the description.
8. The package is on GitHub/Bitbucket/Gitorious
It may have a repository on GitHub/Bitbucket/Gitorious. These packages require the remotes
package to install.
library(remotes)
install_github("packageauthor/foobarbaz")
install_bitbucket("packageauthor/foobarbaz")
install_gitorious("packageauthor/foobarbaz")
(As with installr
, you may need to install.packages("remotes")
first.)
9. There is no source version of the package
Although the binary version of your package is available, the source version is not. You can turn off this check by setting
options(install.packages.check.source = "no")
as described in this SO answer by imanuelc and the Details section of ?install.packages
.
10. The package is in a non-standard repository
Your package is in a non-standard repository (e.g. Rbbg
). Assuming that it is reasonably compliant with CRAN standards, you can still download it using install.packages
; you just have to specify the repository URL.
install.packages("Rbbg", repos = "http://r.findata.org")
RHIPE
on the other hand isn't in a CRAN-like repository and has its own installation instructions.
installing R packages on ubuntu 8.10
The directory /usr/share/local/lib/R
is the default location; the directory is has ownership root:staff
by default. If you add yourself to group staff
(easiest: by editing /etc/group
and /etc/gshadow
) you can write there and you do not need sudo
powers for the installation of packages. That is what I do.
Alternatively, do apt-get install littler
and copy the example file /usr/share/doc/littler/examples/install.r
to /usr/local/bin
and chmod 755
it. The you can just do sudo install.r lattice ggplot2
to take two popular examples.
BTW Ubuntu 8.1 does not exist as a version. Maybe you meant 8.10? Consider upgrading to 9.10 ...
Edit: Also have a look at this recent SO question.
Why do different operating systems install packages from CRAN differently?
In general, Windows binaries will work on all versions of Windows.
Ditto for the key / current versions of MacOS: the provided binaries work.
Linux, sadly, is more complicated because the different distros lay things out differently. Something I build on Ubuntu or Debian (or, more specifically, a particular release version thereof) may not even work on other releases of the same distro, let alone other distro. In some cases you can get binaries. At some point I (co-)owned a build service for all of CRAN, but it died/broke. All doable with effort, but ... some effort.
So from source it is. That use to be the standard anyway which "Unix" was a catch-all phrase covering SunOS/Solaris, AIX, *BSD, SGI and on and on. Often even with different processors. So source.
There have been attempts to provide 'universal binaries': flatpack and snap are two more recent examples. And then there is of course Docker.
Difference between installing a package from source and from compiled binary
I already provided a somewhat detailed answer in response to this SO question.
As an update, these days you even have lots of packages prebuilt thanks to updated cran2deb initiaives:
On Ubuntu you now have almost all CRAN packages prebuilt via Michael Rutter's 'cran2deb for ubuntu' ppa on Launchpad.
For Debian, Don Armstrong now provides a similar service (also covering BioConductor and OmegaHat) at debian-r.debian.net.
Elegant way to check for missing packages and install them?
Yes. If you have your list of packages, compare it to the output from installed.packages()[,"Package"]
and install the missing packages. Something like this:
list.of.packages <- c("ggplot2", "Rcpp")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
Otherwise:
If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.
Related Topics
Can't Loop with R's Leaflet Package to Produce Multiple Maps
Range Standardization (0 to 1) in R
Round a Posix Date (Posixct) with Base R Functionality
Extracting Unique Rows from a Data Table in R
Ggplot X-Axis Labels with All X-Axis Values
Change Colours of Particular Bars in a Bar Chart
Get a List of the Data Sets in a Particular Package
How to Change .Libpaths() Permanently in R
Can the Value.Var in Dcast Be a List or Have Multiple Value Variables
Changing Values When Converting Column Type to Numeric
How to Tell the R Interpreter How to Use the Proxy Server
Finding Overlap in Ranges with R
Why (Or When) Is Rscript (Or Littler) Better Than R Cmd Batch
Row Sums Over Columns with a Certain Pattern in Their Name
Identify Duplicates and Mark First Occurrence and All Others