R packages: Difference between revisions

From 太極
Jump to navigation Jump to search
 
(90 intermediate revisions by the same user not shown)
Line 1: Line 1:
= R package management =
= R package management =
== Challenges in Package Management ==
[https://www.rstudio.com/blog/challenges-in-package-management/ Challenges in Package Management]
== Packages loaded at startup ==
== Packages loaded at startup ==
''getOption("defaultPackages")''
''getOption("defaultPackages")''
Line 8: Line 11:


=== utils::install.packages() ===
=== utils::install.packages() ===
* By default, install.packages() will check versions and install uninstalled packages shown in ''' 'Depends', 'Imports' ''', and ''' 'LinkingTo' ''' fields. See [http://cran.r-project.org/doc/manuals/r-release/R-exts.html R-exts] manual.
* By default, install.packages() will check versions and install uninstalled packages shown in ''' 'Depends', 'Imports' ''', and ''' 'LinkingTo' ''' (<span style="color: red">DIL</span>) fields. See [http://cran.r-project.org/doc/manuals/r-release/R-exts.html R-exts] manual.
* Take advantage of '''Ncpus''' parameter in [https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/install.packages install.packages()]
* Take advantage of '''Ncpus''' parameter in [https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/install.packages install.packages()]
* If we want to install packages listed in 'Suggests' field, we should specify it explicitly by using ''dependencies'' argument:
* If we want to install packages listed in 'Suggests' field, we should specify it explicitly by using ''dependencies'' argument:
Line 108: Line 111:


=== pak ===
=== pak ===
https://cran.r-project.org/web/packages/pak/index.html
* https://cran.r-project.org/web/packages/pak/index.html. 'pak' supports CRAN, 'Bioconductor' and 'GitHub' packages. An example about the [https://tidymodels.github.io/censored/index.html censored] package.
* When I use rsconnect::deployApp() to deploy a shiny app to shinyapps.io, the terminal emits the message ''The following package(s) were installed from sources, but may be available from the following remotes: - pak  [r-lib/pak]''. So it seems shinyapps.io uses the [https://github.com/r-lib/pak pak] to install packages.
* [https://www.r-bloggers.com/2023/08/three-four-r-functions-i-enjoyed-this-week/ How does this package depend on this other package?]


=== BiocManager ===
=== BiocManager ===
Line 120: Line 125:


=== Ubuntu ===
=== Ubuntu ===
* [http://dirk.eddelbuettel.com/blog/2017/12/22/ Finding Binary .deb Files for CRAN Packages]
<ul>
* Add an additional binary source to your source.lists file, see for example, the line in [https://github.com/rocker-org/rocker/blob/master/r-ubuntu/Dockerfile#L27 rocker]. This is mentioned in [https://www.jumpingrivers.com/blog/faster-r-package-installation-rstudio/ Faster R package installation].
<li>[http://dirk.eddelbuettel.com/blog/2017/12/22/ Finding Binary .deb Files for CRAN Packages]
* [http://dirk.eddelbuettel.com/blog/2020/06/22/#027_ubuntu_binaries R and CRAN Binaries for Ubuntu] by Dirk Eddelbuettel
<li>Add an additional binary source to your source.lists file, see for example, the line in [https://github.com/rocker-org/rocker/blob/master/r-ubuntu/Dockerfile#L27 rocker]. This is mentioned in [https://www.jumpingrivers.com/blog/faster-r-package-installation-rstudio/ Faster R package installation].
<li>[http://dirk.eddelbuettel.com/blog/2020/06/22/#027_ubuntu_binaries R and CRAN Binaries for Ubuntu] by Dirk Eddelbuettel
* [https://eddelbuettel.github.io/r2u/ r2u: R Binaries for Ubuntu]; see [https://twitter.com/eddelbuettel/status/1523376755548979201 The new #CRANapt repo has 19000 .deb binaries  (and 200+ from BioConductor) for each with full dependencies and `apt` integration.]
** [https://www.brodrigues.co/blog/2022-10-29-mkusb_minp/ A Linux Live USB as a statistical programming dev environment]
** [https://yegrug.github.io/meetups/2022-10-27/ October 27, 2022: r2u, binary packages on Ubuntu]
* [https://enchufa2.github.io/rspm/ rspm]. [https://www.enchufa2.es/archives/rspm-easy-access-to-rspm-binary-packages-with-automatic-management-of-system-requirements.html {rspm}: easy access to RSPM binary packages with automatic management of system requirements]
* [https://cloud.r-project.org/web/packages/bspm/index.html bspm] R package (cf '''rocker/r-rspm''' docker image which is not an R package). 5 lines. After that, "install.packages()" will trigger "sudo apt-get install". Note: it seems the ''apt-add-repository'' commands will break the ''apt update'' command on '''Debian 11'''. After a successful installation on Ubuntu OS, I'll see the following messages every time I started R,
<pre>
Loading required package: utils
Tracing function "install.packages" in package "utils"
</pre>
Also [https://github.com/Enchufa2/bspm/issues/43 for some reason], I cannot remove packages that were just installed. Not sure if the same problem happened if I use '''rig''' (based on rspm).
<pre>
> install.packages("fgsea") # Works fine
> remove.packages("fgsea")
Removing package from ‘/home/brb/R/x86_64-pc-linux-gnu-library/4.2’
(as ‘lib’ is unspecified)
Error in find.package(pkgs, lib) : there is no package called ‘fgsea’
> packageVersion("fgsea")
[1] ‘1.22.0’
</pre>
Since the ''bspm::enable()'' function enables the integration of '''install_sys()''' into '''install.packages()''', we have to use '''remove_sys(pkgs)''' to uninstall a package. See [https://rdrr.io/cran/bspm/api/ API].
</li>
</ul>


=== RStudio Package Manager & repository ===
=== Posit Package Manager/RStudio Package Manager ===
* [https://blog.rstudio.com/2020/07/01/announcing-public-package-manager/ Announcing Public Package Manager and v1.1.6]
<ul>
* [http://dirk.eddelbuettel.com/blog/2020/07/03/#028_rocker_rspm Welcome RSPM and test-drive with Bionic and Focal]
<li>[https://packagemanager.rstudio.com/__docs__/admin/getting-started/installation/installing-ppm/ PPM Admin Guide]. It only supports Ubuntu (no Debian), RedHat, OpenSUSE.
* [https://docs.rstudio.com/rspm/admin/serving-binaries/ Serving Package Binaries]. These do not include Debian:( So I cannot test on Docker container.
<li>[https://tshafer.com/blog/2023/07/posit-package-manager-linux Posit Package Manager for Linux R Binaries]. Put the following in the <.Rprofile> file. If inside the RStudio, we need to quit the current R session first.
* Package Manager allows linux users to install '''pre-built binaries''' of the packages which will make install quicker. See an example [https://blog.sellorm.com/2021/04/25/shiny-app-in-docker/?s=09 here].
<pre>
options(HTTPUserAgent = sprintf(
  "R/%s R (%s)",
  getRversion(),
  paste(
    getRversion(),
    R.version["platform"],
    R.version["arch"],
    R.version["os"]
  )
))
 
.ppm <- "https://packagemanager.posit.co/cran/__linux__/jammy/latest"
options(repos = c(CRAN = .ppm))
</pre>
<li>It provides '''pre-compiled binary packages''' for Linux and Windows OS, but not macOS. When enabled, RStudio Package Manager will serve the appropriate CRAN binaries to R users instead of the source packages, saving R users significant installation time.
* <strike>It does not include binary packages from bioconductor</strike>. However, Package Manager allows you to supplement local and git sources with your own precompiled binaries.
* [https://support.posit.co/hc/en-us/articles/360016400354-Using-Bioconductor-with-RStudio-Package-Manager Using Bioconductor with RStudio Package Manager] 7/12/2021
* See [https://docs.posit.co/rspm/admin/serving-binaries/ Serving Package Binaries]
<li>It is similar to MRAN as it allows users to use particular a '''snapshot''' of CRAN as their active repositories within their R session
* [https://community.rstudio.com/t/programmatically-retrieve-frozen-urls-for-cran-from-packagemanager-rstudio-com/119326 programmatically retrieve frozen urls for CRAN from packagemanager.rstudio.com]
* See [https://packagemanager.rstudio.com/client/#/repos/2/overview Setup]. We can change the distribution (Source, CentOS7, Rocky Linux 9, OpenSUSE, Red Hat, SLES, Ubuntu, Windows, macOS) for the Repository URL.  
* See [https://packagemanager.rstudio.com/__docs__/user/ User Guide]
<li> Once configured, users can access RStudio Package Manager through all their familiar tools including base R’s install.packages(), packrat, and devtools.
<li>[https://blog.rstudio.com/2020/07/01/announcing-public-package-manager/ Announcing Public Package Manager and v1.1.6]
<li>Package Manager allows linux users to install '''pre-built binaries''' of the packages which will make install quicker. See an example [https://blog.sellorm.com/2021/04/25/shiny-app-in-docker/?s=09 here].
<pre>
<pre>
# Freeze to Apr 29, 2021 8:00 PM
# Freeze to Apr 29, 2021 8:00 PM
Line 137: Line 190:
https://packagemanager.rstudio.com/cran/__linux__/focal/2021-04-23
https://packagemanager.rstudio.com/cran/__linux__/focal/2021-04-23
</pre>
</pre>
<li>[https://www.rstudio.com/blog/publishing-your-own-binary-packages-with-rspm-2022-07/ Bring Your Own Binary Packages with RSPM]
<li>Docker images
* r-base, rocker/r-base don't use PPM (as of R 4.3.3).
* [https://bioconductor.org/help/docker/ Bioconductor] (source on [https://github.com/Bioconductor/bioconductor_docker github]) does take advantage of PPM. In Bioc 3.17/R 4.3.1, Posit's PPM hosts the binaries. As of Bioc 3.18/R 4.3.3, Bioconductor hosts the binaries.</br>
:RStudio will be available on your web browser at http://localhost:8787. The username is "rstudio".
:<syntaxhighlight lang='r'>docker run \
-e PASSWORD=bioc \
-p 8787:8787 \
bioconductor/bioconductor_docker:devel
</syntaxhighlight>
:R will be available on the terminal
:<syntaxhighlight lang='r'>
docker run -it --rm \
    --user rstudio \
    bioconductor/bioconductor_docker:RELEASE_3_18 R
</syntaxhighlight>
</ul>


=== install a package as it existed on a specific date/snapshot: mran repository ===
=== install a package as it existed on a specific date/snapshot: mran repository ===
[https://www.r-bloggers.com/mran-snapshots-and-you-2/ MRAN snapshots, and you] May 22, 2019.
* [https://blog.revolutionanalytics.com/2023/01/mran-time-machine-retired.html MRAN Time Machine will be retired on July 1 2023]
** [https://www.brodrigues.co/blog/2023-01-12-repro_r/ MRAN is getting shutdown] - what else is there for reproducibility with R, or why reproducibility is on a continuum?
* [https://www.r-bloggers.com/mran-snapshots-and-you-2/ MRAN snapshots, and you] May 22, 2019.
 
Look at https://cran.microsoft.com/snapshot/, it seems the snapshot went back 2014 for R 3.1.1.


For example,
For example,
<pre>
<pre>
install.packages("xgboost", repos="https://mran.microsoft.com/snapshot/2019-09-20/")
options(repos = c(CRAN = "https://mran.microsoft.com/snapshot/2022-10-01"))
 
install.packages("xgboost", repos="https://mran.microsoft.com/snapshot/2022-10-01/")
</pre>
</pre>


Line 169: Line 245:
</pre>
</pre>


== Check installed Bioconductor version ==
=== install a package on an old version of R ===
Following [https://www.biostars.org/p/150920/ this post], use '''tools:::.BioC_version_associated_with_R_version()'''.
<ul>
<li>Currently R is 4.2.2
<li>I have build R 4.1.3 and R 4.0.5 from source on Ubuntu 22.04. P.S. We need to include/enable "Source code" URIs  [https://askubuntu.com/a/857433 Error :: You must put some 'source' URIs in your sources.list].
<pre>
sudo apt update
sudo apt-get build-dep r-base


''Mind the '.' in front of the 'BioC'. It may be possible for some installed packages to have been sourced from a different BioC version.''  
tar -xzvf R-4.1.3.tar.gz
cd R-4.1.3
./configure
time make -j2
bin/R
</pre>
<li>I'm trying to install DESEq2 on R 4.1.3 and R 4.0.5
<li>On R 4.1.3, the installation is successful.
<pre>
install.packages("BiocManager")
BiocManager::install("DESeq2") 
packageVersion("DESeq2")  # 1.34.0
</pre>
<li>On R 4.0.5, the installation of "locfit" (current version 1.5-9.7) requires R >= 4.1.0. Check on CRAN, we see the locfit version is 1.5-6 for R 4.0.5 (2021-3-31). So I try this version first.
<pre>
install.packages("BiocManager")
install.packages("remotes")
remotes::install_version("locfit", "1.5-6")
# Error: a 'NAMESPACE' file is required


<syntaxhighlight lang='rsplus'>
# Try the next version 1.5-7
tools:::.BioC_version_associated_with_R_version() # `3.6'
remotes::install_version("locfit", "1.5-7") # Works
tools:::.BioC_version_associated_with_R_version() == '3.6'  # TRUE
BiocManager::install("DESeq2")  # Works too
</syntaxhighlight>
packageVersion("DESeq2") # 1.30.1
</pre>
<li>Now it is a good opportunity to try '''renv'''. So I create a new folder 'testProject' and create a new file 'test.R'. This R file contains only one line: library(DESeq2).
<pre>
> setwd("testProj")
> install.packages("renv")
> renv::init()
* Initializing project ...
* Discovering package dependencies ... Done!
* Copying packages into the cache ... [79/79] Done!
One or more Bioconductor packages are used in your project,
but the BiocManager package is not available.


== CRAN Package Depends on Bioconductor Package ==
Consider installing BiocManager before snapshot.
For example, if I run ''install.packages("NanoStringNorm")'' to install the [https://cran.r-project.org/web/packages/NanoStringNorm/index.html package] from CRAN, I may get
 
The following package(s) will be updated in the lockfile:
...
The version of R recorded in the lockfile will be updated:
- R                      [*] -> [4.0.5]
 
* Lockfile written to '~/Downloads/testProject/renv.lock'.
* Project '~/Downloads/testProject' loaded. [renv 0.16.0]
* renv activated -- please restart the R session.
> renv::install("BiocManager") # NOT SURE IF THIS IS NECESSARY
Retrieving 'https://cloud.r-project.org/src/contrib/BiocManager_1.30.19.tar.gz' ...
OK [file is up to date]
Installing BiocManager [1.30.19] ...
OK [built from source]
Moving BiocManager [1.30.19] into the cache ...
OK [moved to cache in 20 milliseconds]
> q()
</pre>
Now DESeq2 is in "renv.lock" file!
 
Note if we accidentally start a different version of R on an renv-ed project based on an older version of R, we shall see some ''special'' message informing us '''The project library is out of sync with the lockfile'''. At this time, the "renv.lock" is not touched yet.
<pre>
<pre>
ERROR: dependency ‘vsn’ is not available for package ‘NanoStringNorm’
# Bootstrapping renv 0.16.0 --------------------------------------------------
* Downloading renv 0.16.0 ... OK (downloaded source)
* Installing renv 0.16.0 ... Done!
* Successfully installed and loaded renv 0.16.0.
! Using R 4.2.2 (lockfile was generated with R 4.0.5)
* Project '~/Downloads/testProject' loaded. [renv 0.16.0]
* The project library is out of sync with the lockfile.
* Use `renv::restore()` to install packages recorded in the lockfile.
</pre>
</pre>
This is because the NanoStringNorm package depends on the vsn package which is on Bioconductor.
<li>What happened if we start with the same version of R but have a different version of renv in the global environment (vs renv.lock)?
<pre>
install.packages("remotes")
remotes::install_version("renv", "0.15.5")
renv::init()
q()


Another instance is CRAN's 'biospear' (actually 'plsRcox') depends on Bioc's 'survcomp' & 'mixOmics'.
# Start the same R from another directory and update renv to the latest version 0.16.0
# Now go back to the project directory and start R to see what will happen?
# Nothing....  This is because the same project has the old & same version of "renv" library
# in the "$PROJECT/renv" directory
</pre>
If we delete the "renv" subdirectory, ".Rprofile" file but keep "renv.lock" file, and start R (remember the 'renv' package version in the global R is different from the version in "renv.lock".
<pre>
> renv::restore()
This project has not yet been activated.
Activating this project will ensure the project library is used during restore.
Please see `?renv::activate` for more details.


One solution is to run a line '''setRepositories(ind=1:2)'''. See [http://stackoverflow.com/questions/14343817/cran-package-depends-on-bioconductor-package-installing-error this post] or [https://stackoverflow.com/questions/34617306/r-package-with-cran-and-bioconductor-dependencies this one]. Note that the default repository list can be found at (Ubuntu) '''/usr/lib/R/etc/repositories''' file.
Would you like to activate this project before restore? [Y/n]: y
{{Pre}}
Retrieving 'https://cloud.r-project.org/src/contrib/BiocManager_1.30.19.tar.gz' ...
options("repos") # display the available repositories (only CRAN)
OK [file is up to date]
setRepositories(ind=1:2)
Installing BiocManager [1.30.19] ...
options("repos") # CRAN and bioc are included
OK [built from source]
#                                        CRAN
Moving BiocManager [1.30.19] into the cache ...
#                "https://cloud.r-project.org"
OK [moved to cache in 27 milliseconds]
# "https://bioconductor.org/packages/3.6/bioc"
* Project '~/Downloads/testProject' loaded. [renv 0.16.0]
* The project may be out of sync -- use `renv::status()` for more details.
The following package(s) will be updated:


install.packages("biospear") # it will prompt to select CRAN
# Bioconductor =======================
- AnnotationDbi          [* -> 1.56.2]
...
# CRAN ===============================
- renv                  [0.16.0 -> 0.15.5]
...
Do you want to proceed? [y/N]: y
Retrieving 'https://cloud.r-project.org/src/contrib/Archive/renv/renv_0.15.5.tar.gz' ...
OK [downloaded 961.3 Kb in 0.2 secs]
Installing renv [0.15.5] ...
OK [built from source]
Moving renv [0.15.5] into the cache ...
OK [moved to cache in 6.8 milliseconds]
...
The following package(s) have been updated:


install.packages("biospear", repos = "http://cran.rstudio.com") # NOT work since bioc repos is erased
renv [installed version 0.15.5 != loaded version 0.16.0]
</pre>


This will also install the '''BiocInstaller''' package if it has not been installed before. See also [https://www.bioconductor.org/install/ Install Bioconductor Packages].
Consider restarting the R session and loading the newly-installed packages.
</pre>
All the R packages are restored in the $PROJECT/renv directory.
</li>
</ul>


=== Bioconductor packages depend on CRAN ===
== Check installed Bioconductor version ==
For example [https://cran.r-project.org/web/packages/cowplot/index.html cowplot] shows ''breakpointR'' from Bioconductor depends on it.
Following [https://www.biostars.org/p/150920/ this post], use '''tools:::.BioC_version_associated_with_R_version()'''.


== update.packages() ==
''Mind the '.' in front of the 'BioC'. It may be possible for some installed packages to have been sourced from a different BioC version.''
update.packages(ask="graphics") can open a graphical window to select packages.


== Binary packages only for two versions of R ==
[https://www.r-bloggers.com/r-tip-check-what-repos-you-are-using/ Check What Repos You are Using].
'''CRAN only provides binaries for one version of R prior to the current one. So when CRAN moves to post-3.6.* R most non version-stuck mirrors will not have 3.5.* binary versions of packages.'''
== Install a tar.gz (e.g. an archived package) from a local directory ==
<syntaxhighlight lang='bash'>
R CMD INSTALL <package-name>.tar.gz
</syntaxhighlight>
Or in R:
<syntaxhighlight lang='rsplus'>
<syntaxhighlight lang='rsplus'>
# Method 1: cannot install dependencies
tools:::.BioC_version_associated_with_R_version() # `3.6'
install.packages(<pathtopackage>, repos = NULL)
tools:::.BioC_version_associated_with_R_version() == '3.6' # TRUE
# These paths can be source directories or archives or binary package
# archive files  (as created by ‘R CMD build --binary’).
# (‘http://’ and ‘file://’ URLs are also accepted and the files
# will be downloaded and installed from local copies.)
 
# Method 2: take care of dependencies from CRAN
devtools::install(<directory to package>, dependencies = TRUE)
                        # this will use 'R CMD INSTALL' to install the package.
                        # It will try to install dependencies of the package from CRAN,
                        # if they're not already installed.
</syntaxhighlight>
</syntaxhighlight>


The installation process can be nasty due to the dependency issue. Consider the 'biospear' package
== CRAN Package Depends on Bioconductor Package ==
For example, if I run ''install.packages("NanoStringNorm")'' to install the [https://cran.r-project.org/web/packages/NanoStringNorm/index.html package] from CRAN, I may get
<pre>
<pre>
biospear - plsRcox (archived) - plsRglm (archived) - bipartite
ERROR: dependency ‘vsn’ is not available for package ‘NanoStringNorm’
                              - lars
                              - pls
                              - kernlab
                              - mixOmics (CRAN->Bioconductor)
                              - risksetROC
                              - survcomp (Bioconductor)
                              - rms
</pre>
</pre>
So in order to install the 'plsRcox' package, we need to do the following steps. Note: plsRcox package is back on 6/2/2018.
This is because the NanoStringNorm package depends on the vsn package which is on Bioconductor.
{{Pre}}
 
# For curl
Another example is CRAN's ''tidyHeatmap'' that imports ''ComplexHeatmap'' in Bioconductor.
system("apt update")
system("apt install curl libcurl4-openssl-dev libssl-dev")


# For X11
Another instance is CRAN's ''biospear'' (actually [https://cran.r-project.org/web/packages/plsRcox/index.html plsRcox]) that depends on Bioc's 'survcomp' & 'mixOmics'.
system("apt install libcgal-dev libglu1-mesa-dev libglu1-mesa-dev")
system("apt install libfreetype6-dev") # https://stackoverflow.com/questions/31820865/error-in-installing-rgl-package
</pre>


One solution is to run a line '''setRepositories(ind=1:2)'''. See [http://stackoverflow.com/questions/14343817/cran-package-depends-on-bioconductor-package-installing-error this post] or [https://stackoverflow.com/questions/34617306/r-package-with-cran-and-bioconductor-dependencies this one]. Note that the default repository list can be found at (Ubuntu) '''/usr/lib/R/etc/repositories''' file.
{{Pre}}
{{Pre}}
source("https://bioconductor.org/biocLite.R")
options("repos") # display the available repositories (only CRAN)
biocLite("survcomp") # this has to be run before the next command of installing a bunch of packages from CRAN
setRepositories(ind=1:2)
options("repos") # CRAN and bioc are included
#                                        CRAN
#                "https://cloud.r-project.org"
# "https://bioconductor.org/packages/3.6/bioc"
 
install.packages("biospear") # it will prompt to select CRAN


install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz",
install.packages("biospear", repos = "http://cran.rstudio.com") # NOT work since bioc repos is erased
                repos = NULL, type="source")
</pre>
# ERROR: dependencies ‘pkgconfig’, ‘cobs’, ‘corpcor’, ‘devtools’, ‘glmnet’, ‘grplasso’, ‘mboost’, ‘plsRcox’,
 
# ‘pROC’, ‘PRROC’, ‘RCurl’, ‘survAUC’ are not available for package ‘biospear’
This will also install the '''BiocInstaller''' package if it has not been installed before. See also [https://www.bioconductor.org/install/ Install Bioconductor Packages].
install.packages(c("pkgconfig", "cobs", "corpcor", "devtools", "glmnet", "grplasso", "mboost",
 
                  "plsRcox", "pROC", "PRROC", "RCurl", "survAUC"))
=== Bioconductor packages depend on CRAN ===
# optional: install.packages(c("doRNG", "mvnfast"))
For example [https://cran.r-project.org/web/packages/cowplot/index.html cowplot] shows ''breakpointR'' from Bioconductor depends on it.
install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz",
 
                repos = NULL, type="source")
== update.packages() ==
# OR
update.packages(ask="graphics") can open a graphical window to select packages.
# devtools::install_github("cran/biospear")
 
library(biospear) # verify
== only upgrade to binary package versions ==
</pre>
Try setting options(pkgType = "binary") before running update.packages(). To make this always be the default behavior, you can add that line to your .Rprofile file.


To install the (deprecated, bioc) packages 'inSilicoMerging',
How about options(install.packages.check.source = "no"), options(install.packages.compile.from.source = "never")?
{{Pre}}
biocLite(c('rjson', 'Biobase', 'RCurl'))


# destination directory is required
== Binary packages only for two versions of R ==
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz",
[https://www.r-bloggers.com/r-tip-check-what-repos-you-are-using/ Check What Repos You are Using].  
#              "~/Downloads/inSilicoDb_2.7.0.tar.gz")
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz",
#              "~/Downloads/inSilicoMerging_1.15.0.tar.gz")
# ~/Downloads or $HOME/Downloads won't work in untar()
# untar("~/Downloads/inSilicoDb_2.7.0.tar.gz", exdir="/home/brb/Downloads")
# untar("~/Downloads/inSilicoMerging_1.15.0.tar.gz", exdir="/home/brb/Downloads")
# install.packages("~/Downloads/inSilicoDb", repos = NULL)
# install.packages("~/Downloads/inSilicoMerging", repos = NULL)
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz",
                repos = NULL, type = "source")
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz",
                repos = NULL, type = "source")
</pre>


=== R CMD INSTALL -l LIB/--library=LIB option ===
'''CRAN only provides binaries for one version of R prior to the current one. So when CRAN moves to post-3.6.* R most non version-stuck mirrors will not have 3.5.* binary versions of packages.'''
Install a package to a custom location
<pre>
$ R CMD INSTALL -l /usr/me/localR/library myRPackage.tar.gz
</pre>


Use a package installed in a custom location
== Install a tar.gz (e.g. an archived package) from a local directory ==
<pre>
<syntaxhighlight lang='bash'>
R> library("myRPackage", lib.loc="/usr/me/local/R/library")
R CMD INSTALL <package-name>.tar.gz
</syntaxhighlight>
Or in R:
<syntaxhighlight lang='rsplus'>
# Method 1: cannot install dependencies
install.packages(<pathtopackage>, repos = NULL)
# These paths can be source directories or archives or binary package
# archive files  (as created by ‘R CMD build --binary’).
# (‘http://’ and ‘file://’ URLs are also accepted and the files
# will be downloaded and installed from local copies.)


# OR include below in .bashrc file
# Method 2: take care of dependencies from CRAN
$ export R_LIBS=/usr/me/local/R/library
devtools::install(<directory to package>, dependencies = TRUE)
R> .libPaths() # check
                        # this will use 'R CMD INSTALL' to install the package.
R> library("myRPackage")
                        # It will try to install dependencies of the package from CRAN,
                        # if they're not already installed.
</syntaxhighlight>
 
The installation process can be nasty due to the dependency issue. Consider the 'biospear' package
<pre>
biospear - plsRcox (archived) - plsRglm (archived) - bipartite
                              - lars
                              - pls
                              - kernlab
                              - mixOmics (CRAN->Bioconductor)
                              - risksetROC
                              - survcomp (Bioconductor)
                              - rms
</pre>
</pre>
So in order to install the 'plsRcox' package, we need to do the following steps. Note: plsRcox package is back on 6/2/2018.
{{Pre}}
# For curl
system("apt update")
system("apt install curl libcurl4-openssl-dev libssl-dev")


== Install a specific version of R/Bioconductor package ==
# For X11
For packages from CRAN, use something like '''remotes::install_version'''("dplyr", "1.0.2")
system("apt install libcgal-dev libglu1-mesa-dev libglu1-mesa-dev")
system("apt install libfreetype6-dev") # https://stackoverflow.com/questions/31820865/error-in-installing-rgl-package
</pre>


For packages from Bioconductor, see the two solutions [https://stackoverflow.com/questions/49487171/r-how-to-install-a-specified-version-of-a-bioconductor-package R how to install a specified version of a bioconductor package?]
{{Pre}}
source("https://bioconductor.org/biocLite.R")
biocLite("survcomp") # this has to be run before the next command of installing a bunch of packages from CRAN


== Install multiple/different versions of the same R package ==
install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz",  
https://stackoverflow.com/a/2989369
                repos = NULL, type="source")
<pre>
# ERROR: dependencies ‘pkgconfig’, ‘cobs’, ‘corpcor’, ‘devtools’, ‘glmnet’, ‘grplasso’, ‘mboost’, ‘plsRcox’,
install.packages("~/Downloads/foo_0.1.1.tar.gz", lib = "/tmp", repos = NULL)
# ‘pROC’, ‘PRROC’, ‘RCurl’, ‘survAUC’ are not available for package ‘biospear’
# a new folder "/tmp/foo" will be created
install.packages(c("pkgconfig", "cobs", "corpcor", "devtools", "glmnet", "grplasso", "mboost",
library(foo, lib.loc="/tmp") # Or use 'lib' to be consistent with install.packages()     
                  "plsRcox", "pROC", "PRROC", "RCurl", "survAUC"))
 
# optional: install.packages(c("doRNG", "mvnfast"))
library(foo, lib.loc="~/dev/foo/v1")    ## loads v1
install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz",  
# and
                repos = NULL, type="source")
library(foo, lib.loc="~/dev/foo/v2")    ## loads v2
# OR
 
# devtools::install_github("cran/biospear")
packageVersion("foo", lib.loc = "/tmp")
library(biospear) # verify
sessionInfo()
help(package = "foo", lib.loc = "/tmp")
 
remove.packages("foo", lib = "/tmp")
</pre>
</pre>


The same works for install.packages(). help(install.packages)
To install the (deprecated, bioc) packages 'inSilicoMerging',
{{Pre}}
biocLite(c('rjson', 'Biobase', 'RCurl'))


The install_version() from devtools and remotes will overwrite the existing installation.
# destination directory is required
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz",
#              "~/Downloads/inSilicoDb_2.7.0.tar.gz")
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz",
#              "~/Downloads/inSilicoMerging_1.15.0.tar.gz")
# ~/Downloads or $HOME/Downloads won't work in untar()
# untar("~/Downloads/inSilicoDb_2.7.0.tar.gz", exdir="/home/brb/Downloads")
# untar("~/Downloads/inSilicoMerging_1.15.0.tar.gz", exdir="/home/brb/Downloads")
# install.packages("~/Downloads/inSilicoDb", repos = NULL)
# install.packages("~/Downloads/inSilicoMerging", repos = NULL)
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz",
                repos = NULL, type = "source")
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz",
                repos = NULL, type = "source")
</pre>


== Query an R package installed locally ==
=== R CMD INSTALL -l LIB/--library=LIB option ===
Install a package to a custom location
<pre>
<pre>
packageDescription("MASS")
$ R CMD INSTALL -l /usr/me/localR/library myRPackage.tar.gz
packageVersion("MASS")
</pre>
</pre>


== Query an R package (from CRAN) basic information: available.packages() ==
Use a package installed in a custom location
<syntaxhighlight lang='rsplus'>
<pre>
packageStatus() # Summarize information about installed packages
R> library("myRPackage", lib.loc="/usr/me/local/R/library")


available.packages() # List Available Packages at CRAN-like Repositories
# OR include below in .bashrc file
                    # Even I use an old version of R, it still return the latest version of the packages
$ export R_LIBS=/usr/me/local/R/library
                    # The 'problem' happens on install.packages() too.
R> .libPaths() # check
</syntaxhighlight>
R> library("myRPackage")
The '''available.packages()''' command is useful for understanding package dependency. Use '''setRepositories()''' or 'RGUI -> Packages -> select repositories' to select repositories and '''options()$repos''' to check or change the repositories.
</pre>
 
== Install a specific version of R/Bioconductor package ==
For packages from CRAN, use something like '''remotes::install_version'''("dplyr", "1.0.2")


<span style="color: red">The return result of available.packages() depends on the R version. </span>
For packages from Bioconductor, see the two solutions [https://stackoverflow.com/questions/49487171/r-how-to-install-a-specified-version-of-a-bioconductor-package R how to install a specified version of a bioconductor package?]


Also the '''packageStatus()''' is another useful function for query how many packages are in the repositories, how many have been installed, and individual package status (installed or not, needs to be upgraded or not).
== Install multiple/different versions of the same R package ==
{{Pre}}
https://stackoverflow.com/a/2989369
> options()$repos
<pre>
                      CRAN
install.packages("~/Downloads/foo_0.1.1.tar.gz", lib = "/tmp", repos = NULL)
"https://cran.rstudio.com/"  
# a new folder "/tmp/foo" will be created
library(foo, lib.loc="/tmp") # Or use 'lib' to be consistent with install.packages()     


> packageStatus()  
library(foo, lib.loc="~/dev/foo/v1")   ## loads v1
Number of installed packages:
# and
                                   
library(foo, lib.loc="~/dev/foo/v2")    ## loads v2
                                      ok upgrade unavailable
  C:/Program Files/R/R-3.0.1/library 110      0          1


Number of available packages (each package counted only once):
packageVersion("foo", lib.loc = "/tmp")
                                                                                 
sessionInfo()
                                                                                    installed not installed
help(package = "foo", lib.loc = "/tmp")
  http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0                            76          4563
 
  http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.0                                0            5
remove.packages("foo", lib = "/tmp")
  http://www.bioconductor.org/packages/2.12/bioc/bin/windows/contrib/3.0                  16          625
  http://www.bioconductor.org/packages/2.12/data/annotation/bin/windows/contrib/3.0        4          686
> tmp <- available.packages()
> str(tmp)
chr [1:5975, 1:17] "A3" "ABCExtremes" "ABCp2" "ACCLMA" "ACD" "ACNE" "ADGofTest" "ADM3" "AER" ...
- attr(*, "dimnames")=List of 2
  ..$ : chr [1:5975] "A3" "ABCExtremes" "ABCp2" "ACCLMA" ...
  ..$ : chr [1:17] "Package" "Version" "Priority" "Depends" ...
> tmp[1:3,]
            Package      Version Priority Depends                    Imports LinkingTo Suggests           
A3          "A3"          "0.9.2" NA      "xtable, pbapply"          NA      NA        "randomForest, e1071"
ABCExtremes "ABCExtremes" "1.0"   NA      "SpatialExtremes, combinat" NA      NA        NA                 
ABCp2      "ABCp2"      "1.1"  NA      "MASS"                      NA      NA        NA                 
            Enhances License      License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation File
A3          NA      "GPL (>= 2)" NA              NA                    NA      NA    NA    NA              NA 
ABCExtremes NA      "GPL-2"      NA              NA                    NA      NA    NA    NA              NA 
ABCp2      NA      "GPL-2"      NA              NA                    NA      NA    NA    NA              NA 
            Repository                                                   
A3          "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCExtremes "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCp2      "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
</pre>
</pre>
And the following commands find which package depends on Rcpp and also which are from bioconductor repository.
<syntaxhighlight lang='rsplus'>
> pkgName <- "Rcpp"
> rownames(tmp)[grep(pkgName, tmp[,"Depends"])]
> tmp[grep("Rcpp", tmp[,"Depends"]), "Depends"]


> ind <- intersect(grep(pkgName, tmp[,"Depends"]), grep("bioconductor", tmp[, "Repository"]))
The same works for install.packages(). help(install.packages)
> rownames(grep)[ind]
 
NULL
The install_version() from devtools and remotes will overwrite the existing installation.
> rownames(tmp)[ind]
 
[1] "ddgraph"           "DESeq2"            "GeneNetworkBuilder" "GOSemSim"          "GRENITS"         
== Query an R package installed locally ==
[6] "mosaics"           "mzR"                "pcaMethods"        "Rdisop"            "Risa"             
<pre>
[11] "rTANDEM"   
packageDescription("MASS")
</syntaxhighlight>
packageVersion("MASS")
</pre>


== CRAN vs Bioconductor packages ==
== Query an R package (from CRAN) basic information: available.packages() ==
<syntaxhighlight lang='rsplus'>
<syntaxhighlight lang='rsplus'>
> R.version # 3.4.3
packageStatus() # Summarize information about installed packages
# CRAN
> x <- available.packages()
> dim(x)
[1] 12581    17


# Bioconductor Soft
available.packages() # List Available Packages at CRAN-like Repositories
> biocinstallRepos()
                    # Even I use an old version of R, it still return the latest version of the packages
                                              BioCsoft
                    # The 'problem' happens on install.packages() too.
          "https://bioconductor.org/packages/3.6/bioc"
</syntaxhighlight>
                                                BioCann
The '''available.packages()''' command is useful for understanding package dependency. Use '''setRepositories()''' or 'RGUI -> Packages -> select repositories' to select repositories and '''options()$repos''' to check or change the repositories.
"https://bioconductor.org/packages/3.6/data/annotation"
                                                BioCexp
"https://bioconductor.org/packages/3.6/data/experiment"
                                                  CRAN
                            "https://cran.rstudio.com/"
> y <- available.packages(repos = biocinstallRepos()[1])
> dim(y)
[1] 1477  17
> intersect(x[, "Package"], y[, "Package"])
character(0)
# Bioconductor Annotation
> dim(available.packages(repos = biocinstallRepos()[2]))
[1] 909  17
# Bioconductor Experiment
> dim(available.packages(repos = biocinstallRepos()[3]))
[1] 326  17


# CRAN + All Bioconductor
<span style="color: red">The return result of available.packages() depends on the R version. </span>
> z <- available.packages(repos = biocinstallRepos())
> dim(z)
[1] 15292    17
</syntaxhighlight>


== Downloading Bioconductor package with an old R ==
The number of packages returned from available.packages() is smaller than the number of packages obtained from '''rsync''' [https://cran.r-project.org/mirror-howto.html CRAN Mirror HOWTO/FAQ]. For example, 19473 < 25000. Some archived packages are still available via rsync.
When I try to download the [https://bioconductor.org/packages/release/bioc/html/GenomicDataCommons.html GenomicDataCommons] package using R 3.4.4 with Bioc 3.6 (the current R version is 3.5.0), it was found it can only install version 1.2.0 instead the latest version 1.4.1.


It does not work by running biocLite("BiocUpgrade") to upgrade Bioc from 3.6 to 3.7.
Also the '''packageStatus()''' is another useful function for query how many packages are in the repositories, how many have been installed, and individual package status (installed or not, needs to be upgraded or not).
<syntaxhighlight lang='rsplus'>
{{Pre}}
source("https://bioconductor.org/biocLite.R")
> options()$repos
biocLite("BiocUpgrade")
                      CRAN
# Error: Bioconductor version 3.6 cannot be upgraded with R version 3.4.4
"https://cran.rstudio.com/"  
</syntaxhighlight>


See some instruction on [https://packagemanager.rstudio.com/client/#/repos/3/overview RStudio package manager] website.
> packageStatus()
Number of installed packages:
                                   
                                      ok upgrade unavailable
  C:/Program Files/R/R-3.0.1/library 110      0          1


== Analyzing data on CRAN packages ==
Number of available packages (each package counted only once):
New undocumented function in R 3.4.0: '''tools::CRAN_package_db()'''
                                                                                 
 
                                                                                    installed not installed
http://blog.revolutionanalytics.com/2017/05/analyzing-data-on-cran-packages.html
  http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0                            76          4563
 
  http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.0                               0            5
== R package location when they are installed by root ==
  http://www.bioconductor.org/packages/2.12/bioc/bin/windows/contrib/3.0                  16          625
''/usr/local/lib/R/site-library''
  http://www.bioconductor.org/packages/2.12/data/annotation/bin/windows/contrib/3.0        4          686
 
> tmp <- available.packages()
== Customizing your package/library location ==
> str(tmp)
 
chr [1:5975, 1:17] "A3" "ABCExtremes" "ABCp2" "ACCLMA" "ACD" "ACNE" "ADGofTest" "ADM3" "AER" ...
=== Add a personal directory to .libPaths() ===
- attr(*, "dimnames")=List of 2
<syntaxhighlight lang='rsplus'>
  ..$ : chr [1:5975] "A3" "ABCExtremes" "ABCp2" "ACCLMA" ...
.libPaths( c( .libPaths(), "~/userLibrary") )
  ..$ : chr [1:17] "Package" "Version" "Priority" "Depends" ...
</syntaxhighlight>
> tmp[1:3,]
No need to use the assignment operator.
            Package      Version Priority Depends                    Imports LinkingTo Suggests           
A3          "A3"          "0.9.2" NA      "xtable, pbapply"          NA      NA        "randomForest, e1071"
ABCExtremes "ABCExtremes" "1.0"  NA      "SpatialExtremes, combinat" NA      NA        NA                 
ABCp2      "ABCp2"      "1.1"  NA      "MASS"                      NA      NA        NA                 
            Enhances License      License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation File
A3          NA      "GPL (>= 2)" NA              NA                    NA      NA    NA    NA              NA 
ABCExtremes NA      "GPL-2"      NA              NA                    NA      NA    NA    NA              NA 
ABCp2      NA      "GPL-2"      NA              NA                    NA      NA    NA    NA              NA 
            Repository                                                   
A3          "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCExtremes "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCp2      "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
</pre>
And the following commands find which package depends on Rcpp and also which are from bioconductor repository.
<syntaxhighlight lang='rsplus'>
> pkgName <- "Rcpp"
> rownames(tmp)[grep(pkgName, tmp[,"Depends"])]
> tmp[grep("Rcpp", tmp[,"Depends"]), "Depends"]


=== Install personal R packages after upgrade R, .libPaths(), Rprofile.site, R_LIBS_USER ===
> ind <- intersect(grep(pkgName, tmp[,"Depends"]), grep("bioconductor", tmp[, "Repository"]))
{| class="wikitable"  
> rownames(grep)[ind]
|-
NULL
! File
> rownames(tmp)[ind]
! Example
[1] "ddgraph"            "DESeq2"            "GeneNetworkBuilder" "GOSemSim"          "GRENITS"         
|-
[6] "mosaics"            "mzR"                "pcaMethods"        "Rdisop"             "Risa"            
| Rprofile.site/.Rprofile
[11] "rTANDEM"  
| .libPaths(c("/usr/lib/R/site-library",<br /> &emsp;&emsp;&emsp;&emsp;&emsp; "/usr/lib/R/library"))
</syntaxhighlight>
|-
| Renviron.site/.Renviron
| R_LIB_SITE="/usr/lib/R/site-library:/usr/lib/R/library"
|}


Scenario: We already have installed many R packages under R 3.1.X in the user's directory. Now we upgrade R to a new version (3.2.X). We like to have these packages available in R 3.2.X.
== CRAN vs Bioconductor packages ==
<syntaxhighlight lang='rsplus'>
> R.version # 3.4.3
# CRAN
> x <- available.packages()
> dim(x)
[1] 12581    17


<span style="color:#0000FF">For Windows OS, refer to [http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f R for Windows FAQ]</span>
# Bioconductor Soft
 
> biocinstallRepos()
The follow method works on Linux and Windows.
                                              BioCsoft
 
          "https://bioconductor.org/packages/3.6/bioc"
<span style="color:#FF0000">Make sure only one instance of R is running</span>
                                                BioCann
<pre>
"https://bioconductor.org/packages/3.6/data/annotation"
# Step 1. update R's built-in packages and install them on my personal directory
                                                BioCexp
update.packages(ask=FALSE, checkBuilt = TRUE, repos="http://cran.rstudio.com")
"https://bioconductor.org/packages/3.6/data/experiment"
                                                  CRAN
                            "https://cran.rstudio.com/"  
> y <- available.packages(repos = biocinstallRepos()[1])
> dim(y)
[1] 1477  17
> intersect(x[, "Package"], y[, "Package"])
character(0)
# Bioconductor Annotation
> dim(available.packages(repos = biocinstallRepos()[2]))
[1] 909  17
# Bioconductor Experiment
> dim(available.packages(repos = biocinstallRepos()[3]))
[1] 326  17


# Step 2. update Bioconductor packages
# CRAN + All Bioconductor
.libPaths() # The first one is my personal directory
> z <- available.packages(repos = biocinstallRepos())
# [1] "/home/brb/R/x86_64-pc-linux-gnu-library/3.2"
> dim(z)
# [2] "/usr/local/lib/R/site-library"
[1] 15292    17
# [3] "/usr/lib/R/site-library"
</syntaxhighlight>
# [4] "/usr/lib/R/library"
 
== Downloading Bioconductor package with an old R ==
When I try to download the [https://bioconductor.org/packages/release/bioc/html/GenomicDataCommons.html GenomicDataCommons] package using R 3.4.4 with Bioc 3.6 (the current R version is 3.5.0), it was found it can only install version 1.2.0 instead the latest version 1.4.1.


Sys.getenv("R_LIBS_USER") # may or may not equivalent to .libPaths()[1]
It does not work by running biocLite("BiocUpgrade") to upgrade Bioc from 3.6 to 3.7.
ul <- unlist(strsplit(Sys.getenv("R_LIBS_USER"), "/"))
<syntaxhighlight lang='rsplus'>
src <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.1")
source("https://bioconductor.org/biocLite.R")
des <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.2")
biocLite("BiocUpgrade")  
pkg <- dir(src, full.names = TRUE)
# Error: Bioconductor version 3.6 cannot be upgraded with R version 3.4.4
if (!file.exists(des)) dir.create(des)  # If 3.2 subdirectory does not exist yet!
</syntaxhighlight>
file.copy(pkg, des, overwrite=FALSE, recursive = TRUE)
 
source("http://www.bioconductor.org/biocLite.R")
See some instruction on [https://packagemanager.rstudio.com/client/#/repos/3/overview RStudio package manager] website.
biocLite(ask = FALSE)
</pre>


<span style="color:#0000FF">From Robert Kabacoff ([https://www.statmethods.net/interface/customizing.html R in Action])</span>
== Analyzing data on CRAN packages ==
* If you have a customized '''Rprofile.site file''' (see appendix B), save a copy outside of R.
* New undocumented function in R 3.4.0: '''tools::CRAN_package_db()'''
* Launch your current version of R and issue the following statements
* http://blog.revolutionanalytics.com/2017/05/analyzing-data-on-cran-packages.html
<pre>
* [http://blog.schochastics.net/posts/2024-01-17_six-degrees-of-hadley-wickham/ Six degrees of Hadley Wickham: The CRAN co-authorship network]
oldip <- installed.packages()[,1]
save(oldip, file="path/installedPackages.Rdata")
</pre>
where ''path'' is a directory outside of R.
* Download and install the newer version of R.
* If you saved a customized version of the Rprofile.site file in step 1, copy it into the new installation.
* Launch the new version of R, and issue the following statements
<pre>
load("path/installedPackages.Rdata")
newip <- installed.packages()[,1]
for(i in setdiff(oldip, newip))
  install.packages(i)
</pre>
where path is the location specified in step 2.
*  Delete the old installation (optional).


This approach will install only packages that are available from the CRAN. It won’t find packages obtained from other locations. In fact, the process will display a list of packages that can’t be installed For example for packages obtained from Bioconductor, use the following method to update packages
== R package location when they are installed by root ==
<pre>
''/usr/local/lib/R/site-library''
source(http://bioconductor.org/biocLite.R)
biocLite(PKGNAME)
</pre>


=== Persistent config and data for R packages with .Rprofile and .Renviron ===
== Customizing your package/library location ==
[https://blog.r-hub.io/2020/03/12/user-preferences/ Persistent config and data for R packages]. startup, rappdirs, hoardr, keyring.


=== Would you like to use a personal library instead? ===
=== Add a personal directory to .libPaths() ===
Some posts from internet
<syntaxhighlight lang='rsplus'>
* [https://stackoverflow.com/questions/29969838/setting-r-libs-avoiding-would-you-like-to-use-a-personal-library-instead Setting R_LIBS & avoiding “Would you like to use a personal library instead?”]. Note: I try to create ~/.Renviron to add my personal folder in it. But update.packages() still asks me if I like to use a personal library instead (tested on Ubuntu + R 3.4).
.libPaths( c( .libPaths(), "~/userLibrary") )
* [https://stackoverflow.com/questions/39879424/automatically-create-personal-library-in-r automatically create personal library in R]. Using suppressUpdates + specify '''lib''' in biocLite() or update.packages(Sys.getenv("R_LIBS_USER"), ask = F) <syntaxhighlight lang='rsplus'>
# create local user library path (not present by default)
dir.create(path = Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)
# install to local user library path
install.packages(p, lib = Sys.getenv("R_LIBS_USER"), repos = "https://cran.rstudio.com/")
# Bioconductor version
biocLite(p, suppressUpdates = TRUE, lib = Sys.getenv("R_LIBS_USER"))
</syntaxhighlight>
</syntaxhighlight>
No need to use the assignment operator.


The problem can happen if the R was installed to the C:\Program Files\R folder by ''users'' but then some main packages want to be upgraded. R will always pops a message 'Would you like to use a personal library instead?'.  
=== Install personal R packages after upgrade R, .libPaths(), Rprofile.site, R_LIBS_USER ===
 
{| class="wikitable"
To suppress the message and use the personal library always,
|-
* Run R as administrator. If you do that, main packages can be upgraded from C:\Program Files\R\R-X.Y.Z\library folder.
! File
* [[Arraytools#Writable_R_package_directory_cannot_be_found|Writable R package directory cannot be found]] and a [[Arraytools#Download_required_R.2FBioconductor_.28software.29_packages|this]]. A solution here is to change the security of the R library folder so the user has a full control on the folder.
! Example
* [https://cran.r-project.org/bin/windows/base/rw-FAQ.html#Does-R-run-under-Windows-Vista_003f Does R run under Windows Vista/7/8/Server 2008?] There are 3 ways to get around the issue.
|-
* [https://cran.r-project.org/bin/windows/base/rw-FAQ.html#I-don_0027t-have-permission-to-write-to-the-R_002d3_002e3_002e2_005clibrary-directory I don’t have permission to write to the R-3.3.2\library directory]
| Rprofile.site/.Rprofile
| .libPaths(c("/usr/lib/R/site-library",<br /> &emsp;&emsp;&emsp;&emsp;&emsp; "/usr/lib/R/library"))
|-
| Renviron.site/.Renviron
| R_LIB_SITE="/usr/lib/R/site-library:/usr/lib/R/library"
|}
 
Scenario: We already have installed many R packages under R 3.1.X in the user's directory. Now we upgrade R to a new version (3.2.X). We like to have these packages available in R 3.2.X.


Actually the following hints will help us to create a convenient function UpdateMainLibrary() which will install updated main packages in the user's ''Documents'' directory without a warning dialog.
<span style="color:#0000FF">For Windows OS, refer to [http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f R for Windows FAQ]</span>
* '''.libPaths()''' only returns 1 string "C:/Program Files/R/R-x.y.z/library" on the machines that does not have this problem
* '''.libPaths()''' returns two strings "C:/Users/USERNAME/Documents/R/win-library/x.y" & "C:/Program Files/R/R-x.y.z/library" on machines with the problem.
<syntaxhighlight lang='rsplus'>
UpdateMainLibrary <- function() {
  # Update main/site packages
  # The function is used to fix the problem 'Would you like to use a personal library instead?' 
  if (length(.libPaths()) == 1) return()
 
  ind_mloc <- grep("Program", .libPaths()) # main library e.g. 2
  ind_ploc <- grep("Documents", .libPaths()) # personal library e.g. 1
  if (length(ind_mloc) > 0L && length(ind_ploc) > 0L)
    # search outdated main packages
old_mloc <- ! old.packages(.libPaths()[ind_mloc])[, "Package"] %in%
              installed.packages(.libPaths()[ind_ploc])[, "Package"]
    oldpac <- old.packages(.libPaths()[ind_mloc])[old_mloc, "Package"]
if (length(oldpac) > 0L)
        install.packages(oldpac, .libPaths()[ind_ploc]
}
</syntaxhighlight>


On Linux,
The follow method works on Linux and Windows.
<syntaxhighlight lang='rsplus'>
> update.packages()
...
The downloaded source packages are in
‘/tmp/RtmpBrYccd/downloaded_packages’
Warning in install.packages(update[instlib == l, "Package"], l, contriburl = contriburl,  :
                              'lib = "/opt/R/3.5.0/lib/R/library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) yes
...
> system("ls -lt /home/brb/R/x86_64-pc-linux-gnu-library/3.5 | head")
total 224
drwxrwxr-x  9 brb brb 4096 Oct  3 09:30 survival
drwxrwxr-x  9 brb brb 4096 Oct  3 09:29 mgcv
drwxrwxr-x 10 brb brb 4096 Oct  3 09:29 MASS
drwxrwxr-x  9 brb brb 4096 Oct  3 09:29 foreign


# So new versions of survival, mgc, MASS, foreign are installed in the personal directory
<span style="color:#FF0000">Make sure only one instance of R is running</span>
# The update.packages() will issue warnings if we try to run it again.
<pre>
# It's OK to ignore these warnings.
# Step 1. update R's built-in packages and install them on my personal directory
> update.packages()
update.packages(ask=FALSE, checkBuilt = TRUE, repos="http://cran.rstudio.com")
Warning: package 'foreign' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'MASS' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'mgcv' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'survival' in library '/opt/R/3.5.0/lib/R/library' will not be updated
</syntaxhighlight>


=== R_LIBS_USER is empty in R 3.4.1 ===
# Step 2. update Bioconductor packages
See  [[R#install.package.28.29_error.2C_R_LIBS_USER_is_empty_in_R_3.4.1|install.package() error, R_LIBS_USER is empty in R 3.4.1]].
.libPaths() # The first one is my personal directory
# [1] "/home/brb/R/x86_64-pc-linux-gnu-library/3.2"
# [2] "/usr/local/lib/R/site-library"
# [3] "/usr/lib/R/site-library"
# [4] "/usr/lib/R/library"


== List vignettes from a package ==
Sys.getenv("R_LIBS_USER") # may or may not equivalent to .libPaths()[1]
<syntaxhighlight lang='rsplus'>
ul <- unlist(strsplit(Sys.getenv("R_LIBS_USER"), "/"))
vignette(package=PACKAGENAME)
src <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.1")  
</syntaxhighlight>
des <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.2")
 
pkg <- dir(src, full.names = TRUE)
== List data from a package ==
if (!file.exists(des)) dir.create(des) # If 3.2 subdirectory does not exist yet!
<syntaxhighlight lang='rsplus'>
file.copy(pkg, des, overwrite=FALSE, recursive = TRUE)
data(package=PACKAGENAME)
source("http://www.bioconductor.org/biocLite.R")
</syntaxhighlight>
biocLite(ask = FALSE)
 
=== sysdata.rda ===
* [https://stackoverflow.com/a/24363504 Load data object when package is loaded]
* https://r-pkgs.org/data.html
 
== List all functions of a package ==
Assume a package is already loaded. Then
<pre>
ls("package:cowplot")
</pre>
</pre>


[http://www.cookbook-r.com/Scripts_and_functions/Getting_a_list_of_functions_and_objects_in_a_package/ Getting a list of functions and objects in a package]. This also assumes the package in loaded. In addition to functions (separated by primitive and non-primitive), it can show constants and objects.
<span style="color:#0000FF">From Robert Kabacoff ([https://www.statmethods.net/interface/customizing.html R in Action])</span>
* If you have a customized '''Rprofile.site file''' (see appendix B), save a copy outside of R.
* Launch your current version of R and issue the following statements
<pre>
oldip <- installed.packages()[,1]
save(oldip, file="path/installedPackages.Rdata")
</pre>
where ''path'' is a directory outside of R.
* Download and install the newer version of R.
* If you saved a customized version of the Rprofile.site file in step 1, copy it into the new installation.
* Launch the new version of R, and issue the following statements
<pre>
load("path/installedPackages.Rdata")
newip <- installed.packages()[,1]
for(i in setdiff(oldip, newip))
  install.packages(i)
</pre>
where path is the location specified in step 2.
*  Delete the old installation (optional).
 
This approach will install only packages that are available from the CRAN. It won’t find packages obtained from other locations. In fact, the process will display a list of packages that can’t be installed For example for packages obtained from Bioconductor, use the following method to update packages
<pre>
source(http://bioconductor.org/biocLite.R)
biocLite(PKGNAME)
</pre>


== List installed packages and versions ==
=== Persistent config and data for R packages with .Rprofile and .Renviron ===
* http://heuristicandrew.blogspot.com/2015/06/list-of-user-installed-r-packages-and.html
[https://blog.r-hub.io/2020/03/12/user-preferences/ Persistent config and data for R packages]. startup, rappdirs, hoardr, keyring.
* [http://cran.r-project.org/web/packages/checkpoint/index.html checkpoint] package


<syntaxhighlight lang='rsplus'>
=== Would you like to use a personal library instead? ===
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
Some posts from internet
rownames(ip) <- NULL
* [https://stackoverflow.com/questions/29969838/setting-r-libs-avoiding-would-you-like-to-use-a-personal-library-instead Setting R_LIBS & avoiding “Would you like to use a personal library instead?”]. Note: I try to create ~/.Renviron to add my personal folder in it. But update.packages() still asks me if I like to use a personal library instead (tested on Ubuntu + R 3.4).
unique(ip$Priority)
* [https://stackoverflow.com/questions/39879424/automatically-create-personal-library-in-r automatically create personal library in R]. Using suppressUpdates + specify '''lib''' in biocLite() or update.packages(Sys.getenv("R_LIBS_USER"), ask = F) <syntaxhighlight lang='rsplus'>
# [1] <NA>        base        recommended
# create local user library path (not present by default)
# Levels: base recommended
dir.create(path = Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
# install to local user library path
print(ip, row.names=FALSE)
install.packages(p, lib = Sys.getenv("R_LIBS_USER"), repos = "https://cran.rstudio.com/")
# Bioconductor version
biocLite(p, suppressUpdates = TRUE, lib = Sys.getenv("R_LIBS_USER"))
</syntaxhighlight>
</syntaxhighlight>


== Query the names of outdated packages ==
The problem can happen if the R was installed to the C:\Program Files\R folder by ''users'' but then some main packages want to be upgraded. R will always pops a message 'Would you like to use a personal library instead?'.  
{{Pre}}
psi <- packageStatus()$inst
subset(psi, Status == "upgrade", drop = FALSE)
#                    Package                                  LibPath    Version    Priority                Depends
# RcppArmadillo RcppArmadillo C:/Users/brb/Documents/R/win-library/3.2 0.5.100.1.0        <NA>                  <NA>
# Matrix              Matrix      C:/Program Files/R/R-3.2.0/library       1.2-0 recommended R (>= 2.15.2), methods
#                                            Imports LinkingTo                Suggests
# RcppArmadillo                      Rcpp (>= 0.11.0)      Rcpp RUnit, Matrix, pkgKitten
# Matrix        graphics, grid, stats, utils, lattice      <NA>              expm, MASS
#                                            Enhances    License License_is_FOSS License_restricts_use OS_type MD5sum
# RcppArmadillo                                  <NA> GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
# Matrix        MatrixModels, graph, SparseM, sfsmisc GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
#              NeedsCompilation Built  Status
# RcppArmadillo              yes 3.2.0 upgrade
# Matrix                    yes 3.2.0 upgrade
</pre>


The above output does not show the package version from the latest packages on CRAN. So the following snippet does that.
To suppress the message and use the personal library always,
{{Pre}}
* Run R as administrator. If you do that, main packages can be upgraded from C:\Program Files\R\R-X.Y.Z\library folder.
psi <- packageStatus()$inst
* [[Arraytools#Writable_R_package_directory_cannot_be_found|Writable R package directory cannot be found]] and a [[Arraytools#Download_required_R.2FBioconductor_.28software.29_packages|this]]. A solution here is to change the security of the R library folder so the user has a full control on the folder.
pl <- unname(psi$Package[psi$Status == "upgrade"]# List package names
* [https://cran.r-project.org/bin/windows/base/rw-FAQ.html#Does-R-run-under-Windows-Vista_003f Does R run under Windows Vista/7/8/Server 2008?] There are 3 ways to get around the issue.
ap <- as.data.frame(available.packages()[, c(1,2,3)], stringsAsFactors = FALSE)
* [https://cran.r-project.org/bin/windows/base/rw-FAQ.html#I-don_0027t-have-permission-to-write-to-the-R_002d3_002e3_002e2_005clibrary-directory I don’t have permission to write to the R-3.3.2\library directory]
out <- cbind(subset(psi, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
rownames(out) <- NULL
out
#         Package  OldVersion  NewVersion
# 1 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
# 2        Matrix      1.2-0      1.2-1
</pre>


To consider also the packages from Bioconductor, we have the following code. Note that "3.1" means the Bioconductor version and "3.2" is the R version. See [http://bioconductor.org/about/release-announcements/#release-versions Bioconductor release versions] page.
Actually the following hints will help us to create a convenient function UpdateMainLibrary() which will install updated main packages in the user's ''Documents'' directory without a warning dialog.
{{Pre}}
* '''.libPaths()''' only returns 1 string "C:/Program Files/R/R-x.y.z/library" on the machines that does not have this problem
psic <- packageStatus(repos = c(contrib.url(getOption("repos")),
* '''.libPaths()''' returns two strings "C:/Users/USERNAME/Documents/R/win-library/x.y" & "C:/Program Files/R/R-x.y.z/library" on machines with the problem.
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
<syntaxhighlight lang='rsplus'>
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))$inst
UpdateMainLibrary <- function() {
subset(psic, Status == "upgrade", drop = FALSE)
  # Update main/site packages
pl <- unname(psic$Package[psic$Status == "upgrade"])
  # The function is used to fix the problem 'Would you like to use a personal library instead?' 
 
  if (length(.libPaths()) == 1) return()
ap  <- as.data.frame(available.packages(c(contrib.url(getOption("repos")),
 
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
  ind_mloc <- grep("Program", .libPaths()) # main library e.g. 2
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))[, c(1:3)],
  ind_ploc <- grep("Documents", .libPaths()) # personal library e.g. 1
                      stringAsFactors = FALSE)
  if (length(ind_mloc) > 0L && length(ind_ploc) > 0L)
    # search outdated main packages
old_mloc <- ! old.packages(.libPaths()[ind_mloc])[, "Package"] %in%
              installed.packages(.libPaths()[ind_ploc])[, "Package"]
    oldpac <- old.packages(.libPaths()[ind_mloc])[old_mloc, "Package"]
if (length(oldpac) > 0L)
        install.packages(oldpac, .libPaths()[ind_ploc]
}
</syntaxhighlight>


out <- cbind(subset(psic, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
On Linux,
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
<syntaxhighlight lang='rsplus'>
rownames(out) <- NULL
> update.packages()
out
...
#        Package OldVersion NewVersion
The downloaded source packages are in
# 1        limma      3.24.5      3.24.9
‘/tmp/RtmpBrYccd/downloaded_packages’
# 2 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
Warning in install.packages(update[instlib == l, "Package"], l, contriburl = contriburl, :
# 3       Matrix      1.2-0      1.2-1
                              'lib = "/opt/R/3.5.0/lib/R/library"' is not writable
</pre>
Would you like to use a personal library instead? (yes/No/cancel) yes
...
> system("ls -lt /home/brb/R/x86_64-pc-linux-gnu-library/3.5 | head")
total 224
drwxrwxr-x  9 brb brb 4096 Oct  3 09:30 survival
drwxrwxr-x 9 brb brb 4096 Oct 3 09:29 mgcv
drwxrwxr-x 10 brb brb 4096 Oct  3 09:29 MASS
drwxrwxr-x  9 brb brb 4096 Oct  3 09:29 foreign


== Searching for packages in CRAN ==
# So new versions of survival, mgc, MASS, foreign are installed in the personal directory
* [https://github.com/metacran/pkgsearch#readme pkgsearch] package - Search R packages on CRAN
# The update.packages() will issue warnings if we try to run it again.
** [https://rviews.rstudio.com/2019/03/01/some-r-packages-for-roc-curves/ Some R Packages for ROC Curves]
# It's OK to ignore these warnings.
* [http://blog.revolutionanalytics.com/2015/06/fishing-for-packages-in-cran.html Fishing for packages in CRAN]
> update.packages()
* [http://blog.revolutionanalytics.com/2017/01/cran-10000.html CRAN now has 10,000 R packages. Here's how to find the ones you need]
Warning: package 'foreign' in library '/opt/R/3.5.0/lib/R/library' will not be updated
* [https://rviews.rstudio.com/2018/10/22/searching-for-r-packages/ Searching for R packages], [https://cran.r-project.org/web/packages/packagefinder/index.html packagefinder] package <syntaxhighlight lang='rsplus'>
Warning: package 'MASS' in library '/opt/R/3.5.0/lib/R/library' will not be updated
library(packagefinder)
Warning: package 'mgcv' in library '/opt/R/3.5.0/lib/R/library' will not be updated
findPackage("survival")
Warning: package 'survival' in library '/opt/R/3.5.0/lib/R/library' will not be updated
# 272 out of 13256 CRAN packages found in 5 seconds
</syntaxhighlight>
findPackage("follic")
# No results found.
# Actually 'follic' comes from randomForestSRC package
# https://www.rdocumentation.org/packages/randomForestSRC/versions/2.7.0
</syntaxhighlight> The result is shown in an html format with columns of SCORE, NAME, DESC_SHORT, DOWNL_TOTAL & GO.


== [http://www.r-pkg.org/ METACRAN (www.r-pkg.org)] - Search and browse all CRAN/R packages ==
=== R_LIBS_USER is empty in R 3.4.1 ===
* Source code on https://github.com/metacran. The 'PACKAGES' file is updated regularly to Github.
See  [[R#install.package.28.29_error.2C_R_LIBS_USER_is_empty_in_R_3.4.1|install.package() error, R_LIBS_USER is empty in R 3.4.1]].
* [https://stat.ethz.ch/pipermail/r-devel/2015-May/thread.html Announcement] on R/mailing list
 
* Author's homepage on http://gaborcsardi.org/.
== List vignettes from a package ==
<syntaxhighlight lang='rsplus'>
vignette(package=PACKAGENAME)
</syntaxhighlight>


== [https://cran.r-project.org/web/packages/cranly/ cranly] visualisations and summaries for R packages ==
== List data from a package ==
[https://rviews.rstudio.com/2018/05/31/exploring-r-packages/ Exploring R packages with cranly]
<syntaxhighlight lang='rsplus'>
data(package=PACKAGENAME)
</syntaxhighlight>


== Query top downloaded packages, download statistics ==
=== sysdata.rda ===
* Daily download statistics http://cran-logs.rstudio.com/. Note the page is split into 'package' download and 'R' download. It tracks
* [https://stackoverflow.com/a/24363504 Load data object when package is loaded]
** Package: date, time, size, r_version, r_arch, r_os, package, version, country, ip_id.
* https://r-pkgs.org/data.html
** R: date, time, size, R version, os (win/src/osx), county, ip_id (reset daily).
* Original methods
** https://strengejacke.wordpress.com/2015/03/07/cran-download-statistics-of-any-packages-rstats/
** http://blog.revolutionanalytics.com/2015/06/working-with-the-rstudio-cran-logs.html
* https://www.r-bloggers.com/finally-tracking-cran-packages-downloads/. The code still works.
** [https://gist.github.com/arraytools/f037a4fd90ae8fe8e2896db6fe5b7fad My modified code] for showing the top download R packages. The original code suffers from memory issue (tested on my 64GB Linux box) when it is calling rbindlist() from data.table package with large data. Still 64GB is required since the  matrix is 369M by 4 (12GB).
** [http://www.bnosac.be/index.php/blog/12-popularity-bigdata-large-data-packages-in-r-and-ffbase-user-presentation Popularity bigdata / large data packages in R and ffbase useR presentation]
* [https://blog.rsquaredacademy.com/introducing-pkginfo/ pkginfo]: Tools for Retrieving R Package Information. It's only in github. Shiny interface.
* [https://www.r-pkg.org/downloaded Top 100 downloaded packages] from METACRAN


=== cranlogs ===
== List all functions of a package ==
[https://github.com/metacran/cranlogs cranlogs] package - Download Logs from the RStudio CRAN Mirror. Suitable on R console.
Assume a package is already loaded. Then
* [https://colinfay.me/12-months-cranlogs/ 2018 through {cranlogs}]
* [https://hadley.shinyapps.io/cran-downloads/ Shiny app] by Hadley (works for packages on CRAN only). It's like the Google Trend app. An example of collection: '''survC1, survAUC, TreatmentSelection, biospear , (glmnet)'''
* [https://blog.r-hub.io/2019/05/02/cranlogs-2-1-1/ cranlogs 2.1.1 is on CRAN!] 5/2/2019
* [https://github.com/r-hub/cranlogs/issues/56 limit on number of packages as argument to cran_downloads]
<pre>
<pre>
library(cranlogs)
ls("package:cowplot")
out <- cran_top_downloads("last-week", 100) # 100 is the maximum limit
out$package
</pre>
</pre>


=== packageRank ===
[http://www.cookbook-r.com/Scripts_and_functions/Getting_a_list_of_functions_and_objects_in_a_package/ Getting a list of functions and objects in a package]. This also assumes the package in loaded. In addition to functions (separated by primitive and non-primitive), it can show constants and objects.
[https://github.com/lindbrook/packageRank packageRank] package: Computing and Visualizing CRAN Downloads. Suitable to run on RStudio cloud. Include both CRAN and Bioconductor.
{{Pre}}
> plot(cranDownloads(packages = c("packageRank", "limma"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019", to ="2019"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019-12", to ="2019-12"))
> plot(bioconductorDownloads(packages = c("edgeR", "DESeq2", "Rsubread", "limma"), when = "last-year"))
</pre>


=== BiocPkgTools ===
== List installed packages and versions ==
For Bioconductor packages, try [http://bioconductor.org/packages/release/bioc/html/BiocPkgTools.html BiocPkgTools]. See the [https://f1000research.com/articles/8-752 paper].
* http://heuristicandrew.blogspot.com/2015/06/list-of-user-installed-r-packages-and.html
* [http://cran.r-project.org/web/packages/checkpoint/index.html checkpoint] package


=== dlstats ===
<syntaxhighlight lang='rsplus'>
[https://cran.r-project.org/web/packages/dlstats/ dlstats]. Monthly download stats of 'CRAN' and 'Bioconductor' packages.
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
 
rownames(ip) <- NULL
== installation path not writeable from running biocLite() ==
unique(ip$Priority)
When I ran biocLite() to install a new package, I got a message (the Bioc packages are installed successfully anyway)
# [1] <NA>       base        recommended
<pre>
# Levels: base recommended
...
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
* DONE (curatedOvarianData)
print(ip, row.names=FALSE)
</syntaxhighlight>


The downloaded source packages are in
== Query the names of outdated packages ==
/tmp/RtmpHxnH2K/downloaded_packages’
{{Pre}}
installation path not writeable, unable to update packages: rgl, rJava,
psi <- packageStatus()$inst
  codetools, foreign, lattice, MASS, spatial, survival
subset(psi, Status == "upgrade", drop = FALSE)
#                    Package                                  LibPath    Version    Priority                Depends
# RcppArmadillo RcppArmadillo C:/Users/brb/Documents/R/win-library/3.2 0.5.100.1.0        <NA>                  <NA>
# Matrix              Matrix      C:/Program Files/R/R-3.2.0/library      1.2-0 recommended R (>= 2.15.2), methods
#                                            Imports LinkingTo                Suggests
# RcppArmadillo                      Rcpp (>= 0.11.0)      Rcpp RUnit, Matrix, pkgKitten
# Matrix        graphics, grid, stats, utils, lattice     <NA>              expm, MASS
#                                            Enhances    License License_is_FOSS License_restricts_use OS_type MD5sum
# RcppArmadillo                                  <NA> GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
# Matrix        MatrixModels, graph, SparseM, sfsmisc GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
#              NeedsCompilation Built  Status
# RcppArmadillo              yes 3.2.0 upgrade
# Matrix                    yes 3.2.0 upgrade
</pre>
 
The above output does not show the package version from the latest packages on CRAN. So the following snippet does that.
{{Pre}}
psi <- packageStatus()$inst
pl <- unname(psi$Package[psi$Status == "upgrade"])  # List package names
ap <- as.data.frame(available.packages()[, c(1,2,3)], stringsAsFactors = FALSE)
out <- cbind(subset(psi, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
rownames(out) <- NULL
out
#        Package  OldVersion  NewVersion
# 1 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
# 2        Matrix      1.2-0      1.2-1
</pre>
</pre>
However, if I uses install.package() it can update the package
<syntaxhighlight lang='rsplus'>
> packageVersion("survival")
[1] ‘2.42.3’
> update.packages("survival")  # Not working though no error message
> packageVersion("survival")
[1] ‘2.42.3’
> install.packages("survival")
Installing package into ‘/home/brb/R/x86_64-pc-linux-gnu-library/3.4’
...
* DONE (survival)


The downloaded source packages are in
To consider also the packages from Bioconductor, we have the following code. Note that "3.1" means the Bioconductor version and "3.2" is the R version. See [http://bioconductor.org/about/release-announcements/#release-versions Bioconductor release versions] page.
/tmp/RtmpHxnH2K/downloaded_packages’
{{Pre}}
> packageVersion("survival")
psic <- packageStatus(repos = c(contrib.url(getOption("repos")),
[1] ‘2.42.6’
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
> library(survival)
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))$inst
> sessionInfo() # show survival package 2.42-6 was attached
subset(psic, Status == "upgrade", drop = FALSE)
</syntaxhighlight>
pl <- unname(psic$Package[psic$Status == "upgrade"])


It makes sense to always use personal directory when we install packages. See .libPaths().
ap  <- as.data.frame(available.packages(c(contrib.url(getOption("repos")),
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))[, c(1:3)],
                      stringAsFactors = FALSE)


== Warning: cannot remove prior installation of package ==
out <- cbind(subset(psic, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
http://stackoverflow.com/questions/15932152/unloading-and-removing-a-loaded-package-withouth-restarting-r
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
 
rownames(out) <- NULL
Instance 1.
out
<pre>
#         Package  OldVersion  NewVersion
# Install the latest hgu133plus2cdf package
# 1         limma      3.24.5      3.24.9
# Remove/Uninstall hgu133plus2.db package
# 2 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
# Put/Install an old version of IRanges (eg version 1.18.2 while currently it is version 1.18.3)
# 3        Matrix      1.2-0      1.2-1
# Test on R 3.0.1
library(hgu133plus2cdf) # hgu133pluscdf does not depend or import IRanges
source("http://bioconductor.org/biocLite.R")
biocLite("hgu133plus2.db", ask=FALSE) # hgu133plus2.db imports IRanges
# Warning:cannot remove prior installation of package 'IRanges'
# Open Windows Explorer and check IRanges folder. Only see libs subfolder.
</pre>
</pre>


Note:
== Searching for packages in CRAN ==
* In the above example, all packages were installed under C:\Program Files\R\R-3.0.1\library\.
* [https://github.com/metacran/pkgsearch#readme pkgsearch] package - Search R packages on CRAN
* In another instance where I cannot reproduce the problem, new R packages were installed under C:\Users\xxx\Documents\R\win-library\3.0\. The different thing is IRanges package CAN be updated but if I use packageVersion("IRanges") command in R, it still shows the old version.
** [https://rviews.rstudio.com/2019/03/01/some-r-packages-for-roc-curves/ Some R Packages for ROC Curves]
* The above were tested on a desktop.
* [http://blog.revolutionanalytics.com/2015/06/fishing-for-packages-in-cran.html Fishing for packages in CRAN]
* [http://blog.revolutionanalytics.com/2017/01/cran-10000.html CRAN now has 10,000 R packages. Here's how to find the ones you need]
* [https://rviews.rstudio.com/2018/10/22/searching-for-r-packages/ Searching for R packages], [https://cran.r-project.org/web/packages/packagefinder/index.html packagefinder] package <syntaxhighlight lang='rsplus'>
library(packagefinder)
findPackage("survival")  
# 272 out of 13256 CRAN packages found in 5 seconds
findPackage("follic")
# No results found.
# Actually 'follic' comes from randomForestSRC package
# https://www.rdocumentation.org/packages/randomForestSRC/versions/2.7.0
</syntaxhighlight> The result is shown in an html format with columns of SCORE, NAME, DESC_SHORT, DOWNL_TOTAL & GO.


Instance 2.
== [http://www.r-pkg.org/ METACRAN (www.r-pkg.org)] - Search and browse all CRAN/R packages ==
{{Pre}}
* Source code on https://github.com/metacran. The 'PACKAGES' file is updated regularly to Github.
# On a fresh R 3.2.0, I install Bioconductor's depPkgTools & lumi packages. Then I close R, re-open it,
* [https://stat.ethz.ch/pipermail/r-devel/2015-May/thread.html Announcement] on R/mailing list
# and install depPkgTools package again.
* Author's homepage on http://gaborcsardi.org/.
> source("http://bioconductor.org/biocLite.R")
Bioconductor version 3.1 (BiocInstaller 1.18.2), ?biocLite for help
> biocLite("pkgDepTools")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘pkgDepTools’
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/pkgDepTools_1.34.0.zip'
Content type 'application/zip' length 390579 bytes (381 KB)
downloaded 381 KB


package ‘pkgDepTools’ successfully unpacked and MD5 sums checked
== [https://cran.r-project.org/web/packages/cranly/ cranly] visualisations and summaries for R packages ==
Warning: cannot remove prior installation of package ‘pkgDepTools’
[https://rviews.rstudio.com/2018/05/31/exploring-r-packages/ Exploring R packages with cranly]


The downloaded binary packages are in
== Query top downloaded packages, download statistics ==
        C:\Users\brb\AppData\Local\Temp\RtmpYd2l7i\downloaded_packages
* Daily download statistics http://cran-logs.rstudio.com/. Note the page is split into 'package' download and 'R' download. It tracks
> library(pkgDepTools)
** Package: date, time, size, r_version, r_arch, r_os, package, version, country, ip_id.
Error in library(pkgDepTools) : there is no package called ‘pkgDepTools’
** R: date, time, size, R version, os (win/src/osx), county, ip_id (reset daily).
* Original methods
** https://strengejacke.wordpress.com/2015/03/07/cran-download-statistics-of-any-packages-rstats/
** http://blog.revolutionanalytics.com/2015/06/working-with-the-rstudio-cran-logs.html
* https://www.r-bloggers.com/finally-tracking-cran-packages-downloads/. The code still works.
** [https://gist.github.com/arraytools/f037a4fd90ae8fe8e2896db6fe5b7fad My modified code] for showing the top download R packages. The original code suffers from memory issue (tested on my 64GB Linux box) when it is calling rbindlist() from data.table package with large data. Still 64GB is required since the  matrix is 369M by 4 (12GB).
** [http://www.bnosac.be/index.php/blog/12-popularity-bigdata-large-data-packages-in-r-and-ffbase-user-presentation Popularity bigdata / large data packages in R and ffbase useR presentation]
* [https://blog.rsquaredacademy.com/introducing-pkginfo/ pkginfo]: Tools for Retrieving R Package Information. It's only in github. Shiny interface.
* [https://www.r-pkg.org/downloaded Top 100 downloaded packages] from METACRAN
 
=== cranlogs ===
[https://github.com/metacran/cranlogs cranlogs] package - Download Logs from the RStudio CRAN Mirror. Suitable on R console.
* [https://colinfay.me/12-months-cranlogs/ 2018 through {cranlogs}]
* [https://hadley.shinyapps.io/cran-downloads/ Shiny app] by Hadley (works for packages on CRAN only). It's like the Google Trend app. An example of collection: '''survC1, survAUC, TreatmentSelection, biospear , (glmnet)'''
* [https://blog.r-hub.io/2019/05/02/cranlogs-2-1-1/ cranlogs 2.1.1 is on CRAN!] 5/2/2019
* [https://github.com/r-hub/cranlogs/issues/56 limit on number of packages as argument to cran_downloads]
<pre>
library(cranlogs)
out <- cran_top_downloads("last-week", 100) # 100 is the maximum limit
out$package
</pre>
</pre>
The pkgDepTools library folder appears in C:\Users\brb\Documents\R\win-library\3.2, but it is empty. The weird thing is if I try the above steps again, I cannot reproduce the problem.


== Warning: dependency ‘XXX’ is not available ==
=== packageRank ===
[https://stackoverflow.com/a/25721890 How should I deal with “package 'xxx' is not available (for R version x.y.z)” warning?]
[https://github.com/lindbrook/packageRank packageRank] package: Computing and Visualizing CRAN Downloads. Suitable to run on RStudio cloud. Include both CRAN and Bioconductor.
{{Pre}}
> plot(cranDownloads(packages = c("packageRank", "limma"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019", to ="2019"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019-12", to ="2019-12"))
> plot(bioconductorDownloads(packages = c("edgeR", "DESeq2", "Rsubread", "limma"), when = "last-year"))
</pre>
 
=== BiocPkgTools ===
For Bioconductor packages, try [http://bioconductor.org/packages/release/bioc/html/BiocPkgTools.html BiocPkgTools]. See the [https://f1000research.com/articles/8-752 paper].


== Error: there is no package called XXX ==  
=== dlstats ===
The error happened when I try to run library() command on a package that was just installed. R 3.6.0. 'biospear' version is 1.0.2. macOS.
[https://cran.r-project.org/web/packages/dlstats/ dlstats]. Monthly download stats of 'CRAN' and 'Bioconductor' packages.
{{Pre}}
> library(biospear)
Loading required package: pkgconfig
Error: package or namespace load failed for ‘biospear’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
there is no package called ‘mixOmics’
</pre>


Note
== installation path not writeable from running biocLite() ==
When I ran biocLite() to install a new package, I got a message (the Bioc packages are installed successfully anyway)
<pre>
...
* DONE (curatedOvarianData)


* The package [https://cran.r-project.org/web/packages/mixOmics/index.html mixOmics] was removed from CRAN. It is now available on Bioconductor.
The downloaded source packages are in
* Tested to install on a docker container: '''docker run --net=host -it --rm r-base''' <syntaxhighlight lang='rsplus'>
‘/tmp/RtmpHxnH2K/downloaded_packages’
ERROR: dependency ‘car’ is not available for package ‘plsRglm’
installation path not writeable, unable to update packages: rgl, rJava,
* removing ‘/usr/local/lib/R/site-library/plsRglm’
  codetools, foreign, lattice, MASS, spatial, survival
ERROR: dependencies ‘plsRglm’, ‘mixOmics’, ‘survcomp’ are not available for package ‘plsRcox’
</pre>
* removing ‘/usr/local/lib/R/site-library/plsRcox’
However, if I uses install.package() it can update the package
ERROR: dependencies ‘devtools’, ‘plsRcox’, ‘RCurl’ are not available for package ‘biospear’
<syntaxhighlight lang='rsplus'>
* removing ‘/usr/local/lib/R/site-library/biospear’
> packageVersion("survival")
</syntaxhighlight>The car package looks OK on CRAN. [https://cran.r-project.org/web/packages/survcomp/index.html survcomp] was moved from CRAN to Bioconductor too.
[1] ‘2.42.3’
* As we can see above, the official r-base image does not contain libraries enough to install RCurl/devtools packages. Consider the [https://hub.docker.com/r/rocker/tidyverse tidyverse] image (based on [https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/Dockerfile RStudio image]) from the rocker project. <syntaxhighlight lang='bash'>
> update.packages("survival")  # Not working though no error message
docker pull rocker/tidyverse:3.6.0
> packageVersion("survival")
docker run --net=host -it --rm -e PASSWORD=password -p 8787:8787 rocker/tidyverse:3.6.0
[1] ‘2.42.3’
# the default username is 'rstudio'
> install.packages("survival")
# Open a browser, log in. Run 'install.packages("RCurl")'. It works.
Installing package into ‘/home/brb/R/x86_64-pc-linux-gnu-library/3.4’
...
* DONE (survival)
 
The downloaded source packages are in
‘/tmp/RtmpHxnH2K/downloaded_packages’
> packageVersion("survival")
[1] ‘2.42.6’
> library(survival)
> sessionInfo() # show survival package 2.42-6 was attached
</syntaxhighlight>
</syntaxhighlight>
* Testing on Mint linux also shows errors about dependencies of mixOmics and survcomp.
* '''The best practice to install a package that may depend on packages located/moved to Bioconductor''': Run '''setRepositories(ind=1:2)''' before calling install.packages(). However, it does not '''remedy''' the situation that the 1st level imports package (eg plsRcox) was installed before but the 2nd level imports package (eg mixOmics) was not installed.
* [https://stat.ethz.ch/R-manual/R-devel/library/tools/html/dependsOnPkgs.html dependsOnPkgs()]: Find Reverse Dependencies


== Warning: Unable to move temporary installation ==
It makes sense to always use personal directory when we install packages. See .libPaths().
The problem seems to happen only on virtual machines (Virtualbox).
* '''Warning: unable to move temporary installation `C:\Users\brb\Documents\R\win-library\3.0\fileed8270978f5\quadprog`  to `C:\Users\brb\Documents\R\win-library\3.0\quadprog`''' when I try to run 'install.packages("forecast").
* '''Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’ to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’ ''' when I try to run 'biocLite("lumi")'. The other dependency packages look fine although I am not sure if any unknown problem can happen (it does, see below).


Here is a note of my trouble shooting.
== Warning: cannot remove prior installation of package ==
# If I try to ignore the warning and load the lumi package. I will get an error.
http://stackoverflow.com/questions/15932152/unloading-and-removing-a-loaded-package-withouth-restarting-r
# If I try to run biocLite("lumi") again, it will only download & install lumi without checking missing 'plyr' package. Therefore, when I try to load the lumi package, it will give me an error again.
# Even I install the plyr package manually, library(lumi) gives another error - missing mclust package.
{{Pre}}
> biocLite("lumi")
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/BiocInstaller_1.18.2.zip'
Content type 'application/zip' length 114097 bytes (111 KB)
downloaded 111 KB
...
package ‘lumi’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in
Instance 1.
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
<pre>
Old packages: 'BiocParallel', 'Biostrings', 'caret', 'DESeq2', 'gdata', 'GenomicFeatures', 'gplots', 'Hmisc', 'Rcpp', 'RcppArmadillo', 'rgl',
# Install the latest hgu133plus2cdf package
  'stringr'
# Remove/Uninstall hgu133plus2.db package
Update all/some/none? [a/s/n]: a
# Put/Install an old version of IRanges (eg version 1.18.2 while currently it is version 1.18.3)
also installing the dependencies ‘Rsamtools’, ‘GenomicAlignments’, ‘plyr’, ‘rtracklayer’, ‘gridExtra’, ‘stringi’, ‘magrittr’
# Test on R 3.0.1
library(hgu133plus2cdf) # hgu133pluscdf does not depend or import IRanges
source("http://bioconductor.org/biocLite.R")
biocLite("hgu133plus2.db", ask=FALSE) # hgu133plus2.db imports IRanges
# Warning:cannot remove prior installation of package 'IRanges'
# Open Windows Explorer and check IRanges folder. Only see libs subfolder.
</pre>


trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/Rsamtools_1.20.1.zip'
Note:
Content type 'application/zip' length 8138197 bytes (7.8 MB)
* In the above example, all packages were installed under C:\Program Files\R\R-3.0.1\library\.
downloaded 7.8 MB
* In another instance where I cannot reproduce the problem, new R packages were installed under C:\Users\xxx\Documents\R\win-library\3.0\. The different thing is IRanges package CAN be updated but if I use packageVersion("IRanges") command in R, it still shows the old version.
...
* The above were tested on a desktop.
package ‘Rsamtools’ successfully unpacked and MD5 sums checked
package ‘GenomicAlignments’ successfully unpacked and MD5 sums checked
package ‘plyr’ successfully unpacked and MD5 sums checked
Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’
        to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’
package ‘rtracklayer’ successfully unpacked and MD5 sums checked
package ‘gridExtra’ successfully unpacked and MD5 sums checked
package ‘stringi’ successfully unpacked and MD5 sums checked
package ‘magrittr’ successfully unpacked and MD5 sums checked
package ‘BiocParallel’ successfully unpacked and MD5 sums checked
package ‘Biostrings’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘Biostrings’
package ‘caret’ successfully unpacked and MD5 sums checked
package ‘DESeq2’ successfully unpacked and MD5 sums checked
package ‘gdata’ successfully unpacked and MD5 sums checked
package ‘GenomicFeatures’ successfully unpacked and MD5 sums checked
package ‘gplots’ successfully unpacked and MD5 sums checked
package ‘Hmisc’ successfully unpacked and MD5 sums checked
package ‘Rcpp’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘rgl’ successfully unpacked and MD5 sums checked
package ‘stringr’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in
Instance 2.
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
{{Pre}}
> library(lumi)
# On a fresh R 3.2.0, I install Bioconductor's depPkgTools & lumi packages. Then I close R, re-open it,  
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
# and install depPkgTools package again.
  there is no package called ‘plyr’
> source("http://bioconductor.org/biocLite.R")
Error: package or namespace load failed for ‘lumi’
Bioconductor version 3.1 (BiocInstaller 1.18.2), ?biocLite for help
> search()
> biocLite("pkgDepTools")
[1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"      "package:BiocGenerics"  "package:parallel"      "package:stats"       
[7] "package:graphics"      "package:grDevices"    "package:utils"        "package:datasets"      "package:methods"      "Autoloads"           
[13] "package:base"       
> biocLite("lumi")
BioC_mirror: http://bioconductor.org
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘lumi’
Installing package(s) ‘pkgDepTools’
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/lumi_2.20.1.zip'
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/pkgDepTools_1.34.0.zip'
Content type 'application/zip' length 18185326 bytes (17.3 MB)
Content type 'application/zip' length 390579 bytes (381 KB)
downloaded 17.3 MB
downloaded 381 KB


package ‘lumi’ successfully unpacked and MD5 sums checked
package ‘pkgDepTools’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘pkgDepTools’


The downloaded binary packages are in
The downloaded binary packages are in
         C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
         C:\Users\brb\AppData\Local\Temp\RtmpYd2l7i\downloaded_packages
> search()
> library(pkgDepTools)
[1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"      "package:BiocGenerics"  "package:parallel"      "package:stats"       
Error in library(pkgDepTools) : there is no package called ‘pkgDepTools’
[7] "package:graphics"      "package:grDevices"    "package:utils"        "package:datasets"      "package:methods"      "Autoloads"           
</pre>
[13] "package:base"       
The pkgDepTools library folder appears in C:\Users\brb\Documents\R\win-library\3.2, but it is empty. The weird thing is if I try the above steps again, I cannot reproduce the problem.
> library(lumi)
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :  
  there is no package called ‘plyr’
Error: package or namespace load failed for ‘lumi’
> biocLite("plyr")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘plyr’
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/plyr_1.8.2.zip'
Content type 'application/zip' length 1128621 bytes (1.1 MB)
downloaded 1.1 MB


package ‘plyr’ successfully unpacked and MD5 sums checked
== Warning: dependency ‘XXX’ is not available ==
[https://stackoverflow.com/a/25721890 How should I deal with “package 'xxx' is not available (for R version x.y.z)” warning?]


The downloaded binary packages are in
== Error: there is no package called XXX ==
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
The error happened when I try to run library() command on a package that was just installed. R 3.6.0. 'biospear' version is 1.0.2. macOS.
{{Pre}}
> library(biospear)
Loading required package: pkgconfig
Error: package or namespace load failed for ‘biospear’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
there is no package called ‘mixOmics’
</pre>


> library(lumi)
Note
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
  there is no package called ‘mclust’
Error: package or namespace load failed for ‘lumi’


> ?biocLite
* The package [https://cran.r-project.org/web/packages/mixOmics/index.html mixOmics] was removed from CRAN. It is now available on Bioconductor.
Warning messages:
* Tested to install on a docker container: '''docker run --net=host -it --rm r-base''' <syntaxhighlight lang='rsplus'>
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
ERROR: dependency ‘car’ is not available for package ‘plsRglm’
  cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
* removing ‘/usr/local/lib/R/site-library/plsRglm’
2: In find.package(if (is.null(package)) loadedNamespaces() else package,  :
ERROR: dependencies ‘plsRglm’, ‘mixOmics’, ‘survcomp’ are not available for package ‘plsRcox’
  there is no package called ‘Biostrings’
* removing ‘/usr/local/lib/R/site-library/plsRcox’
> library(lumi)
ERROR: dependencies ‘devtools’, ‘plsRcox’, ‘RCurl’ are not available for package ‘biospear’
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
* removing ‘/usr/local/lib/R/site-library/biospear’
  there is no package called ‘mclust’
</syntaxhighlight>The car package looks OK on CRAN. [https://cran.r-project.org/web/packages/survcomp/index.html survcomp] was moved from CRAN to Bioconductor too.
In addition: Warning messages:
* As we can see above, the official r-base image does not contain libraries enough to install RCurl/devtools packages. Consider the [https://hub.docker.com/r/rocker/tidyverse tidyverse] image (based on [https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/Dockerfile RStudio image]) from the rocker project. <syntaxhighlight lang='bash'>
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
docker pull rocker/tidyverse:3.6.0
  cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
docker run --net=host -it --rm -e PASSWORD=password -p 8787:8787 rocker/tidyverse:3.6.0
2: In find.package(if (is.null(package)) loadedNamespaces() else package, :
# the default username is 'rstudio'
  there is no package called ‘Biostrings’
# Open a browser, log in. Run 'install.packages("RCurl")'. It works.
Error: package or namespace load failed for ‘lumi’
</syntaxhighlight>
</pre>
* Testing on Mint linux also shows errors about dependencies of mixOmics and survcomp.
* '''The best practice to install a package that may depend on packages located/moved to Bioconductor''': Run '''setRepositories(ind=1:2)''' before calling install.packages(). However, it does not '''remedy''' the situation that the 1st level imports package (eg plsRcox) was installed before but the 2nd level imports package (eg mixOmics) was not installed.
* [https://stat.ethz.ch/R-manual/R-devel/library/tools/html/dependsOnPkgs.html dependsOnPkgs()]: Find Reverse Dependencies. It seems it only return packages that have been installed locally. For example, tools::dependsOnPkgs("RcppEigen", "LinkingTo")


[http://r.789695.n4.nabble.com/unable-to-move-temporary-installation-td4521714.html Other people also have the similar problem]. The possible cause is the virus scanner locks the file and R cannot move them.
== Warning: Unable to move temporary installation ==
The problem seems to happen only on virtual machines (Virtualbox).
* '''Warning: unable to move temporary installation `C:\Users\brb\Documents\R\win-library\3.0\fileed8270978f5\quadprog`  to `C:\Users\brb\Documents\R\win-library\3.0\quadprog`''' when I try to run 'install.packages("forecast").
* '''Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’ to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’ ''' when I try to run 'biocLite("lumi")'. The other dependency packages look fine although I am not sure if any unknown problem can happen (it does, see below).


Some possible solutions:
Here is a note of my trouble shooting.
# Delete ALL folders under R/library (e.g. C:/Progra~1/R/R-3.2.0/library) folder and install the main package again using install.packages() or biocLite().
# If I try to ignore the warning and load the lumi package. I will get an error.
# For specific package like 'lumi' from Bioconductor, we can [[R#Bioconductor.27s_pkgDepTools_package|find out all dependency packages]] and then install them one by one.
# If I try to run biocLite("lumi") again, it will only download & install lumi without checking missing 'plyr' package. Therefore, when I try to load the lumi package, it will give me an error again.
# Find out and install the top level package which misses dependency packages.  
# Even I install the plyr package manually, library(lumi) gives another error - missing mclust package.
## This is based on the fact that install.packages() or biocLite() '''sometimes''' just checks & installs the 'Depends' and 'Imports' packages and '''won't install all packages recursively'''
{{Pre}}
## we can do a small experiment by removing a package which is not directly dependent/imported by another package; e.g. 'iterators' is not dependent/imported by 'glment' directly but indirectly. So if we run '''remove.packages("iterators"); install.packages("glmnet")''', then the 'iterator' package is still missing.
> biocLite("lumi")
## A real example is if the missing packages are 'Biostrings', 'limma', 'mclust' (these are packages that 'minfi' directly depends/imports although they should be installed when I run biocLite("lumi") command), then I should just run the command '''remove.packages("minfi"); biocLite("minfi")'''. If we just run biocLite("lumi") or biocLite("methylumi"), the missing packages won't be installed.
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/BiocInstaller_1.18.2.zip'
Content type 'application/zip' length 114097 bytes (111 KB)
downloaded 111 KB
...
package ‘lumi’ successfully unpacked and MD5 sums checked


== Error in download.file(url, destfile, method, mode = "wb", ...) ==
The downloaded binary packages are in
HTTP status was '404 Not Found'
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
Old packages: 'BiocParallel', 'Biostrings', 'caret', 'DESeq2', 'gdata', 'GenomicFeatures', 'gplots', 'Hmisc', 'Rcpp', 'RcppArmadillo', 'rgl',
  'stringr'
Update all/some/none? [a/s/n]: a
also installing the dependencies ‘Rsamtools’, ‘GenomicAlignments’, ‘plyr’, ‘rtracklayer’, ‘gridExtra’, ‘stringi’, ‘magrittr’


Tested on an existing R-3.2.0 session. Note that VariantAnnotation 1.14.4 was just uploaded to Bioc.
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/Rsamtools_1.20.1.zip'
{{pre}}
Content type 'application/zip' length 8138197 bytes (7.8 MB)
> biocLite("COSMIC.67")
downloaded 7.8 MB
BioC_mirror: http://bioconductor.org
...
Using Bioconductor version 3.1 (BiocInstaller 1.18.3), R version 3.2.0.
package ‘Rsamtools’ successfully unpacked and MD5 sums checked
Installing package(s) ‘COSMIC.67’
package ‘GenomicAlignments’ successfully unpacked and MD5 sums checked
also installing the dependency ‘VariantAnnotation’
package ‘plyr’ successfully unpacked and MD5 sums checked
Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’
        to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’
package ‘rtracklayer’ successfully unpacked and MD5 sums checked
package ‘gridExtra’ successfully unpacked and MD5 sums checked
package ‘stringi’ successfully unpacked and MD5 sums checked
package ‘magrittr’ successfully unpacked and MD5 sums checked
package ‘BiocParallel’ successfully unpacked and MD5 sums checked
package ‘Biostrings’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘Biostrings’
package ‘caret’ successfully unpacked and MD5 sums checked
package ‘DESeq2’ successfully unpacked and MD5 sums checked
package ‘gdata’ successfully unpacked and MD5 sums checked
package ‘GenomicFeatures’ successfully unpacked and MD5 sums checked
package ‘gplots’ successfully unpacked and MD5 sums checked
package ‘Hmisc’ successfully unpacked and MD5 sums checked
package ‘Rcpp’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘rgl’ successfully unpacked and MD5 sums checked
package ‘stringr’ successfully unpacked and MD5 sums checked


trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
The downloaded binary packages are in
Error in download.file(url, destfile, method, mode = "wb", ...) :
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
  cannot open URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
> library(lumi)
In addition: Warning message:
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
In download.file(url, destfile, method, mode = "wb", ...) :
  there is no package called ‘plyr’
  cannot open: HTTP status was '404 Not Found'
Error: package or namespace load failed for ‘lumi’
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
> search()
  download of package ‘VariantAnnotation’ failed
[1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"      "package:BiocGenerics"  "package:parallel"      "package:stats"       
installing the source package ‘COSMIC.67’
[7] "package:graphics"      "package:grDevices"    "package:utils"        "package:datasets"      "package:methods"      "Autoloads"           
[13] "package:base"       
> biocLite("lumi")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘lumi’
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/lumi_2.20.1.zip'
Content type 'application/zip' length 18185326 bytes (17.3 MB)
downloaded 17.3 MB


trying URL 'http://bioconductor.org/packages/3.1/data/experiment/src/contrib/COSMIC.67_1.4.0.tar.gz'
package ‘lumi’ successfully unpacked and MD5 sums checked
Content type 'application/x-gzip' length 40999037 bytes (39.1 MB)
</pre>


However, when I tested on a new R-3.2.0 (just installed in VM), the COSMIC package installation is successful. That VariantAnnotation version 1.14.4 was installed (this version was just updated today from Bioconductor).
The downloaded binary packages are in
 
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
The cause of the error is the '''[https://github.com/wch/r-source/blob/trunk/src/library/utils/R/packages.R available.package()]''' function will read the rds file first from cache in a tempdir (C:\Users\XXXX\AppData\Local\Temp\RtmpYYYYYY). See lines 51-55 of <packages.R>.
> search()
<pre>
  [1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"      "package:BiocGenerics"  "package:parallel"      "package:stats"       
  dest <- file.path(tempdir(),
[7] "package:graphics"      "package:grDevices"    "package:utils"        "package:datasets"      "package:methods"      "Autoloads"           
                  paste0("repos_", URLencode(repos, TRUE), ".rds"))
[13] "package:base"       
if(file.exists(dest)) {
> library(lumi)
    res0 <- readRDS(dest)
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
} else {
  there is no package called ‘plyr’
    ...
Error: package or namespace load failed for ‘lumi’
> biocLite("plyr")
</pre>
BioC_mirror: http://bioconductor.org
Since my R was opened 1 week ago, the rds file it reads today contains old information. Note that Bioconductor does not hold the source code or binary code for the old version of packages. This explains why biocLite() function broke. When I restart R, the original problem is gone.
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘plyr’
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/plyr_1.8.2.zip'
Content type 'application/zip' length 1128621 bytes (1.1 MB)
downloaded 1.1 MB


If we look at the source code of available.packages(), we will see we could use '''cacheOK''' option in download.file() function.
package ‘plyr’ successfully unpacked and MD5 sums checked
<pre>
download.file(url, destfile, method, cacheOK = FALSE, quiet = TRUE, mode ="wb")
</pre>


== Another case: Error in download.file(url, destfile, method, mode = "wb", ...) ==
The downloaded binary packages are in
{{Pre}}
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
> install.packages("quantreg")


   There is a binary version available but the source version is later:
> library(lumi)
        binary source needs_compilation
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
quantreg  5.33  5.34              TRUE
   there is no package called ‘mclust’
Error: package or namespace load failed for ‘lumi’


Do you want to install from sources the package which needs compilation?
> ?biocLite
y/n: n
Warning messages:
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
Warning in install.packages :
   cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
   cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz': HTTP status was '404 Not Found'
2: In find.package(if (is.null(package)) loadedNamespaces() else package,  :
Error in download.file(url, destfile, method, mode = "wb", ...) :  
  there is no package called ‘Biostrings’
   cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
> library(lumi)
Warning in install.packages :
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
   download of package ‘quantreg’ failed
  there is no package called ‘mclust’
In addition: Warning messages:
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
   cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
2: In find.package(if (is.null(package)) loadedNamespaces() else package,  :
   there is no package called ‘Biostrings’
Error: package or namespace load failed for ‘lumi’
</pre>
</pre>


It seems the binary package cannot be found on the mirror. So the solution here is to download the package from the R main server. Note that after I have successfully installed the binary package from the main R server, I remove the package in R and try to install the binary package from rstudio.com server agin and it works this time.  
[http://r.789695.n4.nabble.com/unable-to-move-temporary-installation-td4521714.html Other people also have the similar problem]. The possible cause is the virus scanner locks the file and R cannot move them.


<pre>
Some possible solutions:
> install.packages("quantreg", repos = "https://cran.r-project.org")
# Delete ALL folders under R/library (e.g. C:/Progra~1/R/R-3.2.0/library) folder and install the main package again using install.packages() or biocLite().
trying URL 'https://cran.r-project.org/bin/macosx/el-capitan/contrib/3.4/quantreg_5.34.tgz'
# For specific package like 'lumi' from Bioconductor, we can [[R#Bioconductor.27s_pkgDepTools_package|find out all dependency packages]] and then install them one by one.
Content type 'application/x-gzip' length 1863561 bytes (1.8 MB)
# Find out and install the top level package which misses dependency packages.  
==================================================
## This is based on the fact that install.packages() or biocLite() '''sometimes''' just checks & installs the 'Depends' and 'Imports' packages and '''won't install all packages recursively'''
downloaded 1.8 MB
## we can do a small experiment by removing a package which is not directly dependent/imported by another package; e.g. 'iterators' is not dependent/imported by 'glment' directly but indirectly. So if we run '''remove.packages("iterators"); install.packages("glmnet")''', then the 'iterator' package is still missing.
</pre>
## A real example is if the missing packages are 'Biostrings', 'limma', 'mclust' (these are packages that 'minfi' directly depends/imports although they should be installed when I run biocLite("lumi") command), then I should just run the command '''remove.packages("minfi"); biocLite("minfi")'''. If we just run biocLite("lumi") or biocLite("methylumi"), the missing packages won't be installed.


==== Another case: Error in download.file() on Windows 7 ====
== Error in download.file(url, destfile, method, mode = "wb", ...) ==
For some reason, IE 8 cannot interpret https://ftp.ncbi.nlm.nih.gov though it understands ftp://ftp.ncbi.nlm.nih.gov.
HTTP status was '404 Not Found'


This is tested using R 3.4.3.
Tested on an existing R-3.2.0 session. Note that VariantAnnotation 1.14.4 was just uploaded to Bioc.
{{Pre}}
{{pre}}
> download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
> biocLite("COSMIC.67")
trying URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
BioC_mirror: http://bioconductor.org
Error in download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz",  :  
Using Bioconductor version 3.1 (BiocInstaller 1.18.3), R version 3.2.0.
   cannot open URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
Installing package(s) ‘COSMIC.67’
also installing the dependency ‘VariantAnnotation’
 
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
Error in download.file(url, destfile, method, mode = "wb", ...) :  
   cannot open URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
In addition: Warning message:
In addition: Warning message:
In download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz",  :
In download.file(url, destfile, method, mode = "wb", ...) :
   InternetOpenUrl failed: 'An error occurred in the secure channel support'
  cannot open: HTTP status was '404 Not Found'
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
   download of package ‘VariantAnnotation’ failed
installing the source package ‘COSMIC.67’


> download.file("ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
trying URL 'http://bioconductor.org/packages/3.1/data/experiment/src/contrib/COSMIC.67_1.4.0.tar.gz'
trying URL 'ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
Content type 'application/x-gzip' length 40999037 bytes (39.1 MB)
downloaded 9.1 MB
</pre>
</pre>


== ERROR: failed to lock directory ==
However, when I tested on a new R-3.2.0 (just installed in VM), the COSMIC package installation is successful. That VariantAnnotation version 1.14.4 was installed (this version was just updated today from Bioconductor).
Follow the suggestion to remove the LOCK file. See the [https://stackoverflow.com/questions/23483303/error-failed-to-lock-directory-c-program-files-r-r-3-0-2-library-for-modifyi post].


It could happened in calling install.packages(), biocLite() or devtools::install_github(), and so on.
The cause of the error is the '''[https://github.com/wch/r-source/blob/trunk/src/library/utils/R/packages.R available.package()]''' function will read the rds file first from cache in a tempdir (C:\Users\XXXX\AppData\Local\Temp\RtmpYYYYYY). See lines 51-55 of <packages.R>.
<pre>
dest <- file.path(tempdir(),
                  paste0("repos_", URLencode(repos, TRUE), ".rds"))
if(file.exists(dest)) {
    res0 <- readRDS(dest)
} else {
    ...
</pre>
Since my R was opened 1 week ago, the rds file it reads today contains old information. Note that Bioconductor does not hold the source code or binary code for the old version of packages. This explains why biocLite() function broke. When I restart R, the original problem is gone.


== Error in unloadNamespace(package) ==
If we look at the source code of available.packages(), we will see we could use '''cacheOK''' option in download.file() function.
<pre>
<pre>
> d3heatmap(mtcars, scale = "column", colors = "Blues")
download.file(url, destfile, method, cacheOK = FALSE, quiet = TRUE, mode ="wb")
Error: 'col_numeric' is not an exported object from 'namespace:scales'
> packageVersion("scales")
[1] ‘0.2.5’
> library(scales)
Error in unloadNamespace(package) :
  namespace ‘scales’ is imported by ‘ggplot2’ so cannot be unloaded
In addition: Warning message:
package ‘scales’ was built under R version 3.2.1
Error in library(scales) :
  Package ‘scales’ version 0.2.4 cannot be unloaded
> search()
[1] ".GlobalEnv"            "package:d3heatmap"      "package:ggplot2"     
[4] "package:microbenchmark" "package:COSMIC.67"      "package:BiocInstaller"
[7] "package:stats"          "package:graphics"      "package:grDevices"   
[10] "package:utils"          "package:datasets"      "package:methods"     
[13] "Autoloads"              "package:base"
</pre>
</pre>
If I open a new R session, the above error will not happen!


The problem occurred because the 'scales' package version required by the d3heatmap package/function is old. See [https://github.com/rstudio/d3heatmap/issues/16 this post]. And when I upgraded the 'scales' package, it was ''locked'' by the package was ''imported'' by the ''ggplot2'' package.
== Another case: Error in download.file(url, destfile, method, mode = "wb", ...) ==
{{Pre}}
> install.packages("quantreg")


== Unload a package ==
  There is a binary version available but the source version is later:
Add '''unload = TRUE''' option to unload the namespace. See [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/detach detach()].
        binary source needs_compilation
<pre>
quantreg  5.33  5.34              TRUE
require(splines)
detach(package:splines, unload=TRUE)
</pre>


== crantastic ==
Do you want to install from sources the package which needs compilation?
https://crantastic.org/. A community site for R packages where you can search for, review and tag CRAN packages.
y/n: n
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
Warning in install.packages :
  cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz': HTTP status was '404 Not Found'
Error in download.file(url, destfile, method, mode = "wb", ...) :
  cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
Warning in install.packages :
  download of package ‘quantreg’ failed
</pre>


https://github.com/hadley/crantastic
It seems the binary package cannot be found on the mirror. So the solution here is to download the package from the R main server. Note that after I have successfully installed the binary package from the main R server, I remove the package in R and try to install the binary package from rstudio.com server agin and it works this time.  
 
== New R packages as reported by [http://dirk.eddelbuettel.com/cranberries/ CRANberries] ==
http://blog.revolutionanalytics.com/2015/07/mranspackages-spotlight.html


<pre>
<pre>
#----------------------------
> install.packages("quantreg", repos = "https://cran.r-project.org")
# SCRAPE CRANBERRIES FILES TO COUNT NEW PACKAGES AND PLOT
trying URL 'https://cran.r-project.org/bin/macosx/el-capitan/contrib/3.4/quantreg_5.34.tgz'
#
Content type 'application/x-gzip' length 1863561 bytes (1.8 MB)
library(ggplot2)
==================================================
# Build a vextor of the directories of interest
downloaded 1.8 MB
year <- c("2013","2014","2015")
</pre>
month <- c("01","02","03","04","05","06","07","08","09","10","11","12")
span <-c(rep(month,2),month[1:7])
dir <- "http://dirk.eddelbuettel.com/cranberries"


url2013 <- file.path(dir,"2013",month)
==== Another case: Error in download.file() on Windows 7 ====
url2014 <- file.path(dir,"2014",month)
For some reason, IE 8 cannot interpret https://ftp.ncbi.nlm.nih.gov though it understands ftp://ftp.ncbi.nlm.nih.gov.
url2015 <- file.path(dir,"2015",month[1:7])
url <- c(url2013,url2014,url2015)


# Read each directory and count the new packages
This is tested using R 3.4.3.
new_p <- vector()
{{Pre}}
for(i in url){
> download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
   raw.data <- readLines(i)
trying URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
  new_p[i] <- length(grep("New package",raw.data,value=TRUE))
Error in download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz",  :
}
   cannot open URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
In addition: Warning message:
In download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", :
  InternetOpenUrl failed: 'An error occurred in the secure channel support'


# Plot
> download.file("ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
time <- seq(as.Date("2013-01-01"), as.Date("2015-07-01"), by="months")
trying URL 'ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
new_pkgs <- data.frame(time,new_p)
downloaded 9.1 MB
 
ggplot(new_pkgs, aes(time,y=new_p)) +
  geom_line() + xlab("") + ylab("Number of new packages") +
  geom_smooth(method='lm') + ggtitle("New R packages as reported by CRANberries")
</pre>
</pre>


== Top new packages in 2015 ==
== ERROR: failed to lock directory ==
* [http://opiateforthemass.es/articles/R-packages-in-2015/ 2015 R packages roundup] by CHRISTOPH SAFFERLING
Follow the suggestion to remove the LOCK file. See the [https://stackoverflow.com/questions/23483303/error-failed-to-lock-directory-c-program-files-r-r-3-0-2-library-for-modifyi post].
* [http://gforge.se/2016/01/r-trends-in-2015/ R trends in 2015] by MAX GORDON


== keep.source.pkgs option ==
It could happened in calling install.packages(), biocLite() or devtools::install_github(), and so on.
[https://blog.r-hub.io/2020/09/03/keep.source/ State of R packages in your library]. The original code formatting and commenting being removed by default, unless one changes some options for installing packages.


* options(keep.source.pkgs = TRUE)
== Error in unloadNamespace(package) ==
* install.packages("rhub", INSTALL_opts = "--with-keep.source", type = "source")
<pre>
* R CMD install --with-keep.source
> d3heatmap(mtcars, scale = "column", colors = "Blues")
Error: 'col_numeric' is not an exported object from 'namespace:scales'
> packageVersion("scales")
[1] ‘0.2.5’
> library(scales)
Error in unloadNamespace(package) :
  namespace ‘scales’ is imported by ‘ggplot2’ so cannot be unloaded
In addition: Warning message:
package ‘scales’ was built under R version 3.2.1
Error in library(scales) :
  Package ‘scales’ version 0.2.4 cannot be unloaded
> search()
[1] ".GlobalEnv"            "package:d3heatmap"      "package:ggplot2"     
[4] "package:microbenchmark" "package:COSMIC.67"      "package:BiocInstaller"  
[7] "package:stats"          "package:graphics"      "package:grDevices"   
[10] "package:utils"          "package:datasets"      "package:methods"     
[13] "Autoloads"              "package:base"  
</pre>
If I open a new R session, the above error will not happen!


== Speeding up package installation ==
The problem occurred because the 'scales' package version required by the d3heatmap package/function is old. See [https://github.com/rstudio/d3heatmap/issues/16 this post]. And when I upgraded the 'scales' package, it was ''locked'' by the package was ''imported'' by the ''ggplot2'' package.
* http://blog.jumpingrivers.com/posts/2017/speed_package_installation/
* [http://dirk.eddelbuettel.com/blog/2017/11/27/#011_faster_package_installation_one (Much) Faster Package (Re-)Installation via Caching]
* [http://dirk.eddelbuettel.com/blog/2017/12/13/#013_faster_package_installation_two (Much) Faster Package (Re-)Installation via Caching, part 2]


== An efficient way to install and load R packages ==
== Unload a package ==
[https://www.statsandr.com/blog/an-efficient-way-to-install-and-load-r-packages/ An efficient way to install and load R packages]
Add '''unload = TRUE''' option to unload the namespace. See [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/detach detach()].
{{Pre}}
# Package names
packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate", "pwr", "psy", "car", "doBy", "imputeMissings", "RcmdrMisc", "questionr", "vcd", "multcomp", "KappaGUI", "rcompanion", "FactoMineR", "factoextra", "corrplot", "ltm", "goeveg", "corrplot", "FSA", "MASS", "scales", "nlme", "psych", "ordinal", "lmtest", "ggpubr", "dslabs", "stringr", "assist", "ggstatsplot", "forcats", "styler", "remedy", "snakecaser", "addinslist", "esquisse", "here", "summarytools", "magrittr", "tidyverse", "funModeling", "pander", "cluster", "abind")
 
# Install packages not yet installed
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}
 
# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
</pre>
Alternatively use the '''pacman''' package.
 
== library( , '''exclude, include.only''') ==
See [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/library ?library]
<pre>
<pre>
library(tidyverse)
require(splines)
library(MASS, exclude='select')
detach(package:splines, unload=TRUE)
library(thepackage, include.only="thefunction")
</pre>
</pre>


== package ‘XXX’ was installed by an R version with different internals ==
== crantastic ==
it needs to be reinstalled for use with this R version. The problem seems to be specific to R 3.5.1 in Ubuntu 16.04. I got this message when I try to install the '''keras''' and '''tidyverse''' packages. The 'XXX' package includes ''nlme'' for installing "tidyverse" and ''Matrix'' for installing "reticulate". I have already logged in as root. I need to manually install these packages again though it seems I did not see a version change for these packages.
https://crantastic.org/. A community site for R packages where you can search for, review and tag CRAN packages.


Same error [https://stackoverflow.com/q/50992032 Error: package was installed by an R version with different internals; it needs to be reinstalled for use with this R version].
https://github.com/hadley/crantastic


Today it also happened when I tried to install "pec" which broke when it was installing "Hmisc". The error message is "Error : package ‘rpart’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version". I am using R 3.5.2. rpart version is ‘4.1.13’. The solution is I install rpart again (under my account is enough) though rpart does not have a new version. Then I can install "Hmisc".
== New R packages as reported by [http://dirk.eddelbuettel.com/cranberries/ CRANberries] ==
http://blog.revolutionanalytics.com/2015/07/mranspackages-spotlight.html


= packrat (reproducible search): project specific package managment =
<pre>
* [https://cran.r-project.org/web/packages/packrat/ CRAN] & [https://rstudio.github.io/packrat/ Github]
#----------------------------
** [https://github.com/rstudio/packrat/issues?q=bioconductor Bioconductor] related issues
# SCRAPE CRANBERRIES FILES TO COUNT NEW PACKAGES AND PLOT
* Videos:
#
** https://www.rstudio.com/resources/webinars/managing-package-dependencies-in-r-with-packrat/
library(ggplot2)
** https://www.rstudio.com/resources/webinars/rstudio-essentials-webinar-series-managing-part-3/
# Build a vextor of the directories of interest
* Packrat will not only store all packages, but also all project files.
year <- c("2013","2014","2015")
* Packrat is integrated in RStudio’s user interface. It allows you to share projects along co-workers easily. See [https://rstudio.github.io/packrat/rstudio.html Using Packrat with RStudio].
month <- c("01","02","03","04","05","06","07","08","09","10","11","12")
* [https://rstudio.github.io/packrat/limitations.html limitations].
span <-c(rep(month,2),month[1:7])
** [https://cran.r-project.org/web/packages/XML/index.html XML] package needs to install some OS library ''libxml2''. So it is not just R package issue.
dir <- "http://dirk.eddelbuettel.com/cranberries"
** [[Install_R#Ubuntu.2FDebian_goodies|Ubuntu goodies]]
* [https://stackoverflow.com/questions/36187543/using-r-with-git-and-packrat Git and packrat]. The ''packrat/src'' directory can be very large. ''If you don't want them available in your git-repo, you simply add packrat/src/ to the .gitignore. But, this will mean that anyone accessing the git-repo will not have access to the package source code, and the files will be downloaded from CRAN, or from wherever the source line dictates within the packrat.lock file.''
** [https://www.joelnitta.com/post/packrat/ Using packrat with git for (better) version control] Jun 2018
* A scenario that we need packrat: suppose we are developing a package in the current R-3.5.X. Our package requires the 'doRNG' package. That package depends the 'rngtools' package. A few months later a new R (3.6.0) was released and a new release (1.3.1.1) of 'rngtools' also requires R-3.6.0. So if we want to install 'doRNG' in R-3.5.x, it will fail with an error: ''dependency 'rngtools' is not available for package 'doRNG' ''.  


== Create a snapshot ==
url2013 <- file.path(dir,"2013",month)
* Do we really need to call packrat::snapshot()? The [https://rstudio.github.io/packrat/walkthrough.html walk through] page says it is not needed but the lock file is not updated from my testing.
url2014 <- file.path(dir,"2014",month)
* I got an error when it is trying to fetch the source code from bioconductor and local repositories: packrat is trying to fetch the source from CRAN in these two packages.
url2015 <- file.path(dir,"2015",month[1:7])
** On normal case, the packrat/packrat.lock file contains two entries in 'Repos' field (line 4).
url <- c(url2013,url2014,url2015)
** The cause of the error is I ran snapshot() after I quitted R and entered again. So the solution is to add bioc and local repositories to options(repos).
** So what is important of running snapshot()?
** Check out the [https://groups.google.com/forum/#!forum/packrat-discuss forum].
<syntaxhighlight lang='rsplus'>
> dir.create("~/projects/babynames", recu=T)
> packrat::init("~/projects/babynames")
Initializing packrat project in directory:
- "~/projects/babynames"


Adding these packages to packrat:
# Read each directory and count the new packages
            _
new_p <- vector()
    packrat   0.4.9-3
for(i in url){
   raw.data <- readLines(i)
  new_p[i] <- length(grep("New package",raw.data,value=TRUE))
}


Fetching sources for packrat (0.4.9-3) ... OK (CRAN current)
# Plot
Snapshot written to '/home/brb/projects/babynames/packrat/packrat.lock'
time <- seq(as.Date("2013-01-01"), as.Date("2015-07-01"), by="months")
Installing packrat (0.4.9-3) ...
new_pkgs <- data.frame(time,new_p)
OK (built source)
Initialization complete!
Unloading packages in user library:
- packrat
Packrat mode on. Using library in directory:
- "~/projects/babynames/packrat/lib"


> install.packages("reshape2")
ggplot(new_pkgs, aes(time,y=new_p)) +
> packrat::snapshot()
  geom_line() + xlab("") + ylab("Number of new packages") +
 
  geom_smooth(method='lm') + ggtitle("New R packages as reported by CRANberries")
> system("tree -L 2 ~/projects/babynames/packrat/")
</pre>
/home/brb/projects/babynames/packrat/
├── init.R
├── lib
│   └── x86_64-pc-linux-gnu
├── lib-ext
│   └── x86_64-pc-linux-gnu
├── lib-R           # base packages
│   └── x86_64-pc-linux-gnu
├── packrat.lock
├── packrat.opts
└── src
    ├── bitops
    ├── glue
    ├── magrittr
    ├── packrat
    ├── plyr
    ├── Rcpp
    ├── reshape2
    ├── stringi
    └── stringr
</syntaxhighlight>


== Restoring snapshots ==
== R packages being removed ==
* https://dirk.eddelbuettel.com/cranberries/cran/removed/
* https://web.archive.org/ to find out the package website before it's removed


Suppose a packrat project was created on Ubuntu 16.04 and we now want to repeat the analysis on Ubuntu 18.04. We first copy the whole project directory ('babynames') to Ubuntu 18.04. Then we should delete the library subdirectory ('packrat/lib') which contains binary files (*.so) that do not work on the new OS. After we delete the library subdirectory, start R from the project directory. Now if we run '''packrat::restore()''' command, it will re-install all missing libraries. Bingo! NOTE: Maybe I should use '''packrat::bundle()''' instead of manually copy the whole project folder.
== Top new packages in 2015 ==
* [http://opiateforthemass.es/articles/R-packages-in-2015/ 2015 R packages roundup] by CHRISTOPH SAFFERLING
* [http://gforge.se/2016/01/r-trends-in-2015/ R trends in 2015] by MAX GORDON


Note: some OS level libraries (e.g. libXXX-dev) need to be installed manually beforehand in order for the magic to work.
== keep.source.pkgs option ==
<syntaxhighlight lang='rsplus'>
[https://blog.r-hub.io/2020/09/03/keep.source/ State of R packages in your library]. The original code formatting and commenting being removed by default, unless one changes some options for installing packages.
$ rm -rf ~/projects/babynames/packrat/lib
$ cd ~/projects/babynames/
$ R
>
> packrat::status()
> remove.packages("plyr")
> packrat::status()
> packrat::restore()
</syntaxhighlight>


== Workflow ==
* options(keep.source.pkgs = TRUE)
<pre>
* install.packages("rhub", INSTALL_opts = "--with-keep.source", type = "source")
setwd("ProjectDir")
* R CMD install --with-keep.source


packrat::init()
[https://bugs.r-project.org/show_bug.cgi?id=18236 Package installation speed for packages installed with ‘keep.source’ has been improved.] 2021-12-2.


packrat::on() # packrat::search_path()
== Speeding up package installation ==
* http://blog.jumpingrivers.com/posts/2017/speed_package_installation/
* [http://dirk.eddelbuettel.com/blog/2017/11/27/#011_faster_package_installation_one (Much) Faster Package (Re-)Installation via Caching]
* [http://dirk.eddelbuettel.com/blog/2017/12/13/#013_faster_package_installation_two (Much) Faster Package (Re-)Installation via Caching, part 2]


install.packages()
== An efficient way to install and load R packages ==
# For personal packages stored locally
[https://www.statsandr.com/blog/an-efficient-way-to-install-and-load-r-packages/ An efficient way to install and load R packages]
packrat::set_opts(local.repos = "~/git/R")
{{Pre}}
packrat::install_local("digest") # dir name of the package
# Package names
library(YourPackageName)
packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate", "pwr", "psy", "car", "doBy", "imputeMissings", "RcmdrMisc", "questionr", "vcd", "multcomp", "KappaGUI", "rcompanion", "FactoMineR", "factoextra", "corrplot", "ltm", "goeveg", "corrplot", "FSA", "MASS", "scales", "nlme", "psych", "ordinal", "lmtest", "ggpubr", "dslabs", "stringr", "assist", "ggstatsplot", "forcats", "styler", "remedy", "snakecaser", "addinslist", "esquisse", "here", "summarytools", "magrittr", "tidyverse", "funModeling", "pander", "cluster", "abind")
  # double check all dependent ones have been installed


packrat::snapshot()
# Install packages not yet installed
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}


packrat::bundle()
# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
</pre>
</pre>
A bundle file (*.tar.gz) will be created under ProjectDir/packrat/src directory. '''Note this tar.gz file includes the whole project folder. '''
Alternatively use the '''pacman''' package.


To unbundle the project in a new R environment/directory:
== library( , '''exclude, include.only''') ==
See [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/library ?library]
<pre>
<pre>
setwd("NewDirectory") # optional
library(tidyverse)
packrat::unbundle(FullPathofBundleTarBall, ".")
library(MASS, exclude='select')
  # this will create 'ProjectDir'
library(thepackage, include.only="thefunction")
  # CPU is more important than disk speed
  # At the end, it will show the project has been unbundled and restored at ...
 
setwd("ProjectDir")
packrat::packrat_mode()  # on
.libPaths()  # verify
library()  # Expect to see packages in our bundle
# packrat::on()
</pre>
</pre>
Example 1: The above method works for packages from Bioconductor; e.g. S4Vectors which depends on BiocGenerics & BiocVersion only. However, Bioconductor project des not have a snapshot repository like MRAN. So it is difficult to reproduce the environment for an earlier release of Bioconductor.


Example 2: bundle our in-house R package for future reproducibility.
== package ‘XXX’ was installed by an R version with different internals ==
it needs to be reinstalled for use with this R version. The problem seems to be specific to R 3.5.1 in Ubuntu 16.04. I got this message when I try to install the '''keras''' and '''tidyverse''' packages. The 'XXX' package includes ''nlme'' for installing "tidyverse" and ''Matrix'' for installing "reticulate". I have already logged in as root. I need to manually install these packages again though it seems I did not see a version change for these packages.


== Set Up a Custom CRAN-like Repository ==
Same error [https://stackoverflow.com/q/50992032 Error: package was installed by an R version with different internals; it needs to be reinstalled for use with this R version].


See https://rstudio.github.io/packrat/custom-repos.html. Note the personal repository name ('sushi' in this example) used in "Repository" field of the personal package will be used in <packrat/packrat.lock> file. So as long as we work on the same computer, it is easy to restore a packrat project containing packages coming from personal repository.
Today it also happened when I tried to install "pec" which broke when it was installing "Hmisc". The error message is "Error : package ‘rpart’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version". I am using R 3.5.2. rpart version is ‘4.1.13’. The solution is I install rpart again (under my account is enough) though rpart does not have a new version. Then I can install "Hmisc".


'''[https://rstudio.github.io/packrat/commands.html Common functions]''':
= packrat and renv =
* packrat::init()
See [[Reproducible#renv:_successor_to_the_packrat_package| Reproducible &rarr; packrat/renv]]
* packrat::snapshot(), packrat::restore()
* packrat::clean()
* packrat::status()
* packrat::install_local() # http://rstudio.github.io/packrat/limitations.html
* packrat::bundle() # see @28:44 of the [https://www.rstudio.com/resources/webinars/managing-package-dependencies-in-r-with-packrat/ video], packrat::unbundle() # see @29:17 of the same video. This will rebuild all packages
* packrat::on(), packrat::off()
* packrat::get_opts()
* packrat::set_opts() # http://rstudio.github.io/packrat/limitations.html
* packrat::opts$local.repos("~/local-cran")
* packrat::opts$external.packages(c("devtools")) # break the isolation
* packrat::extlib()
* packrat::with_extlib()
* packrat::project_dir(), .libPaths()


== Warning ==
= R package dependencies =
* If we download and modify some function definition from a package in CRAN without changing DESCRIPTION file or the package name, the snapshot created using packrat::snapshot() will contain the package source from CRAN instead of local repository. This is because (I guess) the DESCRIPTION file contains a field 'Repository' with the value 'CRAN'.


== Docker ==
== Depends, Imports, Suggests, Enhances, LinkingTo ==
[https://www.joelnitta.com/post/docker-and-packrat/ Docker and Packrat].
See [https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-Dependencies Writing R Extensions] and [[#install.packages.28.29|install.packages()]].
* This is a minimal example that installs a single package each from CRAN, bioconductor, and github to a Docker image using packrat.
* All operations are done in the container. So the host OS does not need to have R installed.
* The R script will install packrat in the container. It will also initialize packrat in the working directory and install R packages there. But in the [https://www.rdocumentation.org/packages/packrat/versions/0.5.0/topics/snapshot packrat::snapshot()] it chooses '''snapshot.sources = FALSE'''. The goal is to generate packrat.lock file.
* The first part of generating packrat.lock is not quite right since the file was generated in the container only. We should use '''-v''' in the ''docker run'' command. The github repository at https://github.com/joelnitta/docker-packrat-example has fixed the problem.
{{Pre}}
$ git clone https://github.com/joelnitta/docker-packrat-example.git
$ cd docker-packrat-example


# Step 1: create the 'packrat.lock' file
* Depends: list of package names which this package depends on. Those packages will be attached (so it is better to use ''Imports'' instead of ''Depends'' as much as you can) before the current package when library or require is called. The ‘Depends’ field can also specify a dependence on a certain version of R.
$ nano install_packages.R     # note: nano is not available in the rstudio container
* Imports: lists packages whose '''namespaces''' are imported from (as specified in the NAMESPACE file) but which do not need to be attached.
                              # need to install additional OS level packages like libcurl
* Suggests: lists packages that are not necessarily needed. This includes packages used only in examples, tests or vignettes, and packages loaded in the body of functions.
                              # in rocker/rstudio. Probably rocker/tidyverse is better than rstudio
* Enhances: lists packages “enhanced” by the package at hand, e.g., by providing methods for classes from these packages, or ways to handle objects from these packages.
                              #
* LinkingTo: A package that wishes to make use of '''header''' files in other packages needs to declare them as a comma-separated list in the field ‘LinkingTo’ in the DESCRIPTION file.
$ docker run -it -e DISABLE_AUTH=true -v $(pwd):/home/rstudio/project rocker/tidyverse:3.6.0 bash
** An example is [https://www.bioconductor.org/packages/release/bioc/html/SingleR.html SingleR] that links to the ''beachmat'' package in its [https://github.com/LTLA/SingleR/blob/master/src/recompute_scores.cpp cpp source code].
# Inside the container now
$ cd home/rstudio/project
$ time Rscript install_packages.R  # generate 'packrat/packrat.lock'
$ exit                        # It took 43 minutes.
                              # Question: is there an easier way to generate packrat.lock without
                              # wasting time to install lots of packages?
# Step 2: build the image
# Open another terminal/tab
$ nano Dockerfile            # change rocker image and R version. Make sure these two are the same as
                              # we have used when we created the 'packrat.lock' file
$ time docker build . -t mycontainer # It took 45 minutes.
$ docker run -it mycontainer R


# Step 3: check the packages defined in 'install_packages.R' are installed
== Package related functions from package 'utils' ==
packageVersion("minimal")
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/available.packages.html available.packages()]; see packageStatus().
packageVersion("biospear")
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/download.packages.html download.packages()]
</pre>
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/packageStatus.html packageStatus(), update(), upgrade()]. packageStatus() will return a list with two components:
Questions:
# inst - a data frame with columns as the matrix returned by '''installed.packages''' plus "Status", a factor with levels c("ok", "upgrade"). Note: the manual does not mention "unavailable" case (but I do get it) in R 3.2.0?
* After running the statement packrat::init(), it will leave a footprint of a hidden file '''.Rprofile''' in the current directory. PS: The [https://rstudio.github.io/packrat/walkthrough.html purpose of .Rprofile file] is to direct R to use the private package library (when it is started from the project directory). <syntaxhighlight lang='rsplus'>
# avail - a data frame with columns as the matrix returned by '''available.packages''' plus "Status", a factor with levels c("installed", "not installed", "unavailable"). Note: I don't get the "unavailable" case in R 3.2.0?
#### -- Packrat Autoloader (version 0.5.0) -- ####
{{Pre}}
source("packrat/init.R")
> x <- packageStatus()
#### -- End Packrat Autoloader -- ####
> names(x)
</syntaxhighlight>
[1] "inst"  "avail"
: If the 'packrat' directory was accidentally deleted, next time when you launch R it will show an error message because it cannot find the file.  
> dim(x[['inst']])
* The ownership of the 'packrat' directory will be root now. See this [https://rviews.rstudio.com/2018/01/18/package-management-for-reproducible-r-code/ Package Management for Reproducible R Code].
[1] 225  17
* This sophisticated approach does not save the package source code. If a package has been updated and the version we used has been moved to archive in CRAN, what will happen when we try to restore it? So it is probably better to use '''snapshot.sources = TRUE''' and run packrat::bundle().
> x[['inst']][1:3, ]
 
              Package                            LibPath Version Priority              Depends Imports
== renv: successor to the packrat package ==
acepack      acepack C:/Program Files/R/R-3.1.2/library 1.3-3.3    <NA>                  <NA>    <NA>
* https://rstudio.github.io/renv/index.html
adabag        adabag C:/Program Files/R/R-3.1.2/library    4.0    <NA> rpart, mlbench, caret    <NA>
* [https://blog.rstudio.com/2019/11/06/renv-project-environments-for-r/ release] 2019-11-6
affxparser affxparser C:/Program Files/R/R-3.1.2/library  1.38.0    <NA>          R (>= 2.6.0)   <NA>
* [https://rstudio.github.io/renv/articles/renv.html Introduction to renv] 2021-01-09
          LinkingTo                                                        Suggests Enhances
* The [https://rstudio.github.io/renv/reference/migrate.html renv::migrate()] function makes it possible to migrate projects from '''Packrat''' to '''renv'''.
acepack        <NA>                                                            <NA>    <NA>
* [https://blog.rstudio.com/2020/08/20/why-package-environment-management-is-critical-for-serious-data-science/ Why Package & Environment Management is Critical for Serious Data Science] and a [https://garciamikep.github.io/useR-webinar/#41 workflow].
adabag          <NA>                                                            <NA>    <NA>
* [https://medium.com/analytics-vidhya/deploying-an-r-shiny-app-on-heroku-free-tier-b31003858b68 Deploying an R Shiny app on Heroku free tier]
affxparser      <NA> R.oo (>= 1.18.0), R.utils (>= 1.32.4),\nAffymetrixDataTestFiles    <NA>
* [https://github.com/rstudio/renv/issues?q=bioconductor Bioconductor] related questions
                      License License_is_FOSS License_restricts_use OS_type MD5sum NeedsCompilation Built
* [https://daryavanichkina.com/posts/2021-07-28-renvhpc Installing packages on a PBS-Pro HPC cluster using renv]
acepack    MIT + file LICENSE            <NA>                  <NA>    <NA>  <NA>             yes 3.1.2
 
adabag            GPL (>= 2)           <NA>                  <NA>    <NA>  <NA>              no 3.1.2
Compare to packrat:
affxparser        LGPL (>= 2)           <NA>                  <NA>    <NA>  <NA>            <NA> 3.1.1
* Many packages are difficult to build from sources. Your system will need to have a compatible compiler toolchain available. In some cases, R packages may depend on C/C++ features that aren't available in an older system toolchain, especially in some older Linux enterprise environments.
                Status
* '''renv no longer attempts to explicitly download and track R package source tarballs within your project.''' For packages from local sources, refer [https://rstudio.github.io/renv/articles/local-sources.html this article].
acepack            ok
* renv has its discovery machinery to analyze your R code to determine which R packages will be included in the lock file. We can however instead prefer to capture ''all'' packages installed into your project library by using '''renv::settings$snapshot.type("all") '''
adabag             ok
 
affxparser unavailable
renv package does not have bundle() nor unbundle() function.
> dim(x[['avail']])
 
[1] 6538  18
<pre>
> x[['avail']][1:3, ]
# mkdir renvdeseq2
                Package Version Priority                        Depends        Imports LinkingTo
setwd("renvdeseq2")
A3                  A3   0.9.2    <NA> R (>= 2.15.0), xtable, pbapply          <NA>      <NA>
renv::init() # attempts to copy and reuse packages
ABCExtremes ABCExtremes    1.0    <NA>      SpatialExtremes, combinat          <NA>      <NA>
            # already installed in your R libraries
ABCanalysis ABCanalysis  1.0.1    <NA>                    R (>= 2.10) Hmisc, plotrix      <NA>
            # We'll be asked to restart the R session if we
                      Suggests Enhances    License License_is_FOSS License_restricts_use OS_type Archs
             # are not doing this in RStudio.
A3          randomForest, e1071    <NA> GPL (>= 2)           <NA>                  <NA>    <NA> <NA>
renv::install("BiocManager")
ABCExtremes                <NA>    <NA>      GPL-2            <NA>                  <NA>    <NA>  <NA>
options(repos = BiocManager::repositories())
ABCanalysis                <NA>    <NA>      GPL-3            <NA>                  <NA>    <NA>  <NA>
renv::install("DESeq2") # OR BiocManager::install("DESeq2")
            MD5sum NeedsCompilation File                                      Repository        Status
renv::snapshot() # create renv.lock
A3            <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
  # it seems the lock file "renvdeseq2/renv.lock" does not
ABCExtremes  <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
   # save any package info I just installed from Bioconductor
ABCanalysis  <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
  # except the renv package.
</pre>
  # Read https://rstudio.github.io/renv/articles/faq.html
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/packageDescription.html packageVersion(), packageDescription()]
renv::dependencies()
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/install.packages.html install.packages()], [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/remove.packages.html remove.packages()].
 
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/installed.packages.html installed.packages()]; see packageStatus().
renv::settings$snapshot.type("all")  
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/update.packages.html update.packages(), old.packages(), new.packages()]
renv::snapshot()  # now all packages are saved in
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/setRepositories.html setRepositories()]
                  # "rendeseq2/renv.lock"
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/contrib.url.html contrib.url()]
</pre>
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/chooseCRANmirror.html chooseCRANmirror()], [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/chooseBioCmirror.html chooseBioCmirror()]
 
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/globalVariables.html suppressForeignCheck()]
Pass renv.lock to other people and/or clone the project repository
<pre>
# Make sure the 'renv' package has been installed on the remote computer
install.packages("renv")
renv::init()  # install the packages declared in renv.lock
</pre>
 
 
Use '''[https://rstudio.github.io/renv/reference/migrate.html renv::migrate()]''' to port a Packrat project to renv.
 
=== renv::dependencies() ===
[https://rstudio.github.io/renv/reference/dependencies.html ?dependencies]. Find R packages used within a project. dependencies() will crawl files within your project, looking for R files and the packages used within those R files.  
<pre>
df <- renv::dependencies("Some_Dir")
</pre>
It also search Rmd files from my testing.
 
=== Cache ===
[https://rstudio.github.io/renv/reference/paths.html ?path] (lined from [https://rstudio.github.io/renv/articles/local-sources.html Installing from Local Sources])
 
On my Linux system, I see the source packages (*.tar.gz) are stored at
* ~/.local/share/renv/source/bioconductor/ # Store bioconductor packages
* ~/.local/share/renv/source/repository/   # Store CRAN packages
 
and the binary packages are stored at
* ~/.local/share/renv/cache/ (~/.local/share/renv/cache/v5/R-4.0/x86_64-pc-linux-gnu/)
 
Note that once I have used renv::init() to restore a project, the related R packages (binary and/or source) will be cached. So next time when we do renv::init(), local R packages can be found.
 
So how do we manage the packages in cache? For example if we are developing an R package and we made a change but did not change the version number. See [https://rstudio.github.io/renv/reference/index.html Reference].
* renv::purge("MyPackage") # remove binary and source
 
{{Pre}}
> root <- renv::paths$root()
 
Welcome to renv!


It looks like this is your first time using renv. This is a one-time message,
=== download.packages() for source package ===
briefly describing some of renv's functionality.
Consider the [https://bioinformatics.mdanderson.org/estimate/rpackage.html esimate] package hosted on [https://r-forge.r-project.org/projects/estimate/ r-forge].
<pre>
download.packages("estimate", destdir = "~/Downloads",  
  repos = "https://R-Forge.R-project.org")
</pre>


renv maintains a local cache of data on the filesystem, located at:
== tools package ==
 
* https://www.rdocumentation.org/packages/tools/versions/3.6.1
  - '~/.local/share/renv'
* CRAN_package_db() from [https://www.rdocumentation.org/packages/tools/versions/3.6.1/topics/CRANtools ?CRANtools]. Especially, it gives the Description and Maintainer information not provided by utils::available.packages()
 
* [https://www.rdocumentation.org/packages/tools/versions/3.6.1/topics/dependsOnPkgs dependsOnPkgs()]
This path can be customized: please see the documentation in `?renv::paths`.
:<syntaxhighlight lang='rsplus'>
db <- tools::CRAN_package_db()
nRcpp <- length(tools::dependsOnPkgs("Rcpp", recursive=FALSE, installed=db) )
nCompiled <- table(db[, "NeedsCompilation"])[["yes"]]
propRcpp <- nRcpp / nCompiled * 100
</syntaxhighlight>
* package.dependencies(), pkgDepends(), etc are deprecated now, mostly in favor of [https://www.rdocumentation.org/packages/tools/versions/3.6.2/topics/package_dependencies package_dependencies()] which is both more flexible and efficient. See [https://cran.rstudio.com/doc/manuals/r-release/NEWS.html R 3.3.0 News]. For example, tools::package_dependencies(c("remotes", "devtools"), recursive=TRUE) shows ''remotes'' has only a few dependencies while ''devtools'' has a lot.


renv will also write to files within the active project folder, including:
== crandep package ==
https://cran.r-project.org/web/packages/crandep/index.html. Useful to find reverse dependencies. [https://rdrr.io/cran/crandep/man/get_dep.html ?get_dep]. Consider the [https://cran.r-project.org/web/packages/abc/index.html abc] package:
<pre>
get_dep("abc", "depends") # abc depends on these packages
                          # note my computer does not have 'abc' installed
#  from      to    type reverse
# 1  abc abc.data depends  FALSE
# 2  abc    nnet depends  FALSE
# 3  abc quantreg depends  FALSE
# 4  abc    MASS depends  FALSE
# 5  abc  locfit depends  FALSE


  - A folder 'renv' in the project directory, and
get_dep("abc", "reverse_depends")
  - A lockfile called 'renv.lock' in the project directory.
#    from      to    type reverse
# 1  abc abctools depends    TRUE
# 2  abc  EasyABC depends    TRUE


In particular, projects using renv will normally use a private, per-project
x <- get_dep("RcppEigen", c("reverse linking to"))
R library, in which new packages will be installed. This project library is
dim(x)
isolated from other R libraries on your system.
# [1] 331  4
head(x, 3)
#        from     to      type reverse
# 1 RcppEigen  abess linking to    TRUE
# 2 RcppEigen    acrt linking to    TRUE
# 3 RcppEigen ADMMnet linking to    TRUE
</pre>


In addition, renv will update files within your project directory, including:
== How does this package depend on this other package ==
[https://www.r-bloggers.com/2023/08/three-four-r-functions-i-enjoyed-this-week/ How does this package depend on this other package?], [https://pak.r-lib.org/reference/pkg_deps_explain.html pak::pkg_deps_explain()]


  - .gitignore
== pkgndep ==
  - .Rbuildignore
* [https://cran.r-project.org/web/packages/pkgndep/index.html CRAN] & the paper [https://academic.oup.com/bioinformatics/advance-article/doi/10.1093/bioinformatics/btac449/6633919?login=false pkgndep: a tool for analyzing dependency heaviness of R packages]
  - .Rprofile
* [https://jokergoo.github.io/2023/11/16/package-dependencies-in-your-session/ Package dependencies in your session]. DESeq2 was used.


Please read the introduction vignette with `vignette("renv")` for more information.
== remotes ==
You can browse the package documentation online at https://rstudio.github.io/renv/.
[https://www.rdocumentation.org/packages/remotes/versions/2.2.0/topics/package_deps  remotes::local_package_deps(dependencies=TRUE)] will find and return all dependent packages based on the "DESCRIPTION" file. See an example [https://github.com/Bioconductor/OrchestratingSingleCellAnalysis here].
Do you want to proceed? [y/N]:  
</pre>


=== Local R packages ===
== Bioconductor's [http://www.bioconductor.org/packages/release/bioc/html/pkgDepTools.html pkgDepTools] package ==
* https://rstudio.github.io/renv/articles/local-sources.html
The is an example of querying the dependencies of the notorious 'lumi' package which often broke the installation script. I am using R 3.2.0 and Bioconductor 3.1.  
* Since local R packages (no matter it is source or binary) are not part of '''renv.lock''', the original location of these packages are not important when we first install these packages.
* When we try to restore local R packages, we can put these packages' source files into '''renv/local''' directory.


<pre>
The '''getInstallOrder''' function is useful to get a list of all (recursive) dependency packages.
# mkdir renvbiotrip
{{Pre}}
setwd("renvbiotrip")
source("http://bioconductor.org/biocLite.R")
renv::init() # we shall restart R according to the instruction
if (!require(pkgDepTools)) {
# * Initializing project ...
  biocLite("pkgDepTools", ask = FALSE)
# * Discovering package dependencies ... Done!
   library(pkgDepTools)
# * Copying packages into the cache ... Done!
}
# The following package(s) will be updated in the lockfile:
MkPlot <- FALSE
# CRAN ===============================
# - renv   [* -> 0.10.0]
# * Lockfile written to '/tmp/renvbiotrip/renv.lock'.
# * Project '/tmp/renvbiotrip' loaded. [renv 0.10.0]
# * renv activated -- please restart the R session.


renv::install("~/Downloads/MyPackage_0.1.1.tar.gz")
library(BiocInstaller)
# 1. The above command will take care of the dependence. Cool !
biocUrl <- biocinstallRepos()["BioCsoft"]
#    That is, we don't need to use the remotes package.
biocDeps <- makeDepGraph(biocUrl, type="source", dosize=FALSE) # pkgDepTools defines its makeDepGraph()
# 2. The output will show if packages are installed from
#    'linked cache' or from source
renv::settings$snapshot.type("all")  
renv::snapshot()
# It will give a message some package(s) were installed from an unknown source
# renv may be unable to restore these packages in the future.
</pre>
Since the dependence package versions change from time to time, if we compare the renv.lock file created yesterday it will likely be different from what we created today (package version and hash tag).


Now we are ready to test the restoration.
PKG <- "lumi"
<ul>
if (MkPlot) {
<li>
  if (!require(Biobase))  {
Pass renv.lock and MyPackage_0.1.1.tar.gz to other people (different instruction if we pass the project repository?). Suppose we have copied renv.lock to renvbiotrip/ directory on a new computer.
    biocLite("Biobase", ask = FALSE)
<pre>
    library(Biobase)
# mkdir renvbiotrip
  }
## Copy renv.lock to renvbiotrip/
  if (!require(Rgraphviz){
# mkdir renvbiotrip/renv/local
    biocLite("Rgraphviz", ask = FALSE)  
## Copy MyPackage_0.1.1.tar.gz (private packages) to renvbiotrip/renv/local
    library(Rgraphviz)
install.packages("renv")
  }
renv::restore() # install the packages declared in renv.lock
  categoryNodes <- c(PKG, names(acc(biocDeps, PKG)[[1]])) 
           
  categoryGraph <- subGraph(categoryNodes, biocDeps)  
# The output will show if packages are installed from
  nn <- makeNodeAttrs(categoryGraph, shape="ellipse")
# 'linked cache' or from source     
  plot(categoryGraph, nodeAttrs=nn)  # Complete but plot is too complicated & font is too small.
library(MyPackage) # verify  
}
MyPackage::foo()   # test   
</pre>
</li>
<li>We can test renv.lock in a Docker container from another directory to mimic the way of passing the file to other people. For example,  
<pre>
docker run --rm -it -v $(pwd):/home/docker -w /home/docker r-base:4.0.0
</pre>
</li>
<li>We can create a docker image based on the renv.lock and MyPackage.tar.gz files. See the '''renvbiotrip''' repository.</li>
</ul>


Note that
system.time(allDeps <- makeDepGraph(biocinstallRepos(), type="source",
* If we issue renv::restore() instead of renv::init() on the destination machine, the packages will be installed into the global environment.  
                          keep.builtin=TRUE, dosize=FALSE)) # takes a little while
* It seems '''renv::init()''' is equivalent to '''renv::activate()''' AND '''renv::restore()''' on the destination machine.
#    user  system elapsed
# 175.737  10.994 186.875
# Warning messages:
# 1: In .local(from, to, graph) : edges replaced: ‘SNPRelate|gdsfmt’
# 2: In .local(from, to, graph) :
#  edges replaced: ‘RCurl|methods’, ‘NA|bitops’


=== Docker ===
# When needed.only=TRUE, only those dependencies not currently installed are included in the list.
* https://environments.rstudio.com/docker.html
x1 <- sort(getInstallOrder(PKG, allDeps, needed.only=TRUE)$packages); x1
* [https://biolitika.si/pin-r-package-versions-using-docker-and-renv.html Pin package versions in your production Docker image]
[1] "affy"                              "affyio"                         
<ul>
[3] "annotate"                          "AnnotationDbi"                   
<li>[https://rstudio.github.io/renv/articles/docker.html Using renv with Docker]. Note that there are two ways for the Docker approach. One way is to include package installation in the Docker file which embeds the packages into the image. A second approach is to add appropriate R packages when the container is run.
[5] "base64"                            "beanplot"                       
  <ol>
[7] "Biobase"                          "BiocParallel"                   
  <li>Creating Docker Images with renv (see [https://environments.rstudio.com/docker.html here] for 3 example Registries: Rocker Project/R-Hub/RStudio). Make sure <renv.lock> file and the local R package <MyPackage_0.1.0.tar.gz> are in the current directory.
[9] "biomaRt"                          "Biostrings"                     
    <pre>
[11] "bitops"                            "bumphunter"                     
FROM r-base:4.0.0
[13] "colorspace"                        "DBI"                             
RUN R -e 'install.packages("renv")'
[15] "dichromat"                        "digest"                         
COPY renv.lock /home/docker
[17] "doRNG"                            "FDb.InfiniumMethylation.hg19"   
RUN mkdir -p /home/docker/renv/local
[19] "foreach"                           "futile.logger"                   
COPY MyPackage_0.1.0.tar.gz /home/renv/local
[21] "futile.options"                    "genefilter"                     
WORKDIR /home/docker
[23] "GenomeInfoDb"                      "GenomicAlignments"               
RUN R -e 'renv::restore()'
[25] "GenomicFeatures"                  "GenomicRanges"                   
CMD ["R"]
[27] "GEOquery"                          "ggplot2"                        
</pre>
[29] "gtable"                            "illuminaio"                     
  </li>
[31] "IRanges"                          "iterators"                       
  <li>Running Docker Containers with renv
[33] "labeling"                          "lambda.r"                       
    <pre>
[35] "limma"                            "locfit"                         
docker build -t renvMyPackage .
[37] "lumi"                              "magrittr"                       
docker run --rm -it renvMyPackage # OR
[39] "matrixStats"                      "mclust"                         
docker run --rm -it -v $(pwd):/home/docker renvbiotrip
[41] "methylumi"                         "minfi"                          
</pre>
[43] "multtest"                         "munsell"                        
Question: how to update a package within a container? 1. start the container with root and update packages in the container 2. system("su docker") to switch to the user 'docker'. 3. when we run system("su docker"), it will exit R and go to the shell. Run "whoami" to double check the current user and type "R" to enter R again.
[45] "nleqslv"                          "nor1mix"                         
 
[47] "org.Hs.eg.db"                      "pkgmaker"                       
Another simple but inferior way to test the docker method is the following: assuming <renv.lock> is saved in the ProjectDir directory and the ProjectDir directory does not have ''renv'' nor ''.Rprofile''. The big drawback of this approach is the created renv directory and <.Rprofile> belongs to the user ''root''.
[49] "plyr"                              "preprocessCore"                 
    <pre>
[51] "proto"                            "quadprog"                       
docker run --rm -it -v ProjectDir:/home r-base:4.0.0
[53] "RColorBrewer"                      "Rcpp"                            
install.packages("renv")
[55] "RCurl"                            "registry"                        
setwd("/home")
[57] "reshape"                          "reshape2"                       
renv::init()
[59] "rngtools"                          "Rsamtools"                       
</pre> 
[61] "RSQLite"                          "rtracklayer"                     
  </li>
[63] "S4Vectors"                        "scales"                         
  </ol>
[65] "siggenes"                          "snow"                           
</li>
[67] "stringi"                          "stringr"                         
</ul>
[69] "TxDb.Hsapiens.UCSC.hg19.knownGene" "XML"                             
* [https://www.r-bloggers.com/2021/08/setting-up-a-transparent-reproducible-r-environment-with-docker-renv/ Setting up a transparent reproducible R environment with Docker + renv]
[71] "xtable"                            "XVector"                         
 
[73] "zlibbioc"                       
== Github actions ==
[https://orchid00.github.io/actions_sandbox/testing-with-renev.html Chapter 5 Testing with a reproducible environment]


= R package dependencies =
# When needed.only=FALSE the complete list of dependencies is given regardless of the set of currently installed packages.
x2 <- sort(getInstallOrder(PKG, allDeps, needed.only=FALSE)$packages); x2
[1] "affy"                              "affyio"                            "annotate"                       
[4] "AnnotationDbi"                    "base64"                            "beanplot"                       
[7] "Biobase"                          "BiocGenerics"                      "BiocInstaller"                   
[10] "BiocParallel"                      "biomaRt"                          "Biostrings"                     
[13] "bitops"                            "bumphunter"                        "codetools"                       
[16] "colorspace"                        "DBI"                              "dichromat"                       
[19] "digest"                            "doRNG"                            "FDb.InfiniumMethylation.hg19"   
[22] "foreach"                          "futile.logger"                    "futile.options"                 
[25] "genefilter"                        "GenomeInfoDb"                      "GenomicAlignments"               
[28] "GenomicFeatures"                  "GenomicRanges"                    "GEOquery"                       
[31] "ggplot2"                          "graphics"                          "grDevices"                       
[34] "grid"                              "gtable"                            "illuminaio"                     
[37] "IRanges"                          "iterators"                        "KernSmooth"                     
[40] "labeling"                          "lambda.r"                          "lattice"                         
[43] "limma"                            "locfit"                            "lumi"                           
[46] "magrittr"                          "MASS"                              "Matrix"                         
[49] "matrixStats"                      "mclust"                            "methods"                         
[52] "methylumi"                        "mgcv"                              "minfi"                           
[55] "multtest"                          "munsell"                          "nleqslv"                         
[58] "nlme"                              "nor1mix"                          "org.Hs.eg.db"                   
[61] "parallel"                          "pkgmaker"                          "plyr"                           
[64] "preprocessCore"                    "proto"                            "quadprog"                       
[67] "RColorBrewer"                      "Rcpp"                              "RCurl"                           
[70] "registry"                          "reshape"                          "reshape2"                       
[73] "rngtools"                          "Rsamtools"                        "RSQLite"                         
[76] "rtracklayer"                      "S4Vectors"                        "scales"                         
[79] "siggenes"                          "snow"                              "splines"                         
[82] "stats"                            "stats4"                            "stringi"                         
[85] "stringr"                          "survival"                          "tools"                           
[88] "TxDb.Hsapiens.UCSC.hg19.knownGene" "utils"                            "XML"                             
[91] "xtable"                            "XVector"                          "zlibbioc"


== Depends, Imports, Suggests, Enhances, LinkingTo ==
> sort(setdiff(x2, x1)) # Not all R's base packages are included; e.g. 'base', 'boot', ...
See [https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-Dependencies Writing R Extensions] and [[#install.packages.28.29|install.packages()]].
[1] "BiocGenerics"  "BiocInstaller" "codetools"    "graphics"      "grDevices"   
[6] "grid"          "KernSmooth"    "lattice"      "MASS"          "Matrix"     
[11] "methods"      "mgcv"          "nlme"          "parallel"      "splines"     
[16] "stats"        "stats4"        "survival"      "tools"        "utils" 
</pre>
[[File:Lumi rgraphviz.svg|200px]]
 
== Bioconductor [https://bioconductor.org/packages/devel/bioc/html/BiocPkgTools.html BiocPkgTools] ==
Collection of simple tools for learning about Bioc Packages. Functionality includes access to :
* Download statistics
* General package listing
* Build reports
* Package dependency graphs
* Vignettes
 
[https://seandavi.github.io/BiocPkgTools/articles/BiocPkgTools.html#dependency-graphs Overview of BiocPkgTools & Dependency graphs]


* Depends: list of package names which this package depends on. Those packages will be attached (so it is better to use ''Imports'' instead of ''Depends'' as much as you can) before the current package when library or require is called. The ‘Depends’ field can also specify a dependence on a certain version of R.
[https://www.biorxiv.org/content/10.1101/642132v1 BiocPkgTools: Toolkit for Mining the Bioconductor Package Ecosystem] in biorxiv.org.
* Imports: lists packages whose '''namespaces''' are imported from (as specified in the NAMESPACE file) but which do not need to be attached.
* Suggests: lists packages that are not necessarily needed. This includes packages used only in examples, tests or vignettes, and packages loaded in the body of functions.
* Enhances: lists packages “enhanced” by the package at hand, e.g., by providing methods for classes from these packages, or ways to handle objects from these packages.
* LinkingTo: A package that wishes to make use of '''header''' files in other packages needs to declare them as a comma-separated list in the field ‘LinkingTo’ in the DESCRIPTION file.
** An example is [https://www.bioconductor.org/packages/release/bioc/html/SingleR.html SingleR] that links to the ''beachmat'' package in its [https://github.com/LTLA/SingleR/blob/master/src/recompute_scores.cpp cpp source code].


== Package related functions from package 'utils' ==
== [http://cran.r-project.org/web/packages/miniCRAN/ miniCRAN package]  ==
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/available.packages.html available.packages()]; see packageStatus().
'''miniCRAN''' package can be used to identify package dependencies or create a local CRAN repository. It can be used on repositories other than CRAN, such as Bioconductor.
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/download.packages.html download.packages()]
 
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/packageStatus.html packageStatus(), update(), upgrade()]. packageStatus() will return a list with two components:
* http://blog.revolutionanalytics.com/2014/07/dependencies-of-popular-r-packages.html
# inst - a data frame with columns as the matrix returned by '''installed.packages''' plus "Status", a factor with levels c("ok", "upgrade"). Note: the manual does not mention "unavailable" case (but I do get it) in R 3.2.0?
* http://www.r-bloggers.com/introducing-minicran-an-r-package-to-create-a-private-cran-repository/
# avail - a data frame with columns as the matrix returned by '''available.packages''' plus "Status", a factor with levels c("installed", "not installed", "unavailable"). Note: I don't get the "unavailable" case in R 3.2.0?
* http://www.magesblog.com/2014/09/managing-r-package-dependencies.html
* [http://blog.revolutionanalytics.com/2015/10/using-minicran-in-azure-ml.html Using miniCRAN in Azure ML]
* [http://www.mango-solutions.com/wp/2016/01/minicran-developing-internal-cran-repositories/ developing internal CRAN Repositories]
 
Before we go into R, we need to install some packages from Ubuntu terminal. See [[R#Ubuntu.2FDebian_2|here]].
{{Pre}}
{{Pre}}
> x <- packageStatus()
# Consider glmnet package (today is 4/29/2015)
> names(x)
# Version: 2.0-2
[1] "inst"  "avail"
# Depends: Matrix (≥ 1.0-6), utils, foreach
> dim(x[['inst']])
# Suggests: survival, knitr, lars
[1] 225  17
if (!require("miniCRAN")) {
> x[['inst']][1:3, ]
  install.packages("miniCRAN", dependencies = TRUE, repos="http://cran.rstudio.com") # include 'igraph' in Suggests.
              Package                            LibPath Version Priority              Depends Imports
  library(miniCRAN)
acepack      acepack C:/Program Files/R/R-3.1.2/library 1.3-3.3    <NA>                  <NA>    <NA>
}
adabag        adabag C:/Program Files/R/R-3.1.2/library    4.0     <NA> rpart, mlbench, caret    <NA>
if (!"igraph" %in% installed.packages()[,1]) install.packages("igraph")
affxparser affxparser C:/Program Files/R/R-3.1.2/library 1.38.0    <NA>          R (>= 2.6.0)   <NA>
 
          LinkingTo                                                        Suggests Enhances
tags <- "glmnet"
acepack        <NA>                                                            <NA>    <NA>
pkgDep(tags, suggests=TRUE, enhances=TRUE) # same as pkgDep(tags)
adabag          <NA>                                                            <NA>    <NA>
[1] "glmnet"    "Matrix"    "foreach"  "codetools" "iterators" "lattice"  "evaluate"  "digest"    
affxparser      <NA> R.oo (>= 1.18.0), R.utils (>= 1.32.4),\nAffymetrixDataTestFiles    <NA>
[9] "formatR"  "highr"    "markdown"  "stringr"  "yaml"      "mime"      "survival"  "knitr"   
                      License License_is_FOSS License_restricts_use OS_type MD5sum NeedsCompilation Built
# [17] "lars" 
acepack    MIT + file LICENSE            <NA>                  <NA>    <NA>  <NA>              yes 3.1.2
 
adabag            GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>              no 3.1.2
dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE) # miniCRAN defines its makeDepGraph()
affxparser        LGPL (>= 2)           <NA>                  <NA>    <NA>  <NA>            <NA> 3.1.1
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
                Status
acepack            ok
adabag              ok
affxparser unavailable
> dim(x[['avail']])
[1] 6538   18
> x[['avail']][1:3, ]
                Package Version Priority                        Depends        Imports LinkingTo
A3                  A3  0.9.2    <NA> R (>= 2.15.0), xtable, pbapply          <NA>      <NA>
ABCExtremes ABCExtremes    1.0    <NA>      SpatialExtremes, combinat          <NA>      <NA>
ABCanalysis ABCanalysis  1.0.1    <NA>                    R (>= 2.10) Hmisc, plotrix      <NA>
                      Suggests Enhances    License License_is_FOSS License_restricts_use OS_type Archs
A3          randomForest, e1071    <NA> GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
ABCExtremes                <NA>    <NA>      GPL-2            <NA>                  <NA>    <NA>  <NA>
ABCanalysis                <NA>    <NA>      GPL-3            <NA>                  <NA>    <NA>  <NA>
            MD5sum NeedsCompilation File                                      Repository        Status
A3            <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
ABCExtremes  <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
ABCanalysis  <NA>            <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
</pre>
</pre>
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/packageDescription.html packageVersion(), packageDescription()]
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/install.packages.html install.packages()], [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/remove.packages.html remove.packages()].
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/installed.packages.html installed.packages()]; see packageStatus().
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/update.packages.html update.packages(), old.packages(), new.packages()]
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/setRepositories.html setRepositories()]
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/contrib.url.html contrib.url()]
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/chooseCRANmirror.html chooseCRANmirror()], [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/chooseBioCmirror.html chooseBioCmirror()]
* [http://stat.ethz.ch/R-manual/R-devel/library/utils/html/globalVariables.html suppressForeignCheck()]


== tools package ==
[[File:MiniCRAN dep.svg|300px]] [[File:pkgDepTools dep.svg|300px]]
* https://www.rdocumentation.org/packages/tools/versions/3.6.1
[[File:Glmnet dep.svg|300px]]
* CRAN_package_db() from [https://www.rdocumentation.org/packages/tools/versions/3.6.1/topics/CRANtools ?CRANtools]. Especially, it gives the Description and Maintainer information not provided by utils::available.packages()
 
* [https://www.rdocumentation.org/packages/tools/versions/3.6.1/topics/dependsOnPkgs dependsOnPkgs()]
We can also display the dependence for a package from the [http://cran.r-project.org/web/packages/miniCRAN/vignettes/miniCRAN-non-CRAN-repos.html Bioconductor] repository.
:<syntaxhighlight lang='rsplus'>
{{Pre}}
db <- tools::CRAN_package_db()
tags <- "DESeq2"
nRcpp <- length(tools::dependsOnPkgs("Rcpp", recursive=FALSE, installed=db) )
# Depends S4Vectors, IRanges, GenomicRanges, Rcpp (>= 0.10.1), RcppArmadillo (>= 0.3.4.4)
nCompiled <- table(db[, "NeedsCompilation"])[["yes"]]
# Imports BiocGenerics(>= 0.7.5), Biobase, BiocParallel, genefilter, methods, locfit, geneplotter, ggplot2, Hmisc
propRcpp <- nRcpp / nCompiled * 100
# Suggests RUnit, gplots, knitr, RColorBrewer, BiocStyle, airway,\npasilla (>= 0.2.10), DESeq, vsn
</syntaxhighlight>
# LinkingTo    Rcpp, RcppArmadillo
* package.dependencies(), pkgDepends(), etc are deprecated now, mostly in favor of [https://www.rdocumentation.org/packages/tools/versions/3.6.2/topics/package_dependencies package_dependencies()] which is both more flexible and efficient. See [https://cran.rstudio.com/doc/manuals/r-release/NEWS.html R 3.3.0 News]. For example, tools::package_dependencies(c("remotes", "devtools"), recursive=TRUE) shows ''remotes'' has only a few dependencies while ''devtools'' has a lot.
index <- function(url, type="source", filters=NULL, head=5, cols=c("Package", "Version")){
  contribUrl <- contrib.url(url, type=type)
  available.packages(contribUrl, type=type, filters=filters)
}


== crandep package ==
bioc <- local({
https://cran.r-project.org/web/packages/crandep/index.html. Useful to find reverse dependencies. [https://rdrr.io/cran/crandep/man/get_dep.html ?get_dep]. Consider the [https://cran.r-project.org/web/packages/abc/index.html abc] package:
  env <- new.env()
<pre>
  on.exit(rm(env))
get_dep("abc", "depends") # abc depends on these packages
  evalq(source("http://bioconductor.org/biocLite.R", local=TRUE), env)
                          # note my computer does not have 'abc' installed
   biocinstallRepos() # return URLs
#   from      to    type reverse
})
# 1  abc abc.data depends  FALSE
# 2  abc    nnet depends  FALSE
# 3  abc quantreg depends  FALSE
# 4  abc    MASS depends  FALSE
# 5  abc  locfit depends  FALSE


get_dep("abc", "reverse_depends")
bioc
#   from      to    type reverse
#                                              BioCsoft
# 1  abc abctools depends    TRUE
#            "http://bioconductor.org/packages/3.0/bioc"
# 2  abc  EasyABC depends    TRUE
#                                                BioCann
# "http://bioconductor.org/packages/3.0/data/annotation"
#                                                BioCexp
# "http://bioconductor.org/packages/3.0/data/experiment"
#                                              BioCextra
#          "http://bioconductor.org/packages/3.0/extra"  
#                                                  CRAN
#                                "http://cran.fhcrc.org"  
#                                             CRANextra
#                   "http://www.stats.ox.ac.uk/pub/RWin"
str(index(bioc["BioCsoft"])) # similar to cranJuly2014 object


x <- get_dep("RcppEigen", c("reverse linking to"))  
system.time(dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE, availPkgs = index(bioc["BioCsoft"]))) # Very quick!
dim(x)
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
# [1] 331  4
head(x, 3)
#        from      to      type reverse
# 1 RcppEigen  abess linking to    TRUE
# 2 RcppEigen    acrt linking to    TRUE
# 3 RcppEigen ADMMnet linking to    TRUE
</pre>
</pre>
[[File:deseq2 dep.svg|300px]] [[File:Lumi dep.svg|300px]]


== remotes ==
The dependencies of [http://www.bioconductor.org/packages/release/bioc/html/GenomicFeatures.html GenomicFeature] and [http://www.bioconductor.org/packages/release/bioc/html/GenomicAlignments.html GenomicAlignments] are more complicated. So we turn the 'suggests' option to FALSE.
[https://www.rdocumentation.org/packages/remotes/versions/2.2.0/topics/package_deps  remotes::local_package_deps(dependencies=TRUE)] will find and return all dependent packages based on the "DESCRIPTION" file. See an example [https://github.com/Bioconductor/OrchestratingSingleCellAnalysis here].
<syntaxhighlight lang='rsplus'>
tags <- "GenomicAlignments"
dg <- makeDepGraph(tags, suggests=FALSE, enhances=FALSE, availPkgs = index(bioc["BioCsoft"]))
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
</syntaxhighlight>
[[File:Genomicfeature dep dep.svg|300px]] [[File:Genomicalignments dep.svg|300px]]


== Bioconductor's [http://www.bioconductor.org/packages/release/bioc/html/pkgDepTools.html pkgDepTools] package ==
=== Github repository ===
The is an example of querying the dependencies of the notorious 'lumi' package which often broke the installation script. I am using R 3.2.0 and Bioconductor 3.1.
[https://thierrymoudiki.github.io/blog/2020/10/16/r/esgtoolkit-new-cran Submitting R package to CRAN]


The '''getInstallOrder''' function is useful to get a list of all (recursive) dependency packages.
== cranlike ==
{{Pre}}
https://github.com/r-hub/cranlike
source("http://bioconductor.org/biocLite.R")
if (!require(pkgDepTools)) {
  biocLite("pkgDepTools", ask = FALSE)
  library(pkgDepTools)
}
MkPlot <- FALSE


library(BiocInstaller)
cranlike keeps the package data in a SQLite database, in addition to the PACKAGES* files. This database is the canonical source of the package data. It can be updated quickly, to add and remove packages. The PACKAGES* files are generated from the database.
biocUrl <- biocinstallRepos()["BioCsoft"]
biocDeps <- makeDepGraph(biocUrl, type="source", dosize=FALSE) # pkgDepTools defines its makeDepGraph()


PKG <- "lumi"
== [http://mran.revolutionanalytics.com/ MRAN] (CRAN only) & checkpoint package ==
if (MkPlot) {
* http://blog.revolutionanalytics.com/2014/10/explore-r-package-connections-at-mran.html
  if (!require(Biobase))  {
* [https://rsangole.netlify.app/post/2020/10/10/reproducible-work-in-r/?s=09 Reproducible Work in R]
    biocLite("Biobase", ask = FALSE)
* [https://datacolada.org/108 MRAN is Dead, long live GRAN] 4/28/2023
    library(Biobase)
  }
  if (!require(Rgraphviz))  {
    biocLite("Rgraphviz", ask = FALSE)
    library(Rgraphviz)
  }
  categoryNodes <- c(PKG, names(acc(biocDeps, PKG)[[1]])) 
  categoryGraph <- subGraph(categoryNodes, biocDeps)
  nn <- makeNodeAttrs(categoryGraph, shape="ellipse")
  plot(categoryGraph, nodeAttrs=nn)  # Complete but plot is too complicated & font is too small.
}


system.time(allDeps <- makeDepGraph(biocinstallRepos(), type="source",
According to the snapsot list [https://cran.microsoft.com/snapshot/ here], the oldest version is 2014-08-18 which corresponds to R 3.1.0.
                          keep.builtin=TRUE, dosize=FALSE)) # takes a little while
#    user  system elapsed
# 175.737  10.994 186.875
# Warning messages:
# 1: In .local(from, to, graph) : edges replaced: ‘SNPRelate|gdsfmt’
# 2: In .local(from, to, graph) :
#  edges replaced: ‘RCurl|methods’, ‘NA|bitops’


# When needed.only=TRUE, only those dependencies not currently installed are included in the list.
[https://mran.revolutionanalytics.com/documents/rro/reproducibility#timemachine checkpoint] package
x1 <- sort(getInstallOrder(PKG, allDeps, needed.only=TRUE)$packages); x1
<pre>
[1] "affy"                              "affyio"                         
library(checkpoint)
[3] "annotate"                          "AnnotationDbi"                   
checkpoint("2015-03-31")
[5] "base64"                            "beanplot"                       
</pre>
  [7] "Biobase"                           "BiocParallel"                   
 
[9] "biomaRt"                          "Biostrings"                     
'''Note the Bioconductor packages have no similar solution.'''
[11] "bitops"                            "bumphunter"                     
 
[13] "colorspace"                        "DBI"                             
== groundhog package ==
[15] "dichromat"                        "digest"                         
* [https://cran.r-project.org/web/packages/groundhog/index.html groundhog]: Version-Control for CRAN, GitHub, and GitLab Packages (Bioconductor?)
[17] "doRNG"                            "FDb.InfiniumMethylation.hg19"      
* It seems groundhog is like checkpoint but groundhog does not depend on MRAN and it will figure out the package dependencies by itself.
[19] "foreach"                           "futile.logger"                   
* [https://www.brodrigues.co/blog/2023-01-12-repro_r/ MRAN is getting shutdown] - what else is there for reproducibility with R, or why reproducibility is on a continuum? The author provides an example where we can integrate "groundhog" in the Dockerfile for reproducibility. Pay attention to the sentence (appear 2 times) '''“why use Docker at all? Since it’s easy to install older versions of R on Windows and macOS, wouldn’t an renv.lock file suffice? Or even just {groundhog} which is arguably even easier to use?” '''
[21] "futile.options"                    "genefilter"                     
* [https://github.com/rocker-org/rocker-versioned2/issues/593 MRAN is getting shutdown #593] related to Rocker project. Rocker now uses [https://docs.posit.co/rspm/admin/ RSPM (PPM now)] from RStudio.
[23] "GenomeInfoDb"                      "GenomicAlignments"               
* groundhog.library() differs from the library() function: 1) it installed and loaded packages in one step, 2) it allowed to install multiple packages.
[25] "GenomicFeatures"                  "GenomicRanges"                   
 
[27] "GEOquery"                          "ggplot2"                        
Note I use the docker's R since that's the easiest way to use an old version of R (e.g. for some old R script) in Linux. Note that it will create a new folder '''R_groundhog''' folder in the working directory (see the message below).
[29] "gtable"                           "illuminaio"                     
<pre>
[31] "IRanges"                           "iterators"                       
$ docker run --rm -it -v $(pwd):/home/docker \
[33] "labeling"                          "lambda.r"                       
     -w /home/docker -u docker r-base:4.0.2 R
[35] "limma"                            "locfit"                         
 
[37] "lumi"                             "magrittr"                        
> install.packages("groundhog")
[39] "matrixStats"                      "mclust"                         
> library(groundhog")
[41] "methylumi"                        "minfi"                           
groundhog needs authorization to save files to '/home/docker/R_groundhog'
[43] "multtest"                          "munsell"                         
Enter 'OK' to provide authorization
[45] "nleqslv"                           "nor1mix"                         
OK
[47] "org.Hs.eg.db"                     "pkgmaker"                       
The groundhog folder path is now:
[49] "plyr"                             "preprocessCore"                  
/home/docker/R_groundhog/groundhog_library/
[51] "proto"                             "quadprog"                        
Loaded 'groundhog' (version:2.1.0) using R-4.0.2
[53] "RColorBrewer"                      "Rcpp"                           
Tips and troubleshooting: https://groundhogR.com
[55] "RCurl"                            "registry"                       
 
[57] "reshape"                          "reshape2"                       
> groundhog.library("
[59] "rngtools"                          "Rsamtools"                       
  library(ggplot2)",
[61] "RSQLite"                          "rtracklayer"                     
    "2020-10-10")
[63] "S4Vectors"                        "scales"                         
> library()
[65] "siggenes"                          "snow"                           
# Packages in library ‘/usr/local/lib/R/site-library’:
[67] "stringi"                          "stringr"                         
> packageVersion("ggplot2")
[69] "TxDb.Hsapiens.UCSC.hg19.knownGene" "XML"                            
[1] ‘3.3.2’  # the latest version is 3.4.0 on R 4.2.2
[71] "xtable"                           "XVector"                         
> library(ggplot2)
[73] "zlibbioc"                       
</pre>
 
(Current groundhog is v3.1.0) If I don't specify a user in docker run, I'll need to call groundhog.library() twice in order to install packages (eg. "DT" package). In this case I can use '''groundhog:::save.cookie("copy_instead_of_renaming")''' before calling groundhog.library().
<pre>
$ docker run -it --rm rocker/verse:4.3.0 bash
root@58978695ec12:/# R
R version 4.3.0 (2023-04-21) -- "Already Tomorrow"
...
> install.packages("groundhog"); library(groundhog)
 
> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/local/lib/R/library"
> get.groundhog.folder()
[1] "/root/R_groundhog/groundhog_library/"
 
> groundhog:::save.cookie("copy_instead_of_renaming")
groundhog needs authorization to save files to '/root/R_groundhog/'
Enter 'OK' to provide authorization, and 'NO' not to.
|  >OK
 
Groundhog folder set to: '/root/R_groundhog/groundhog_library/
 
--- You may change it with`set.groundhog.folder(<path>)`---
Downloading database with information for all CRAN packages ever published
trying URL 'http://s3.wasabisys.com/groundhog/cran.toc.rds'
Content type 'application/octet-stream' length 2010504 bytes (1.9 MB)
==================================================
downloaded 1.9 MB
 
Downloading database with installation times for all source packages on CRAN
trying URL 'http://s3.wasabisys.com/groundhog/cran.times.rds'
Content type 'application/octet-stream' length 803912 bytes (785 KB)
==================================================
downloaded 785 KB
> groundhog.library("DT", "2023-4-23")
> find.package("DT")
</pre>
 
== rang ==
[http://blog.schochastics.net/post/rang-make-ancient-r-code-run-again/ rang: make ancient R code run again]


# When needed.only=FALSE the complete list of dependencies is given regardless of the set of currently installed packages.
== [https://cran.r-project.org/web/packages/cranly/ cranly] ==
x2 <- sort(getInstallOrder(PKG, allDeps, needed.only=FALSE)$packages); x2
[https://cran.r-project.org/web/packages/cranly/vignettes/dependence_trees.html R package dependence trees]
[1] "affy"                              "affyio"                            "annotate"                       
[4] "AnnotationDbi"                    "base64"                            "beanplot"                       
[7] "Biobase"                          "BiocGenerics"                      "BiocInstaller"                   
[10] "BiocParallel"                      "biomaRt"                          "Biostrings"                     
[13] "bitops"                            "bumphunter"                        "codetools"                       
[16] "colorspace"                        "DBI"                              "dichromat"                       
[19] "digest"                            "doRNG"                            "FDb.InfiniumMethylation.hg19"   
[22] "foreach"                          "futile.logger"                    "futile.options"                 
[25] "genefilter"                        "GenomeInfoDb"                      "GenomicAlignments"               
[28] "GenomicFeatures"                  "GenomicRanges"                    "GEOquery"                       
[31] "ggplot2"                          "graphics"                          "grDevices"                       
[34] "grid"                              "gtable"                            "illuminaio"                     
[37] "IRanges"                          "iterators"                        "KernSmooth"                     
[40] "labeling"                          "lambda.r"                          "lattice"                         
[43] "limma"                            "locfit"                            "lumi"                           
[46] "magrittr"                          "MASS"                              "Matrix"                         
[49] "matrixStats"                      "mclust"                            "methods"                         
[52] "methylumi"                        "mgcv"                              "minfi"                           
[55] "multtest"                          "munsell"                          "nleqslv"                         
[58] "nlme"                              "nor1mix"                          "org.Hs.eg.db"                   
[61] "parallel"                          "pkgmaker"                          "plyr"                           
[64] "preprocessCore"                    "proto"                            "quadprog"                       
[67] "RColorBrewer"                      "Rcpp"                              "RCurl"                           
[70] "registry"                          "reshape"                          "reshape2"                       
[73] "rngtools"                          "Rsamtools"                        "RSQLite"                         
[76] "rtracklayer"                      "S4Vectors"                        "scales"                         
[79] "siggenes"                          "snow"                              "splines"                         
[82] "stats"                            "stats4"                            "stringi"                         
[85] "stringr"                          "survival"                          "tools"                           
[88] "TxDb.Hsapiens.UCSC.hg19.knownGene" "utils"                            "XML"                             
[91] "xtable"                            "XVector"                          "zlibbioc"


> sort(setdiff(x2, x1)) # Not all R's base packages are included; e.g. 'base', 'boot', ...
== sessioninfo ==
[1] "BiocGenerics" "BiocInstaller" "codetools"    "graphics"      "grDevices"   
<pre>
[6] "grid"          "KernSmooth"    "lattice"      "MASS"          "Matrix"     
tmp = session_info("sessioninfo")
[11] "methods"       "mgcv"          "nlme"          "parallel"      "splines"     
dim(tmp$packages) # [1]  7 11
[16] "stats"        "stats4"        "survival"      "tools"        "utils" 
 
tmp = session_info("tidyverse")
dim(tmp$packages) # [1] 95 11
</pre>
</pre>
[[File:Lumi rgraphviz.svg|200px]]


== Bioconductor [https://bioconductor.org/packages/devel/bioc/html/BiocPkgTools.html BiocPkgTools] ==
== Reverse dependence ==
Collection of simple tools for learning about Bioc Packages. Functionality includes access to :
* http://romainfrancois.blog.free.fr/index.php?post/2011/10/30/Rcpp-reverse-dependency-graph
* Download statistics
* [http://www.markvanderloo.eu/yaRb/2019/04/26/checking-reverse-dependencies-the-tiny-way/ Checking reverse dependencies: the tiny way]
* General package listing
* Build reports
* Package dependency graphs
* Vignettes


[https://seandavi.github.io/BiocPkgTools/articles/BiocPkgTools.html#dependency-graphs Overview of BiocPkgTools & Dependency graphs]
== Install packages offline ==
http://www.mango-solutions.com/wp/2017/05/installing-packages-without-internet/


[https://www.biorxiv.org/content/10.1101/642132v1 BiocPkgTools: Toolkit for Mining the Bioconductor Package Ecosystem] in biorxiv.org.
== Install a packages locally and its dependencies ==
It's impossible to install the dependencies if you want to install a package locally. See [http://r.789695.n4.nabble.com/Windows-GUI-quot-Install-Packages-from-local-zip-files-quot-and-dependencies-td848173.html Windows-GUI: "Install Packages from local zip files" and dependencies]


== [http://cran.r-project.org/web/packages/miniCRAN/ miniCRAN package] ==
== A minimal R package (for testing purpose) ==
'''miniCRAN''' package can be used to identify package dependencies or create a local CRAN repository. It can be used on repositories other than CRAN, such as Bioconductor.
* https://github.com/joelnitta/minimal. Question: is there a one from CRAN?
* [https://kbroman.org/pkg_primer/pages/minimal.html The minimal R package]
* [https://cran.r-project.org/web/packages/QuadRoot/ QuadRoot] - the smallest R package on CRAN as of 5/10/2023. Others include (starting from the smallest) relen, signibox, freqdist, fdq.


* http://blog.revolutionanalytics.com/2014/07/dependencies-of-popular-r-packages.html
== An R package that does not require others during install.packages() ==
* http://www.r-bloggers.com/introducing-minicran-an-r-package-to-create-a-private-cran-repository/
* [https://cran.r-project.org/web/packages/crayon/index.html crayon]
* http://www.magesblog.com/2014/09/managing-r-package-dependencies.html
* [https://cran.rstudio.com/web/packages/testit/index.html testit]
* [http://blog.revolutionanalytics.com/2015/10/using-minicran-in-azure-ml.html Using miniCRAN in Azure ML]
* [https://cran.rstudio.com/web/packages/docopt/index.html docopt] and [https://cran.rstudio.com/web/packages/littler/index.html littler]. See also [https://hadley.shinyapps.io/cran-downloads/ cran-downloads] shiny.
* [http://www.mango-solutions.com/wp/2016/01/minicran-developing-internal-cran-repositories/ developing internal CRAN Repositories]
* [https://cran.r-project.org/web/packages/Rcpp/index.html Rcpp]


Before we go into R, we need to install some packages from Ubuntu terminal. See [[R#Ubuntu.2FDebian_2|here]].
= Create a new R package, namespace, documentation =
{{Pre}}
* http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf (highly recommend)
# Consider glmnet package (today is 4/29/2015)
* https://stat.ethz.ch/pipermail/r-devel/2013-July/066975.html
# Version: 2.0-2
* [http://stackoverflow.com/questions/7283134/what-is-the-benefit-of-import-in-a-namespace-in-r/7283511#7283511 Benefit of import in a namespace]
# Depends: Matrix (≥ 1.0-6), utils, foreach
* This youtube [http://www.youtube.com/watch?v=jGeCCxdZsDQ video] from Tyler Rinker teaches how to use RStudio to develop an R package and also use Git to do version control. Very useful!
# Suggests: survival, knitr, lars
* [https://github.com/jtleek/rpackages Developing R packages] by Jeff Leek in Johns Hopkins University.
if (!require("miniCRAN"))  {
* [http://r-pkgs.had.co.nz/ R packages] book by Hadley Wickham.
  install.packages("miniCRAN", dependencies = TRUE, repos="http://cran.rstudio.com") # include 'igraph' in Suggests.
* [http://kbroman.org/pkg_primer/ R package primer] a minimal tutorial from Karl Broman.
  library(miniCRAN)
* [https://datascienceplus.com/how-to-make-and-share-an-r-package-in-3-steps/ How to make and share an R package in 3 steps] (6/14/2017)
}
* [https://github.com/jennybc/pkg-dev-tutorial Package Development tutorial for useR! 2019 Toulouse]
if (!"igraph" %in% installed.packages()[,1]) install.packages("igraph")
* [https://devguide.ropensci.org/ rOpenSci Packages: Development, Maintenance, and Peer Review]
* [https://b-rodrigues.github.io/modern_R/package-development.html Package development] from "Modern R with the tidyverse"
* [https://youtu.be/qxRSzDejea4 How to write your own R package] (video, 10 minutes)
* [https://youtu.be/EpTkT6Rkgbs Building R packages with devtools and usethis | RStudio] (video, thomas mock)
* [https://quantixed.org/2022/09/06/the-package-learning-how-to-build-an-r-package/ The Package: learning how to build an R package]
* [https://www.mzes.uni-mannheim.de/socialsciencedatalab/article/r-package/ How to write your own R package and publish it on CRAN]


tags <- "glmnet"
== Package structure ==
pkgDep(tags, suggests=TRUE, enhances=TRUE) # same as pkgDep(tags)
http://r-pkgs.had.co.nz/package.html. On Linux/macOS, use '''tree -d DIRNAME''' to show the directories only. At a minimum, we will have '''R''' and '''man''' directories.
#  [1] "glmnet"    "Matrix"    "foreach"  "codetools" "iterators" "lattice"  "evaluate"  "digest" 
#  [9] "formatR"  "highr"    "markdown"  "stringr"  "yaml"      "mime"      "survival"  "knitr"   
# [17] "lars" 


dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE) # miniCRAN defines its makeDepGraph()
* ChangeLog
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
* DESCRIPTION
</pre>
* MD5
 
* NAMESPACE
[[File:MiniCRAN dep.svg|300px]] [[File:pkgDepTools dep.svg|300px]]
* R/
[[File:Glmnet dep.svg|300px]]
** zzz.R
 
* build/
We can also display the dependence for a package from the [http://cran.r-project.org/web/packages/miniCRAN/vignettes/miniCRAN-non-CRAN-repos.html Bioconductor] repository.
** Package.pdf (eg [https://github.com/cran/dplyr/tree/master/build dplyr])
{{Pre}}
** vignette.rds
tags <- "DESeq2"
* data/
# Depends S4Vectors, IRanges, GenomicRanges, Rcpp (>= 0.10.1), RcppArmadillo (>= 0.3.4.4)
* demo/
# Imports BiocGenerics(>= 0.7.5), Biobase, BiocParallel, genefilter, methods, locfit, geneplotter, ggplot2, Hmisc
* inst/
# Suggests RUnit, gplots, knitr, RColorBrewer, BiocStyle, airway,\npasilla (>= 0.2.10), DESeq, vsn
** extdata/
# LinkingTo    Rcpp, RcppArmadillo
** doc/
index <- function(url, type="source", filters=NULL, head=5, cols=c("Package", "Version")){
*** FileName.R
  contribUrl <- contrib.url(url, type=type)
*** FileName.Rmd
  available.packages(contribUrl, type=type, filters=filters)
*** FileName.html
}
** include/
** othersYouInclude/
** tinytest/
** CITATION
* man/
** figures/
* src/
* tests/
** testthat
* vignettes/


bioc <- local({
=== NAMESPACE and DESCRIPTION ===
  env <- new.env()
[https://stackoverflow.com/a/13261139 Namespace dependencies not required]. ''If you use import or importFrom in your NAMESPACE file, you should have an entry for that package in the Imports section of your DESCRIPTION file (unless there is a reason that you need to use Depends in which case the package should have an entry in Depends, and not Imports).''
  on.exit(rm(env))
  evalq(source("http://bioconductor.org/biocLite.R", local=TRUE), env)
  biocinstallRepos() # return URLs
})


bioc
=== license ===
#                                              BioCsoft
* [https://stackoverflow.com/a/38549043 What kind of license is the best license for an R package?]
#            "http://bioconductor.org/packages/3.0/bioc"
* https://cran.r-project.org/web/licenses/
#                                                BioCann
* https://r-pkgs.org/description.html#description-license
# "http://bioconductor.org/packages/3.0/data/annotation"
* [https://win-vector.com/2019/07/30/some-notes-on-gnu-licenses-in-r-packages/ Some Notes on GNU Licenses in R Packages]
#                                                BioCexp
# "http://bioconductor.org/packages/3.0/data/experiment"
#                                             BioCextra
#          "http://bioconductor.org/packages/3.0/extra"
#                                                  CRAN
#                                "http://cran.fhcrc.org"
#                                              CRANextra
#                  "http://www.stats.ox.ac.uk/pub/RWin"
str(index(bioc["BioCsoft"])) # similar to cranJuly2014 object


system.time(dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE, availPkgs = index(bioc["BioCsoft"]))) # Very quick!
== Install software for PDF output ==
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
* Windows: [http://miktex.org/download Miktex] and [https://pandoc.org/installing.html pandoc] ([http://rprogramming.net/create-html-or-pdf-files-with-r-knitr-miktex-and-pandoc/ Create HTML or PDF Files with R, Knitr, MiKTeX, and Pandoc])
</pre>
* Unix: [https://tug.org/texlive/ TeX Live] ('''sudo apt-get install texlive-full''') and [https://pandoc.org/installing.html pandoc]
[[File:deseq2 dep.svg|300px]] [[File:Lumi dep.svg|300px]]
* Mac: [https://tug.org/mactex/ MacTex]


The dependencies of [http://www.bioconductor.org/packages/release/bioc/html/GenomicFeatures.html GenomicFeature] and [http://www.bioconductor.org/packages/release/bioc/html/GenomicAlignments.html GenomicAlignments] are more complicated. So we turn the 'suggests' option to FALSE.
== Windows: Rtools ==
<syntaxhighlight lang='rsplus'>
[https://thecoatlessprofessor.com/programming/installing-rtools-for-compiled-code-via-rcpp/ Installing RTools for Compiled Code via Rcpp]. Just remember to check the option to include some paths in the '''PATH''' environment variable.
tags <- "GenomicAlignments"
dg <- makeDepGraph(tags, suggests=FALSE, enhances=FALSE, availPkgs = index(bioc["BioCsoft"]))
plot(dg, legendPosition = c(-1, 1), vertex.size=20)
</syntaxhighlight>
[[File:Genomicfeature dep dep.svg|300px]] [[File:Genomicalignments dep.svg|300px]]


=== Github repository ===
== R CMD ==
[https://thierrymoudiki.github.io/blog/2020/10/16/r/esgtoolkit-new-cran Submitting R package to CRAN]
[https://cran.r-project.org/doc/manuals/R-admin.html R Installation and Administration]


== cranlike ==
* R CMD build XXX. Note this will not create pdf files for vignettes. The output is a tarball.
https://github.com/r-hub/cranlike
* R CMD INSTALL
* R CMD REMOVE
* R CMD SHLIB files. For example, "Rcmd shlib *.f *.c -o surv.dll" on Windows.
* R CMD make
* R CMD check XXX. Useful if we want to create reference manual (PDF file). See [https://stackoverflow.com/a/16987795 R create reference manual with R CMD check].
* R CMD javareconf


cranlike keeps the package data in a SQLite database, in addition to the PACKAGES* files. This database is the canonical source of the package data. It can be updated quickly, to add and remove packages. The PACKAGES* files are generated from the database.
== usethis package ==
* https://usethis.r-lib.org/. '''usethis''' is a workflow package: it automates repetitive tasks that arise during project setup and development, both for R packages and non-package projects.
** [https://youtu.be/79s3z0gIuFU?t=705 R Package Development 1: Where to Start] by John Muschelli. [https://www.youtube.com/playlist?list=PLk3B5c8iCV-T4LM0mwEyWIunIunLyEjqM R Package Development playlist].
** [https://usethis.r-lib.org/reference/create_package.html create_package(path)] with the package name of your choice.
** [https://javirudolph.github.io/RLadies-Gainesville-FL/20190429-Rudolph-packages/apr29presentation.html#1 Sharing with others and helping yourself] (slides, use right click menu to save as an HTML file)
* [https://www.tidyverse.org/articles/2019/04/usethis-1.5.0/ usethis 1.5.0]
* [https://www.tidyverse.org/blog/2020/04/usethis-1-6-0/ usethis 1.6.0]
* [https://www.tidyverse.org/blog/2020/12/usethis-2-0-0/ usethis 2.0.0]


== [http://mran.revolutionanalytics.com/ MRAN] (CRAN only) ==
=== Github Actions ===
* http://blog.revolutionanalytics.com/2014/10/explore-r-package-connections-at-mran.html
To use the '''Github Actions''' for continuous integration/CI checks,
* [https://rsangole.netlify.app/post/2020/10/10/reproducible-work-in-r/?s=09 Reproducible Work in R]
* I first follow [https://ropenscilabs.github.io/actions_sandbox/packageci.html this] to run ''usethis::use_github_action_check_release()''. Once I commit and push the files to Github, Github Actions are kicked off. The R console also tell me to copy and paste a line to add a workflow's badge to README.md.
* Then I modify the yaml file to become [https://github.com/r-lib/actions/blob/master/examples/check-standard.yaml this] to run a standard check. This took 3m24s to run.  
* Then I further delete the 'devel' line (line 23) to reduce one more platform to check. This took 3m to check. My example is on [https://github.com/arraytools/rtoy/actions Github] (rtoy).


According to the snapsot list [https://cran.microsoft.com/snapshot/ here], the oldest version is 2014-08-18 which corresponds to R 3.1.0.
I also try to follow [https://ropenscilabs.github.io/actions_sandbox/websites-using-pkgdown-bookdown-and-blogdown.html Github Actions with R] and create a [https://github.com/r-lib/pkgdown pkgdown]/package documentation web page.
* '''usethis::use_github_actions("pkgdown")''' Now go to Github repo's Settings -> Options and scroll down until you see ''Github Pages''. For ''Source'', the page site should be set to being built from the root folder of the ''gh-pages''.
* I have used Jekyll to create a gh-page. I don't need to delete anything for this new gh-pages. I just need to go to the repository setting and (scroll down until we see Github Pages) change the Source of Github Pages to 'gh-pages branch' from 'master branch'.
* The files on the gh-pages branch are generated by Github Actions; these files are not available on my local machine. My location machine only has '''.github/workflows/pkgdown.yaml''' file.


[https://mran.revolutionanalytics.com/documents/rro/reproducibility#timemachine checkpoint] package
Question:
<pre>
* The workflow file specifies R version and OS platform.  
library(checkpoint)
* Right now the workflow file (like pkgdown) is using "r-lib/actions/setup-r@master"  that has an "action.yml" file. The r-version is '3.x' only. What about if R 4.0.0 is released?
checkpoint("2015-03-31")
</pre>


'''Note the Bioconductor packages have no similar solution.'''
=== Packages, webpages and Github ===
[https://debruine.github.io/tutorials/index.html Tutorials] by Lisa DeBruine


== [https://cran.r-project.org/web/packages/cranly/ cranly] ==
== biocthis ==
[https://cran.r-project.org/web/packages/cranly/vignettes/dependence_trees.html R package dependence trees]
[https://lcolladotor.github.io/biocthis/ biocthis], [https://speakerdeck.com/lcolladotor/biocthis-tab slides]


== sessioninfo ==
== R package depends vs imports ==
<pre>
* http://stackoverflow.com/questions/8637993/better-explanation-of-when-to-use-imports-depends
tmp = session_info("sessioninfo")
* http://stackoverflow.com/questions/9893791/imports-and-depends
dim(tmp$packages) # [1]  7 11
* https://stat.ethz.ch/pipermail/r-devel/2013-August/067082.html


tmp = session_info("tidyverse")
In the namespace era Depends is never really needed. All modern packages have no technical need for Depends anymore. Loosely speaking the only purpose of Depends today is to expose other package's functions to the user without re-exporting them.
dim(tmp$packages) # [1] 95 11
</pre>


== Reverse dependence ==
load = functions exported in myPkg are available to interested parties as myPkg::foo or via direct imports - essentially this means the package can now be used
* http://romainfrancois.blog.free.fr/index.php?post/2011/10/30/Rcpp-reverse-dependency-graph
* [http://www.markvanderloo.eu/yaRb/2019/04/26/checking-reverse-dependencies-the-tiny-way/ Checking reverse dependencies: the tiny way]


== Install packages offline ==
attach = the namespace (and thus all exported functions) is attached to the search path - the only effect is that you have now added the exported functions to the global pool of functions - sort of like dumping them in the workspace (for all practical purposes, not technically)
http://www.mango-solutions.com/wp/2017/05/installing-packages-without-internet/


== Install a packages locally and its dependencies ==
import a function into a package = make sure that this function works in my package regardless of the search path (so I can write fn1 instead of pkg1::fn1 and still know it will come from pkg1 and not someone's workspace or other package that chose the same name)
It's impossible to install the dependencies if you want to install a package locally. See [http://r.789695.n4.nabble.com/Windows-GUI-quot-Install-Packages-from-local-zip-files-quot-and-dependencies-td848173.html Windows-GUI: "Install Packages from local zip files" and dependencies]


== A minimal R package (for testing purpose) ==
------------------------------------------------------------------------
https://github.com/joelnitta/minimal. Question: is there a one from CRAN?
* https://stat.ethz.ch/pipermail/r-devel/2013-September/067451.html


== An R package that does not require others during install.packages() ==
The distinction is between "loading" and "attaching" a package. Loading
* [https://cran.r-project.org/web/packages/crayon/index.html crayon]
it (which would be done if you had MASS::loglm, or imported it)
* [https://cran.rstudio.com/web/packages/testit/index.html testit]
guarantees that the package is initialized and in memory, but doesn't
* [https://cran.rstudio.com/web/packages/docopt/index.html docopt] and [https://cran.rstudio.com/web/packages/littler/index.html littler]. See also [https://hadley.shinyapps.io/cran-downloads/ cran-downloads] shiny.
make it visible to the user without the explicit MASS:: prefix.
* [https://cran.r-project.org/web/packages/Rcpp/index.html Rcpp]
Attaching it first loads it, then modifies the user's search list so the
user can see it.


= Create a new R package, namespace, documentation =
Loading is less intrusive, so it's preferred over attaching.  Both
* http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf (highly recommend)
library() and require() would attach it.
* https://stat.ethz.ch/pipermail/r-devel/2013-July/066975.html
* [http://stackoverflow.com/questions/7283134/what-is-the-benefit-of-import-in-a-namespace-in-r/7283511#7283511 Benefit of import in a namespace]
* This youtube [http://www.youtube.com/watch?v=jGeCCxdZsDQ video] from Tyler Rinker teaches how to use RStudio to develop an R package and also use Git to do version control. Very useful!
* [https://github.com/jtleek/rpackages Developing R packages] by Jeff Leek in Johns Hopkins University.
* [http://r-pkgs.had.co.nz/ R packages] book by Hadley Wickham.
* [http://kbroman.org/pkg_primer/ R package primer] a minimal tutorial from Karl Broman.
* [https://datascienceplus.com/how-to-make-and-share-an-r-package-in-3-steps/ How to make and share an R package in 3 steps] (6/14/2017)
* [https://github.com/jennybc/pkg-dev-tutorial Package Development tutorial for useR! 2019 Toulouse]
* [https://devguide.ropensci.org/ rOpenSci Packages: Development, Maintenance, and Peer Review]
* [https://b-rodrigues.github.io/modern_R/package-development.html Package development] from "Modern R with the tidyverse"
* [https://youtu.be/qxRSzDejea4 How to write your own R package] (video, 10 minutes)


== Package structure ==
=== import() and importFrom() ===
http://r-pkgs.had.co.nz/package.html. On Linux/macOS, use '''tree -d DIRNAME''' to show the directories only. At a minimum, we will have '''R''' and '''man''' directories.
If our package depends on a package, we need to made some changes. Below we assume the package ''glmnet'' in our new package.


* ChangeLog
* DESCRIPTION: '''Imports: glmnet '''
* DESCRIPTION
* NAMESPACE: either '''import(glmnet)''' to import all functions from glmnet or '''importFrom(glmnet, cv.glmnet)''' to import 'cv.glmnet' only
* MD5
* hello.R: nothing needs to be added
* NAMESPACE
* R/
** zzz.R
* build/
** Package.pdf (eg [https://github.com/cran/dplyr/tree/master/build dplyr])
** vignette.rds
* data/
* demo/
* inst/
** extdata/
** doc/
*** FileName.R
*** FileName.Rmd
*** FileName.html
** include/
** othersYouInclude/
** tinytest/
** CITATION
* man/
** figures/
* src/
* tests/
** testthat
* vignettes/


=== NAMESPACE and DESCRIPTION ===
For more resource, see
[https://stackoverflow.com/a/13261139 Namespace dependencies not required]. ''If you use import or importFrom in your NAMESPACE file, you should have an entry for that package in the Imports section of your DESCRIPTION file (unless there is a reason that you need to use Depends in which case the package should have an entry in Depends, and not Imports).''
* https://r-pkgs.org/namespace.html
* http://r-pkgs.had.co.nz/namespace.html


=== license ===
== R package suggests ==
* [https://stackoverflow.com/a/38549043 What kind of license is the best license for an R package?]
[https://cran.r-project.org/web/packages/stringr/index.html stringr] has suggested '''htmlwidgets'''. An error will come out if the suggested packages are not available.
* https://cran.r-project.org/web/licenses/
<syntaxhighlight lang='rsplus'>
* https://r-pkgs.org/description.html#description-license
> library(stringr)
* [https://win-vector.com/2019/07/30/some-notes-on-gnu-licenses-in-r-packages/ Some Notes on GNU Licenses in R Packages]
> str_view(c("abc", "a.c", "bef"), "a\\.c")
Error in loadNamespace(name) : there is no package called ‘htmlwidgets’
</syntaxhighlight>


== Install software for PDF output ==
== Useful functions for accessing files in packages ==
* Windows: [http://miktex.org/download Miktex] and [https://pandoc.org/installing.html pandoc] ([http://rprogramming.net/create-html-or-pdf-files-with-r-knitr-miktex-and-pandoc/ Create HTML or PDF Files with R, Knitr, MiKTeX, and Pandoc])
* [https://stat.ethz.ch/R-manual/R-devel/library/base/html/system.file.html system.file()]
* Unix: [https://tug.org/texlive/ TeX Live] ('''sudo apt-get install texlive-full''') and [https://pandoc.org/installing.html pandoc]
* [https://stat.ethz.ch/R-manual/R-devel/library/base/html/find.package.html path.package() & find.package()]. Note that path.package() requires the package to be loaded.
* Mac: [https://tug.org/mactex/ MacTex]
* [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/file.path file.path()]. This is not related to packages.
* [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/normalizePath normalizePath()]. This is not related to packages.
{{Pre}}
> system.file(package = "batr")
[1] "f:/batr"
> system.file("extdata", "logo.png", package = "cowplot") # Mac
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/cowplot/extdata/logo.png"


== Windows: Rtools ==
> path.package("batr")
[https://thecoatlessprofessor.com/programming/installing-rtools-for-compiled-code-via-rcpp/ Installing RTools for Compiled Code via Rcpp]. Just remember to check the option to include some paths in the '''PATH''' environment variable.
[1] "f:\\batr"
> path.package("ggplot2") # Mac
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/ggplot2"


== R CMD ==
# sometimes it returns the forward slash format for some reason; C:/Program Files/R/R-3.4.0/library/batr
[https://cran.r-project.org/doc/manuals/R-admin.html R Installation and Administration]
# so it is best to add normalizePath().
> normalizePath(path.package("batr"))


* R CMD build XXX. Note this will not create pdf files for vignettes. The output is a tarball.
> file.path("f:", "git", "surveyor")
* R CMD INSTALL
[1] "f:/git/surveyor"
* R CMD REMOVE
</pre>
* R CMD SHLIB files. For example, "Rcmd shlib *.f *.c -o surv.dll" on Windows.
* R CMD make
* R CMD check XXX. Useful if we want to create reference manual (PDF file). See [https://stackoverflow.com/a/16987795 R create reference manual with R CMD check].
* R CMD javareconf


== usethis package ==
== Internal functions ==
* https://usethis.r-lib.org/. '''usethis''' is a workflow package: it automates repetitive tasks that arise during project setup and development, both for R packages and non-package projects.
* [https://blog.r-hub.io/2019/12/12/internal-functions/ Internal functions in R packages]
** [https://youtu.be/79s3z0gIuFU?t=705 R Package Development 1: Where to Start] by John Muschelli. [https://www.youtube.com/playlist?list=PLk3B5c8iCV-T4LM0mwEyWIunIunLyEjqM R Package Development playlist].
* [https://statisticaloddsandends.wordpress.com/2021/06/10/documentation-for-internal-functions/ Documentation for internal functions]
** [https://usethis.r-lib.org/reference/create_package.html create_package(path)] with the package name of your choice.
** [https://javirudolph.github.io/RLadies-Gainesville-FL/20190429-Rudolph-packages/apr29presentation.html#1 Sharing with others and helping yourself] (slides, use right click menu to save as an HTML file)
* [https://www.tidyverse.org/articles/2019/04/usethis-1.5.0/ usethis 1.5.0]
* [https://www.tidyverse.org/blog/2020/04/usethis-1-6-0/ usethis 1.6.0]
* [https://www.tidyverse.org/blog/2020/12/usethis-2-0-0/ usethis 2.0.0]


=== Github Actions ===
== RStudio shortcuts ==
To use the '''Github Actions''' for continuous integration/CI checks,
[[File:RStudio Build.png|200px]]
* I first follow [https://ropenscilabs.github.io/actions_sandbox/packageci.html this] to run ''usethis::use_github_action_check_release()''. Once I commit and push the files to Github, Github Actions are kicked off. The R console also tell me to copy and paste a line to add a workflow's badge to README.md.
* Then I modify the yaml file to become [https://github.com/r-lib/actions/blob/master/examples/check-standard.yaml this] to run a standard check. This took 3m24s to run.
* Then I further delete the 'devel' line (line 23) to reduce one more platform to check. This took 3m to check. My example is on [https://github.com/arraytools/rtoy/actions Github] (rtoy).


I also try to follow [https://ropenscilabs.github.io/actions_sandbox/websites-using-pkgdown-bookdown-and-blogdown.html Github Actions with R] and create a [https://github.com/r-lib/pkgdown pkgdown]/package documentation web page.
== available package ==
* '''usethis::use_github_actions("pkgdown")''' Now go to Github repo's Settings -> Options and scroll down until you see ''Github Pages''. For ''Source'', the page site should be set to being built from the root folder of the ''gh-pages''.
[https://cran.r-project.org/web/packages/available/index.html available]. Check if a given package name is available to use. It checks the name's validity. Checks if it is used on 'GitHub', 'CRAN' and 'Bioconductor'.
* I have used Jekyll to create a gh-page. I don't need to delete anything for this new gh-pages. I just need to go to the repository setting and (scroll down until we see Github Pages) change the Source of Github Pages to 'gh-pages branch' from 'master branch'.
* The files on the gh-pages branch are generated by Github Actions; these files are not available on my local machine. My location machine only has '''.github/workflows/pkgdown.yaml''' file.


Question:
== Create an R package ==
* The workflow file specifies R version and OS platform.
[https://www.pipinghotdata.com/posts/2020-10-25-your-first-r-package-in-1-hour/?s=09 Your first R package in 1 hour]
* Right now the workflow file (like pkgdown) is using "r-lib/actions/setup-r@master"  that has an "action.yml" file. The r-version is '3.x' only. What about if R 4.0.0 is released?


=== Packages, webpages and Github ===
=== Using usethis ===
[https://debruine.github.io/tutorials/index.html Tutorials] by Lisa DeBruine
[https://trinostics.blogspot.com/2020/04/new-r-package-foo-updated.html New R Package 'foo' -- Updated]
# setwd()
# usethis::create_package("foo")
# usethis::use_git(); usethis_github()
# usethis::use_mit_license("Your name")
# usethis::use_r("foo_function")
# usethis::use_package("dplyr")  # specify import dependency, e.g. dplyr package
# usethis::use_testthat()
# usethis::use_test("firsttest")
# setwd("..")
# roxygen2::roxygenise()


== biocthis ==
=== Using [https://github.com/hadley/devtools devtools] and [http://cran.r-project.org/web/packages/roxygen2/index.html roxygen2] ===
[https://lcolladotor.github.io/biocthis/ biocthis], [https://speakerdeck.com/lcolladotor/biocthis-tab slides]


== R package depends vs imports ==
* https://www.r-project.org/nosvn/pandoc/devtools.html
* http://stackoverflow.com/questions/8637993/better-explanation-of-when-to-use-imports-depends
* A useful [http://thepoliticalmethodologist.com/2014/08/14/building-and-maintaining-r-packages-with-devtools-and-roxygen2/ post] by Jacob Montgomery. Watch the [https://www.youtube.com/watch?v=9PyQlbAEujY#t=19 youtube video] there. The process requires 3 components: RStudio software, devtools and roxygen2 (creating documentation from R code) packages.
* http://stackoverflow.com/questions/9893791/imports-and-depends
* [https://uoftcoders.github.io/studyGroup/lessons/r/packages/lesson/ MAKING PACKAGES IN R USING DEVTOOLS]
* https://stat.ethz.ch/pipermail/r-devel/2013-August/067082.html
* [http://r-pkgs.had.co.nz/r.html R code workflow] from Hadley Wickham.
* [https://blog.r-hub.io/2020/04/29/maintenance/ Workflow automation tools for package developers]
* [https://jozefhajnala.gitlab.io/r/r102-addin-roxytags/ RStudio:addins part 2 - roxygen documentation formatting made easy]
** Inserting a skeleton - Do this by placing your cursor anywhere in the function you want to document and click Code Tools -> Insert Roxygen Skeleton
* [http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf Instructions for Creating Your Own R Package]. It includes creating a R package with functions written in C++ via Rcpp helper function.
* [https://www.rstudio.com/wp-content/uploads/2015/06/devtools-cheatsheet.pdf devtools cheatsheet] (2 pages)
* My first R package [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-1/ Part 1], [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-2/ Part 2], [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-3/ Part 3]. It uses 3 packages: usethis, roxygen2 and devtools.
* [https://twitter.com/JennyBryan/status/1364288709265215489?s=20 If you love devtools::load_all(), pkgload is where the magic actually happens]. [https://r-lib.github.io/pkgload/news/index.html pkgload] package.


In the namespace era Depends is never really needed. All modern packages have no technical need for Depends anymore. Loosely speaking the only purpose of Depends today is to expose other package's functions to the user without re-exporting them.
How to use [http://rstudio-pubs-static.s3.amazonaws.com/2556_4e9f1c2af93b4683a19e2303a52bb2d5.html devtools::load_all("FolderName")]. load_all() loads any modified R files, and recompile and reload any modified C or Fortran files.
{{Pre}}
# Step 1
library(devtools)


load = functions exported in myPkg are available to interested parties as myPkg::foo or via direct imports - essentially this means the package can now be used
# Step 2
dir.create(file.path("MyCode", "R"), recursive = TRUE)
cat("foo=function(x){x*2}", file = file.path("MyCode", "R", "foo.R"))
write.dcf(list(Package = "MyCode", Title = "My Code for this project", Description = "To tackle this problem",
    Version = "0.0", License = "For my eyes only", Author = "First Last <[email protected]>",
    Maintainer = "First Last <[email protected]>"), file = file.path("MyCode", "DESCRIPTION"))
# OR
# create("path/to/package/pkgname")
# create() will create R/ directory, DESCRIPTION and NAMESPACE files.


attach = the namespace (and thus all exported functions) is attached to the search path - the only effect is that you have now added the exported functions to the global pool of functions - sort of like dumping them in the workspace (for all practical purposes, not technically)
# Step 3 (C/Fortran code, optional)
dir.create(file.path("MyCode", "src"))
cat("void cfoo(double *a, double *b, double *c){*c=*a+*b;}\n", file = file.path("MyCode",
    "src", "cfoo.c"))
cat("useDynLib(MyCode)\n", file = file.path("MyCode", "NAMESPACE"))


import a function into a package = make sure that this function works in my package regardless of the search path (so I can write fn1 instead of pkg1::fn1 and still know it will come from pkg1 and not someone's workspace or other package that chose the same name)
# Step 4
load_all("MyCode")


------------------------------------------------------------------------
# Step 5
* https://stat.ethz.ch/pipermail/r-devel/2013-September/067451.html
# Modify R/C/Fortran code and run load_all("MyCode")


The distinction is between "loading" and "attaching" a package. Loading
# Step 6 (Automatically generate the documentation from R source code,  
it (which would be done if you had MASS::loglm, or imported it)
#        in the man/ folder and export the function in NAMESPACE file.
guarantees that the package is initialized and in memory, but doesn't
#        optional, repeat if any function's help has changed)
make it visible to the user without the explicit MASS:: prefix.
document("MyCode")
Attaching it first loads it, then modifies the user's search list so the
user can see it.


Loading is less intrusive, so it's preferred over attaching.  Both
# Step 7 (check the package, optional)
library() and require() would attach it.
check("MyCode")


=== import() and importFrom() ===
# Step 8 (Deployment, create a tarball,
If our package depends on a package, we need to made some changes. Below we assume the package ''glmnet'' in our new package.
#        optional, repeat if necessary)
build("MyCode")


* DESCRIPTION: '''Imports: glmnet '''
# Step 9 (Install the package, optional)
* NAMESPACE: either '''import(glmnet)''' to import all functions from glmnet or '''importFrom(glmnet, cv.glmnet)''' to import 'cv.glmnet' only
install("MyCode")
* hello.R: nothing needs to be added
</pre>


For more resource, see
'''Note''':
* https://r-pkgs.org/namespace.html
# '''load_all("FolderName")''' will make the FolderName to become ''like'' a package to be loaded into the current R session so the 2nd item returned from '''search()''' will be '''"package:FolderName"'''. However, the ''FolderName'' does not exist under Program Files/R/R-X.Y.Z/library nor Documents/R/win-library/X.Y/ (Windows OS).
* http://r-pkgs.had.co.nz/namespace.html
# '''build("FolderName")''' will create a tarball in the current directory. User can install the new package for example using Packages -> Install packages from local files on Windows OS. This will build/run vignettes so it may take some time. The tarball will contain a '''build''' folder containing 'vignette.rds' file. It'll also create a new folder '''inst/doc''' containing 3 files (MyPkg.html, MyPkg.Rmd and MyPkg.R). The vignettes may contain a new folder '''MyPkg_cache''' if we use chunk = TRUE option in Rmd file. Note install.packages() will not run the R code in vignettes.
# For the simplest R package, the source code only contains a file <DESCRIPTION> and a folder <R> with individual R files in the text format.


== R package suggests ==
=== Using RStudio ===
[https://cran.r-project.org/web/packages/stringr/index.html stringr] has suggested '''htmlwidgets'''. An error will come out if the suggested packages are not available.
<ul>
<syntaxhighlight lang='rsplus'>
<li>[https://support.rstudio.com/hc/en-us/articles/200486508-Building-Testing-and-Distributing-Packages Building, Testing, and Distributing Packages] from RStudio. <BR>
> library(stringr)
[[File:RstudioBuild.png|100px]]
> str_view(c("abc", "a.c", "bef"), "a\\.c")
</li>
Error in loadNamespace(name) : there is no package called ‘htmlwidgets’
</ul>
</syntaxhighlight>
* Youtube
 
** [https://youtu.be/qxRSzDejea4 How to write your own R package] from IDG TECHtalk
== Useful functions for accessing files in packages ==
** [https://www.youtube.com/playlist?list=PLk3B5c8iCV-T4LM0mwEyWIunIunLyEjqM R Package Development playlist] by John Muschelli.
* [https://stat.ethz.ch/R-manual/R-devel/library/base/html/system.file.html system.file()]
** https://youtu.be/Mwe64ziVQXA which does not use RStudio to create a new package
* [https://stat.ethz.ch/R-manual/R-devel/library/base/html/find.package.html path.package() & find.package()]. Note that path.package() requires the package to be loaded.
* [https://medium.com/@shiansu/how-to-create-and-distribute-an-r-package-a296217435dc How to Create and Distribute an R Package].  
* [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/file.path file.path()]. This is not related to packages.
** The goal of the article to set up an easily installable R package on Github for others to use via remotes::install_github().
* [https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/normalizePath normalizePath()]. This is not related to packages.
** The main tools required are RStudio along with the packages roxygen2 and usethis.
{{Pre}}
** How to take care of Bioconductor and Github Dependencies
> system.file(package = "batr")
** How to publish your package to Github
[1] "f:/batr"
* [https://technistema.com/posts/building-a-corporate-r-package-for-pleasure-and-profit/ Building a Corporate R Package for Pleasure and Profit]
> system.file("extdata", "logo.png", package = "cowplot") # Mac
* Toy example*
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/cowplot/extdata/logo.png"
*# In RStudio, click "File" - "New Project" - "New Directory" - "R package". Package name = "a1" (this naming will guarantee this package will be shown on top of all R packages in RStudio). Press 'Create Project'. This new project folder will have necessary files for an R package including DESCRIPTION, NAMESPACE, R/hello.R, man/hello.Rd. The "hello.R" file will be opened in RStudio automatically.
*# Create a new R file under a1/R folder. An example of this R file <add.R> containing roxygen comments can be found under [http://r-pkgs.had.co.nz/man.html here]. Pressing Ctrl/Cmd + Shift + D (or running '''devtools::document()''') will generate a man/add.Rd.  
*# In R, type '''usethis::use_vignette("my-vignette") ''' to create a new vignette. The new vignette "my-vignette.Rmd" will be saved under "a1/vignettes" subfolder. We can modify the Rmd file as we need.
*# In RStudio, click "Build" - "Build Source Package". You will see some messages on the "Build" tab of the top-right panel. Eventually, a tarball "/home/$USERNAME/a1_0.1.0.tar.gz" is created (I create the project under /home/$USERNAME directory).
*# We can install the package in R '''install.packages("~/a1_0.1.0.tar.gz", repos= NULL, type = "source")''' if we have already created the tarball. Another method is to use RStudio "Build" - "Install and Restart" (Ctrl + Shift + B).
*# In RStudio, type '''help(package = "a1") ''' or click "Packages" tab on the bottom-right panel and click "a1" package. It will show a line "User guides, package vignettes and other documentation". The vignette we just created will be available in HTML, source and R code format.


> path.package("batr")
=== Using RStudio.cloud ===
[1] "f:\\batr"
There is a problem. We can use devtools::create("/cloud/project") to create a new package. When it builds the source package, the package file will be located in the root directory of the package. However, if we use a local RStudio to create a source package, the source package will be located in the upper directory.
> path.package("ggplot2") # Mac
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/ggplot2"


# sometimes it returns the forward slash format for some reason; C:/Program Files/R/R-3.4.0/library/batr
== Binary packages ==
# so it is best to add normalizePath().
* No .R files in the ''R/'' directory. There are 3 files that store the parsed functions in an efficient file format. This is the result of loading all the R code and then saving the functions with ''save()''.
> normalizePath(path.package("batr"))
* A ''Meta/'' directory contains a number of Rds files. These files contain cached metadata about the package, like what topics the help files cover and parsed version of the ''DESCRIPTION'' file.
* An ''html/'' directory.
* ''libs/'' directory if you have any code in the ''src/' directory
* The contents of ''inst/'' are moved to the top-level directory.


> file.path("f:", "git", "surveyor")
=== Building the tarball ===
[1] "f:/git/surveyor"
* No matter we uses '''devtools::build()''' or the terminal '''R CMD build MyPkg''' it will uses run the R code in vignette. Be cautious on the extra time and storage the process incurred.
</pre>
* If 'cache = TRUE' is used in vignettes, it will create a new subfolder called MyuPkg_cache under the vignettes folder. This takes a lot of space (eg 1GB in some case).


== Internal functions ==
=== Building the binary ===
* [https://blog.r-hub.io/2019/12/12/internal-functions/ Internal functions in R packages]
<pre>
* [https://statisticaloddsandends.wordpress.com/2021/06/10/documentation-for-internal-functions/ Documentation for internal functions]
R CMD INSTALL --build MyPkg.tar.gz
# OR
R CMD INSTALL --build Full_Path_Of_MyPkg
</pre>
The binary (on Windows) can be installed by '''install.packages("Mypkg.zip",repos=NULL)'''


== RStudio shortcuts ==
If the installation is successful, it will overwrite any existing installation of the same package. To prevent changes to the present working installation or to provide an install location with write access, create a suitably located directory with write access and use the -l option to build the package in the chosen location.
[[File:RStudio Build.png|200px]]
<pre>
R CMD INSTALL -l location --build pkg
</pre>


== available package ==
== R folder ==
[https://cran.r-project.org/web/packages/available/index.html available]. Check if a given package name is available to use. It checks the name's validity. Checks if it is used on 'GitHub', 'CRAN' and 'Bioconductor'.
* https://r-pkgs.org/r.html
* zzz.R. .onLoad() function.
* [https://stackoverflow.com/a/20223846 R: How to run some code on load of package?]


== Create an R package ==
See an example from DuoClustering2018.
[https://www.pipinghotdata.com/posts/2020-10-25-your-first-r-package-in-1-hour/?s=09 Your first R package in 1 hour]
<pre>
 
#' @importFrom utils read.csv
=== Using usethis ===
.onLoad <- function(libname, pkgname) {
[https://trinostics.blogspot.com/2020/04/new-r-package-foo-updated.html New R Package 'foo' -- Updated]
  fl <- system.file("extdata", "metadata.csv", package = "DuoClustering2018")
# setwd()
  titles <- utils::read.csv(fl, stringsAsFactors = FALSE)$Title
# usethis::create_package("foo")
  ExperimentHub::createHubAccessors(pkgname, titles)
# usethis::use_git(); usethis_github()
}
# usethis::use_mit_license("Your name")
</pre>
# usethis::use_r("foo_function")
Note that the environment of a function from the DuoClustering2018 package is not the package name.
# usethis::use_package("dplyr")  # specify import dependency, e.g. dplyr package
<pre>
# usethis::use_testthat()
environment(clustering_summary_filteredExpr10_TrapnellTCC_v2)
# usethis::use_test("firsttest")
<environment: 0x7fe01dbe7dd0>
# setwd("..")
</pre>
# roxygen2::roxygenise()
Q: where is the definition of DuoClustering2018::clustering_summary_filteredExpr10_TrapnellTCC_v2()? A: [https://master.bioconductor.org/packages/release/bioc/vignettes/AnnotationHub/inst/doc/CreateAHubPackage.html#building-the-package createHubAccessors() - Creating A Hub Package: ExperimentHub or AnnotationHub].


=== Using [https://github.com/hadley/devtools devtools] and [http://cran.r-project.org/web/packages/roxygen2/index.html roxygen2] ===
== data ==
http://r-pkgs.had.co.nz/data.html


* https://www.r-project.org/nosvn/pandoc/devtools.html
Three ways to include data in your package.
* A useful [http://thepoliticalmethodologist.com/2014/08/14/building-and-maintaining-r-packages-with-devtools-and-roxygen2/ post] by Jacob Montgomery. Watch the [https://www.youtube.com/watch?v=9PyQlbAEujY#t=19 youtube video] there. The process requires 3 components: RStudio software, devtools and roxygen2 (creating documentation from R code) packages.
* If you want to store binary data and make it available to the user, put it in '''data/'''. This is the best place to put example datasets.
* [https://uoftcoders.github.io/studyGroup/lessons/r/packages/lesson/ MAKING PACKAGES IN R USING DEVTOOLS]
* If you want to store parsed data, but not make it available to the user, put it in '''R/sysdata.rda'''. This is the best place to put data that your functions need.
* [http://r-pkgs.had.co.nz/r.html R code workflow] from Hadley Wickham.
<ul>
* [https://blog.r-hub.io/2020/04/29/maintenance/ Workflow automation tools for package developers]
<li>If you want to store raw data, put it in '''inst/extdata'''. See [https://r-pkgs.org/data.html#data-extdata External data]. An [https://bioconductor.org/packages/release/data/experiment/vignettes/tximportData/inst/doc/tximportData.html example] from tximportData package.
* [https://jozefhajnala.gitlab.io/r/r102-addin-roxytags/ RStudio:addins part 2 - roxygen documentation formatting made easy]
<pre>
** Inserting a skeleton - Do this by placing your cursor anywhere in the function you want to document and click Code Tools -> Insert Roxygen Skeleton
# grep -r extdata /home/brb/R/x86_64-pc-linux-gnu-library/4.0
* [http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf Instructions for Creating Your Own R Package]. It includes creating a R package with functions written in C++ via Rcpp helper function.
logo_file <- system.file("extdata", "logo.png", package = "cowplot")
* [https://www.rstudio.com/wp-content/uploads/2015/06/devtools-cheatsheet.pdf devtools cheatsheet] (2 pages)
orgDBLoc = system.file("extdata", "org.Hs.eg.sqlite", package="org.Hs.eg.db")
* My first R package [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-1/ Part 1], [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-2/ Part 2], [https://qualityandinnovation.com/2019/10/13/my-first-r-package-part-3/ Part 3]. It uses 3 packages: usethis, roxygen2 and devtools.
 
* [https://twitter.com/JennyBryan/status/1364288709265215489?s=20 If you love devtools::load_all(), pkgload is where the magic actually happens]. [https://r-lib.github.io/pkgload/news/index.html pkgload] package.
# grep -r readRDS /home/brb/R/x86_64-pc-linux-gnu-library/4.0
patient.data  <- readRDS("assets/coxnet.RDS")
</pre>
</li>
</ul>


How to use [http://rstudio-pubs-static.s3.amazonaws.com/2556_4e9f1c2af93b4683a19e2303a52bb2d5.html devtools::load_all("FolderName")]. load_all() loads any modified R files, and recompile and reload any modified C or Fortran files.
[https://blog.r-hub.io/2020/05/29/distribute-data/ How to distribute data with your R package]
{{Pre}}
# Step 1
library(devtools)


# Step 2
== Rd file ==
dir.create(file.path("MyCode", "R"), recursive = TRUE)
[https://github.com/wviechtb/mathjaxr Using Mathjax in Rd Files]
cat("foo=function(x){x*2}", file = file.path("MyCode", "R", "foo.R"))
write.dcf(list(Package = "MyCode", Title = "My Code for this project", Description = "To tackle this problem",
    Version = "0.0", License = "For my eyes only", Author = "First Last <[email protected]>",
    Maintainer = "First Last <noname@example.com>"), file = file.path("MyCode", "DESCRIPTION"))
# OR
# create("path/to/package/pkgname")
# create() will create R/ directory, DESCRIPTION and NAMESPACE files.


# Step 3 (C/Fortran code, optional)
== Vignette ==
dir.create(file.path("MyCode", "src"))
* [https://lifehacker.com/how-to-create-vignettes-that-really-pull-a-room-togethe-1846703615 How to Create Vignettes That Really Pull a Room Together]
cat("void cfoo(double *a, double *b, double *c){*c=*a+*b;}\n", file = file.path("MyCode",
* See examples from packages like [https://github.com/cran/magrittr magrittr] (Rmd, jpg) or [https://github.com/cran/tidyr tidyr] (Rmd, csv) or [https://github.com/cran/rvest rvest] (Rmd, png) or [https://github.com/mikelove/DESeq2/blob/master/vignettes/DESeq2.Rmd DESeq2].
    "src", "cfoo.c"))
* Many vignette uses '''output: rmarkdown::html_vignette''' which will expand the width to the page. [https://github.com/mikelove/rnaseqGene/blob/master/vignettes/rnaseqGene.Rmd This one] uses '''output: BiocStyle::html_document''' which will have a [https://bioconductor.org/packages/release/workflows/vignettes/rnaseqGene/inst/doc/rnaseqGene.html narrow width].
cat("useDynLib(MyCode)\n", file = file.path("MyCode", "NAMESPACE"))
* [http://www.markvanderloo.eu/yaRb/2019/01/11/add-a-static-pdf-vignette-to-an-r-package/ Add a static pdf vignette to an R package]
* [https://www.rdocumentation.org/packages/devtools/versions/2.2.0/topics/build_vignettes devtools::build_vignettes()].  The files are copied in the 'doc' directory and an vignette index is created in 'Meta/vignette.rds', as they would be in a built package.
* [https://stackoverflow.com/a/37692810 build_vignette can't find functions]
 
Long execution for R code in vignette
* [https://community.rstudio.com/t/caching-chunks-in-rstudio-does-not-work/9040/7 Caching chunks in RStudio does not work]
* [https://rmflight.github.io/posts/2014/07/vignetteAnalysis.html caching doesn't seem to work here (i.e. when used in a document that is a vignette)]
* [https://trinostics.blogspot.com/2020/05/how-to-add-vignette-to-package-in.html How to Add a Vignette to a Package in RStudio]
* [https://blog.r-hub.io/2020/06/03/vignettes/ Optimal workflows for package vignettes]


# Step 4
== NEWS ==
load_all("MyCode")
[https://blog.r-hub.io/2020/05/08/pkg-news/ Why and how maintain a NEWS file for your R package?]


# Step 5
== README.Rmd & README.md files ==
# Modify R/C/Fortran code and run load_all("MyCode")
See [http://r-pkgs.had.co.nz/release.html Releasing a package] from R packages by Hadley Wickham.


# Step 6 (Automatically generate the documentation from R source code,
[https://stackoverflow.com/a/31914830 How to convert .Rmd into .md in R studio?]
#        in the man/ folder and export the function in NAMESPACE file.
#        optional, repeat if any function's help has changed)
document("MyCode")


# Step 7 (check the package, optional)
Example: [https://github.com/tidyverse/ggplot2 ggplot2] repository at Github
check("MyCode")


# Step 8 (Deployment, create a tarball,
It seems RStudio cannot create TOC for *.md files. [https://github.com/cran/glmnet/blob/master/vignettes/glmnet_beta.Rmd glmnet] package creates [https://cran.r-project.org/web/packages/glmnet/vignettes/glmnet_beta.pdf TOC] of its vignette by itself. [[Text_editor#Markdown_TOC_.28table_of_contents.29_extension|Visual Studio Code]] has an extension to do that.
#        optional, repeat if necessary)
 
build("MyCode")
=== badge ===
[https://github.com/guangchuangyu/badger badger]: Badge for R Package


# Step 9 (Install the package, optional)
== tests folder and testthat package==
install("MyCode")
* [http://r-pkgs.had.co.nz/tests.html testthat] package.
</pre>
** [https://journal.r-project.org/archive/2011/RJ-2011-002/RJ-2011-002.pdf testthat: Get Started with Testing] 2011
** [https://codingclubuc3m.rbind.io/post/2019-11-26/ Automated testing with 'testthat' in practice]
** Lots of packages suggest testthat; for example [https://cran.r-project.org/web/packages/princurve/index.html princurve], [https://github.com/cran/glmnetUtils/blob/master/tests/testthat/test1_glmnet.R glmnetUtils].
** [https://stackoverflow.com/a/52175513 How to run a package's testthat tests]
** [https://support.rstudio.com/hc/en-us/articles/200486508-Building-Testing-and-Distributing-Packages Building, Testing, and Distributing Packages] from RStudio
** '''Run Tests''' button. See [https://rstudio.com/products/rstudio/release-notes/ RStudio release notes] from RStudio v1.2 - April 8, 2019.
** [https://ugoproto.github.io/ugo_r_doc/working_with_rstudio_ide/ Working with the RStudio IDE]
** [http://www.win-vector.com/blog/2019/03/unit-tests-in-r/ Unit Tests in R] 2019
** The result of '''dput''' can be used in expect_equal().
** [https://statisticaloddsandends.wordpress.com/2020/05/11/a-small-gotcha-when-comparing-lists-using-testthat/ A small gotcha when comparing lists using testthat]


'''Note''':
== .Rbuildignore ==
# '''load_all("FolderName")''' will make the FolderName to become ''like'' a package to be loaded into the current R session so the 2nd item returned from '''search()''' will be '''"package:FolderName"'''. However, the ''FolderName'' does not exist under Program Files/R/R-X.Y.Z/library nor Documents/R/win-library/X.Y/ (Windows OS).
[https://blog.r-hub.io/2020/05/20/rbuildignore/ Non-standard files/directories, Rbuildignore and inst]
# '''build("FolderName")''' will create a tarball in the current directory. User can install the new package for example using Packages -> Install packages from local files on Windows OS. This will build/run vignettes so it may take some time. The tarball will contain a '''build''' folder containing 'vignette.rds' file. It'll also create a new folder '''inst/doc''' containing 3 files (MyPkg.html, MyPkg.Rmd and MyPkg.R). The vignettes may contain a new folder '''MyPkg_cache''' if we use chunk = TRUE option in Rmd file. Note install.packages() will not run the R code in vignettes.
# For the simplest R package, the source code only contains a file <DESCRIPTION> and a folder <R> with individual R files in the text format.


=== Using RStudio ===
== URL checker ==
<ul>
* [https://blog.r-hub.io/2020/12/01/url-checks/ A NOTE on URL checks of your R package]
<li>[https://support.rstudio.com/hc/en-us/articles/200486508-Building-Testing-and-Distributing-Packages Building, Testing, and Distributing Packages] from RStudio. <BR>
* [https://github.com/r-lib/urlchecker urlchecker] package
[[File:RstudioBuild.png|100px]]
</li>
</ul>
* Youtube
** [https://youtu.be/qxRSzDejea4 How to write your own R package] from IDG TECHtalk
** [https://www.youtube.com/playlist?list=PLk3B5c8iCV-T4LM0mwEyWIunIunLyEjqM R Package Development playlist] by John Muschelli.
** https://youtu.be/Mwe64ziVQXA which does not use RStudio to create a new package
* [https://medium.com/@shiansu/how-to-create-and-distribute-an-r-package-a296217435dc How to Create and Distribute an R Package].
** The goal of the article to set up an easily installable R package on Github for others to use via remotes::install_github().
** The main tools required are RStudio along with the packages roxygen2 and usethis.
** How to take care of Bioconductor and Github Dependencies
** How to publish your package to Github
* [https://technistema.com/posts/building-a-corporate-r-package-for-pleasure-and-profit/ Building a Corporate R Package for Pleasure and Profit]
* Toy example*
*# In RStudio, click "File" - "New Project" - "New Directory" - "R package". Package name = "a1" (this naming will guarantee this package will be shown on top of all R packages in RStudio). Press 'Create Project'. This new project folder will have necessary files for an R package including DESCRIPTION, NAMESPACE, R/hello.R, man/hello.Rd. The "hello.R" file will be opened in RStudio automatically.
*# Create a new R file under a1/R folder. An example of this R file <add.R> containing roxygen comments can be found under [http://r-pkgs.had.co.nz/man.html here]. Pressing Ctrl/Cmd + Shift + D (or running '''devtools::document()''') will generate a man/add.Rd.
*# In R, type '''usethis::use_vignette("my-vignette") ''' to create a new vignette. The new vignette "my-vignette.Rmd" will be saved under "a1/vignettes" subfolder. We can modify the Rmd file as we need.
*# In RStudio, click "Build" - "Build Source Package". You will see some messages on the "Build" tab of the top-right panel. Eventually, a tarball "/home/$USERNAME/a1_0.1.0.tar.gz" is created (I create the project under /home/$USERNAME directory).
*# We can install the package in R '''install.packages("~/a1_0.1.0.tar.gz", repos= NULL, type = "source")''' if we have already created the tarball. Another method is to use RStudio "Build" - "Install and Restart" (Ctrl + Shift + B).
*# In RStudio, type '''help(package = "a1") ''' or click "Packages" tab on the bottom-right panel and click "a1" package. It will show a line "User guides, package vignettes and other documentation". The vignette we just created will be available in HTML, source and R code format.


=== Using RStudio.cloud ===
== What is a library? ==
There is a problem. We can use devtools::create("/cloud/project") to create a new package. When it builds the source package, the package file will be located in the root directory of the package. However, if we use a local RStudio to create a source package, the source package will be located in the upper directory.
A library is simply a directory containing installed packages.


== Binary packages ==
You can use ''.libPaths()'' to see which libraries are currently active.
* No .R files in the ''R/'' directory. There are 3 files that store the parsed functions in an efficient file format. This is the result of loading all the R code and then saving the functions with ''save()''.
<syntaxhighlight lang='rsplus'>
* A ''Meta/'' directory contains a number of Rds files. These files contain cached metadata about the package, like what topics the help files cover and parsed version of the ''DESCRIPTION'' file.
.libPaths()
* An ''html/'' directory.
* ''libs/'' directory if you have any code in the ''src/' directory
* The contents of ''inst/'' are moved to the top-level directory.


=== Building the tarball ===
lapply(.libPaths(), dir)
* No matter we uses '''devtools::build()''' or the terminal '''R CMD build MyPkg''' it will uses run the R code in vignette. Be cautious on the extra time and storage the process incurred.
</syntaxhighlight>
* If 'cache = TRUE' is used in vignettes, it will create a new subfolder called MyuPkg_cache under the vignettes folder. This takes a lot of space (eg 1GB in some case).


=== Building the binary ===
== Object names ==
<pre>
* Variable and function names should be lower case.
R CMD INSTALL --build MyPkg.tar.gz
* Use an underscore (_) to separate words within a name (reserve . for S3 methods).  
# OR
* [https://en.wikipedia.org/wiki/Camel_case Camel case] is a legitimate alternative, but be consistent! For example, preProcess(), twoClassData, createDataPartition(), trainingRows, trainPredictors, testPredictors, trainClasses, testClasses have been used in [https://cran.r-project.org/web/packages/AppliedPredictiveModeling/index.html Applied Predictive Modeling] by [http://appliedpredictivemodeling.com/ Kuhn & Johnson].  
R CMD INSTALL --build Full_Path_Of_MyPkg
* Generally, variable names should be nouns and function names should be verb.
</pre>
The binary (on Windows) can be installed by '''install.packages("Mypkg.zip",repos=NULL)'''


If the installation is successful, it will overwrite any existing installation of the same package. To prevent changes to the present working installation or to provide an install location with write access, create a suitably located directory with write access and use the -l option to build the package in the chosen location.  
== Spacing ==
<pre>
* Add a space around the operators +, -, \ and *.  
R CMD INSTALL -l location --build pkg
* Include a space around the assignment operators, <- and =.
</pre>
* Add a space around any comparison operators such as == and <.


== R folder ==
== Indentation ==
* https://r-pkgs.org/r.html
* Use two spaces to indent code.  
* zzz.R. .onLoad() function.
* Never mix tabs and spaces.  
* [https://stackoverflow.com/a/20223846 R: How to run some code on load of package?]
* RStudio can automatically convert the tab character to spaces (see Tools -> Global options -> Code).


See an example from DuoClustering2018.
== \dontrun{} ==
<pre>
* [https://cran.r-project.org/doc/manuals/R-exts.html Writing R Extensions > Documenting functions] and search for '''dontrun'''
#' @importFrom utils read.csv
* [https://stackoverflow.com/questions/12038160/how-to-not-run-an-example-using-roxygen2 How to not run an example using roxygen2?]
.onLoad <- function(libname, pkgname) {
* [https://stackoverflow.com/questions/1454211/what-does-not-run-mean-in-r-help-pages What does "not run" mean in R help pages?] See ?example .
  fl <- system.file("extdata", "metadata.csv", package = "DuoClustering2018")
  titles <- utils::read.csv(fl, stringsAsFactors = FALSE)$Title
  ExperimentHub::createHubAccessors(pkgname, titles)
}
</pre>
Note that the environment of a function from the DuoClustering2018 package is not the package name.
<pre>
environment(clustering_summary_filteredExpr10_TrapnellTCC_v2)
<environment: 0x7fe01dbe7dd0>
</pre>
Q: where is the definition of DuoClustering2018::clustering_summary_filteredExpr10_TrapnellTCC_v2()? A: [https://master.bioconductor.org/packages/release/bioc/vignettes/AnnotationHub/inst/doc/CreateAHubPackage.html#building-the-package createHubAccessors() - Creating A Hub Package: ExperimentHub or AnnotationHub].


== data ==
== formatR and lintr package ==
http://r-pkgs.had.co.nz/data.html
Use formatR package to clean up poorly formatted code
<syntaxhighlight lang='rsplus'>
install.packages("formatR")
formatR::tidy_dir("R")
</syntaxhighlight>


Three ways to include data in your package.
Another way is to use the [https://github.com/jimhester/lintr lintr] package ([https://en.wikipedia.org/wiki/Lint_(software) lint]).
* If you want to store binary data and make it available to the user, put it in '''data/'''. This is the best place to put example datasets.
<syntaxhighlight lang='rsplus'>
* If you want to store parsed data, but not make it available to the user, put it in '''R/sysdata.rda'''. This is the best place to put data that your functions need.
install.packages("lintr")
<ul>
lintr:::lin_package()
<li>If you want to store raw data, put it in '''inst/extdata'''. See [https://r-pkgs.org/data.html#data-extdata External data]. An [https://bioconductor.org/packages/release/data/experiment/vignettes/tximportData/inst/doc/tximportData.html example] from tximportData package.
</syntaxhighlight>
<pre>
# grep -r extdata /home/brb/R/x86_64-pc-linux-gnu-library/4.0
logo_file <- system.file("extdata", "logo.png", package = "cowplot")
orgDBLoc = system.file("extdata", "org.Hs.eg.sqlite", package="org.Hs.eg.db")


# grep -r readRDS /home/brb/R/x86_64-pc-linux-gnu-library/4.0
== Rcpp ==
patient.data  <- readRDS("assets/coxnet.RDS")
[https://arxiv.org/abs/1911.06416 Thirteen Simple Steps for Creating An R Package with an External C++ Library]
</pre>
</li>
</ul>


[https://blog.r-hub.io/2020/05/29/distribute-data/ How to distribute data with your R package]
== C library ==
[https://working-with-data.mazamascience.com/2021/07/16/using-r-packaging-a-c-library-in-15-minutes/ Using R — Packaging a C library in 15 minutes]


== Rd file ==
== Data package ==
[https://github.com/wviechtb/mathjaxr Using Mathjax in Rd Files]
[https://rstudio4edu.github.io/rstudio4edu-book/data-pkg.html Chapter 12 Create a data package] from rstudio4edu


== Vignette ==
== Minimal R package for submission ==
* [https://lifehacker.com/how-to-create-vignettes-that-really-pull-a-room-togethe-1846703615 How to Create Vignettes That Really Pull a Room Together]
https://stat.ethz.ch/pipermail/r-devel/2013-August/067257.html and [http://cran.r-project.org/web/packages/policies.html CRAN Repository Policy].
* See examples from packages like [https://github.com/cran/magrittr magrittr] (Rmd, jpg) or [https://github.com/cran/tidyr tidyr] (Rmd, csv) or [https://github.com/cran/rvest rvest] (Rmd, png) or [https://github.com/mikelove/DESeq2/blob/master/vignettes/DESeq2.Rmd DESeq2].
* Many vignette uses '''output: rmarkdown::html_vignette''' which will expand the width to the page. [https://github.com/mikelove/rnaseqGene/blob/master/vignettes/rnaseqGene.Rmd This one] uses '''output: BiocStyle::html_document''' which will have a [https://bioconductor.org/packages/release/workflows/vignettes/rnaseqGene/inst/doc/rnaseqGene.html narrow width].
* [http://www.markvanderloo.eu/yaRb/2019/01/11/add-a-static-pdf-vignette-to-an-r-package/ Add a static pdf vignette to an R package]
* [https://www.rdocumentation.org/packages/devtools/versions/2.2.0/topics/build_vignettes devtools::build_vignettes()]. The files are copied in the 'doc' directory and an vignette index is created in 'Meta/vignette.rds', as they would be in a built package.
* [https://stackoverflow.com/a/37692810 build_vignette can't find functions]


Long execution for R code in vignette
== Create R Windows Binary on non-Windows OS ==
* [https://community.rstudio.com/t/caching-chunks-in-rstudio-does-not-work/9040/7 Caching chunks in RStudio does not work]
* [https://stackoverflow.com/a/16745596 Create R Windows Binary from .tar.gz linux]
* [https://rmflight.github.io/posts/2014/07/vignetteAnalysis.html caching doesn't seem to work here (i.e. when used in a document that is a vignette)]
* [http://win-builder.r-project.org/ win-builder] from r-project.org
* [https://trinostics.blogspot.com/2020/05/how-to-add-vignette-to-package-in.html How to Add a Vignette to a Package in RStudio]
* [https://blog.r-hub.io/2020/06/03/vignettes/ Optimal workflows for package vignettes]


== NEWS ==
== r-hub/rhub package: the R package builder service ==
[https://blog.r-hub.io/2020/05/08/pkg-news/ Why and how maintain a NEWS file for your R package?]
https://github.com/r-hub/proposal, [https://www.r-bloggers.com/2024/04/r-hub-v2/ R-hub v2] 2024/4.


== README.Rmd & README.md files ==
* [https://blog.r-hub.io/2019/04/08/rhub-1.1.1/ rhub 1.1.1 is on CRAN!] 2019/4/8
See [http://r-pkgs.had.co.nz/release.html Releasing a package] from R packages by Hadley Wickham.
* [https://blog.r-hub.io/2019/03/26/why-care/ R package developers, why should you care about R-hub?] 2019/3/26
* [https://r-hub.github.io/rhub/articles/local-debugging.html Local Linux checks with Docker]
* https://www.rstudio.com/resources/videos/r-hub-overview/
* http://blog.revolutionanalytics.com/2016/10/r-hub-public-beta.html
* [https://jozef.io/r107-multiplatform-gitlabci-rhub/ Setting up continuous multi-platform R package building, checking and testing with R-Hub, Docker and GitLab CI/CD for free, with a working example] 2019/4/27
* [https://blog.r-hub.io/2019/05/29/keep-up-with-cran/ How to keep up with CRAN policies and processes?] 2019/5/29
* [https://blog.r-hub.io/2019/12/19/r-hub-usage/ R-hub usage in a few figures]


[https://stackoverflow.com/a/31914830 How to convert .Rmd into .md in R studio?]
{{Pre}}
# Today 1/1/2020
$ git clone https://github.com/arraytools/rtoy.git
$ rm -rf rtoy/.git
$ rm rtoy/.gitignore rtoy/_config.yml
$ R
> install.packages("devtools", repos = "https://cran.rstudio.com")
> install.packages("rhub")
> devtools::install_github("r-hub/sysreqs") #needed before calling local_check_linux()


Example: [https://github.com/tidyverse/ggplot2 ggplot2] repository at Github
> pkg_path <- "~/Downloads/rtoy"
> chk <- local_check_linux(pkg_path, image = "rhub/debian-gcc-release")


It seems RStudio cannot create TOC for *.md files. [https://github.com/cran/glmnet/blob/master/vignettes/glmnet_beta.Rmd glmnet] package creates [https://cran.r-project.org/web/packages/glmnet/vignettes/glmnet_beta.pdf TOC] of its vignette by itself. [[Text_editor#Markdown_TOC_.28table_of_contents.29_extension|Visual Studio Code]] has an extension to do that.
─  Building package


=== badge ===
Container name: 6dca434d-84f9-42e2-ab83-b8c364594476-2
[https://github.com/guangchuangyu/badger badger]: Badge for R Package
It will _not_ be removed after the check.


== tests folder and testthat package==
R-hub Linux builder script v0.10.0 (c) R Consortium, 2018-2019
* [http://r-pkgs.had.co.nz/tests.html testthat] package.
** [https://journal.r-project.org/archive/2011/RJ-2011-002/RJ-2011-002.pdf testthat: Get Started with Testing] 2011
** [https://codingclubuc3m.rbind.io/post/2019-11-26/ Automated testing with 'testthat' in practice]
** Lots of packages suggest testthat; for example [https://cran.r-project.org/web/packages/princurve/index.html princurve], [https://github.com/cran/glmnetUtils/blob/master/tests/testthat/test1_glmnet.R glmnetUtils].
** [https://stackoverflow.com/a/52175513 How to run a package's testthat tests]
** [https://support.rstudio.com/hc/en-us/articles/200486508-Building-Testing-and-Distributing-Packages Building, Testing, and Distributing Packages] from RStudio
** '''Run Tests''' button. See [https://rstudio.com/products/rstudio/release-notes/ RStudio release notes] from RStudio v1.2 - April 8, 2019.
** [https://ugoproto.github.io/ugo_r_doc/working_with_rstudio_ide/ Working with the RStudio IDE]
** [http://www.win-vector.com/blog/2019/03/unit-tests-in-r/ Unit Tests in R] 2019
** The result of '''dput''' can be used in expect_equal().
** [https://statisticaloddsandends.wordpress.com/2020/05/11/a-small-gotcha-when-comparing-lists-using-testthat/ A small gotcha when comparing lists using testthat]


== .Rbuildignore ==
Package: /tmp/RtmpviOCT1/file470972a3c4ab/rtoy_0.1.0.tar.gz
[https://blog.r-hub.io/2020/05/20/rbuildignore/ Non-standard files/directories, Rbuildignore and inst]
Docker image: rhub/debian-gcc-release
Env vars:
R CMD check arguments:
Unable to find image 'rhub/debian-gcc-release:latest' locally
latest: Pulling from rhub/debian-gcc-release
Digest: sha256:a9e01ca57bfd44f20eb6719f0bfecdd8cf0f59610984342598a53f11555b515d
Status: Downloaded newer image for rhub/debian-gcc-release:latest
Sysreqs platform: linux-x86_64-debian-gcc
No system requirements


== URL checker ==
>>>>>==================== Installing system requirements
* [https://blog.r-hub.io/2020/12/01/url-checks/ A NOTE on URL checks of your R package]
8fa4f66e41954b4ed1112eb72e76683c28345863fcc7e260edac6a9a30387fed
* [https://github.com/r-lib/urlchecker urlchecker] package


== What is a library? ==
>>>>>==================== Starting Docker container
A library is simply a directory containing installed packages.
2dfae75c9de95b598ff8d9cd19d3cfc7ffe7f16edbc3d4f81c896e4e6b956ebd
ls: cannot access '/opt/R-*': No such file or directory
> source('https://bioconductor.org/biocLite.R')
Error: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install
Execution halted
Error in run(bash, c(file.path(wd, "rhub-linux.sh"), args), echo = TRUE,  :
  System command error
</pre>
Moreover, it create a new Docker image and a new Docker container. We need to manually clean them:-(


You can use ''.libPaths()'' to see which libraries are currently active.
Running a check on its own (remote) server works
<syntaxhighlight lang='rsplus'>
{{Pre}}
.libPaths()
> check_on_linux("rtoy") # will run remotely.
    # We need to verify the email and enter a token.
    # We will get a report and a full build log.
    # The report includes both the linux command and the log from the server.
</pre>


lapply(.libPaths(), dir)
Now I go back to the original method.
</syntaxhighlight>
{{Pre}}
$ R
> install.packages("tinytex")
> tinytex::install_tinytex()
> q()
$ exit


== Object names ==
$ sudo apt-get install texinfo
* Variable and function names should be lower case.
$ R CMD build rtoy
* Use an underscore (_) to separate words within a name (reserve . for S3 methods).
$ R CMD check rtoy_0.1.0.tar.gz
* [https://en.wikipedia.org/wiki/Camel_case Camel case] is a legitimate alternative, but be consistent! For example, preProcess(), twoClassData, createDataPartition(), trainingRows, trainPredictors, testPredictors, trainClasses, testClasses have been used in [https://cran.r-project.org/web/packages/AppliedPredictiveModeling/index.html Applied Predictive Modeling] by [http://appliedpredictivemodeling.com/ Kuhn & Johnson].
* Generally, variable names should be nouns and function names should be verb.


== Spacing ==
# Install pandoc
* Add a space around the operators +, -, \ and *.  
$ wget https://github.com/jgm/pandoc/releases/download/2.9.1/pandoc-2.9.1-1-amd64.deb
* Include a space around the assignment operators, <- and =.  
$ sudo dpkg -i pandoc-2.9.1-1-amd64.deb
* Add a space around any comparison operators such as == and <.
$ R CMD check --as-cran rtoy_0.1.0.tar.gz
# Ignore a 'Note' https://stackoverflow.com/a/23831508
</pre>


== Indentation ==
== rcmdcheck ==
* Use two spaces to indent code.
Run R CMD check from R and Capture Results
* Never mix tabs and spaces.
* RStudio can automatically convert the tab character to spaces (see Tools -> Global options -> Code).


== \dontrun{} ==
The [https://github.com/r-lib/rcmdcheck rcmdcheck] package was used by [https://github.com/r-lib/actions/tree/master/examples Github Actions for R language] from r-lib/R infrastructure.
* [https://cran.r-project.org/doc/manuals/R-exts.html Writing R Extensions > Documenting functions] and search for '''dontrun'''
* [https://stackoverflow.com/questions/12038160/how-to-not-run-an-example-using-roxygen2 How to not run an example using roxygen2?]
* [https://stackoverflow.com/questions/1454211/what-does-not-run-mean-in-r-help-pages What does "not run" mean in R help pages?] See ?example .


== formatR and lintr package ==
== CRAN check API ==
Use formatR package to clean up poorly formatted code
* [https://blog.r-hub.io/2019/06/10/cran-checks-api/ Overview of the CRAN checks API] 2019/6/10
<syntaxhighlight lang='rsplus'>
* [https://ropensci.org/technotes/2019/10/09/cran-checks-api-update/ update] 2019/10/9
install.packages("formatR")
formatR::tidy_dir("R")
</syntaxhighlight>


Another way is to use the [https://github.com/jimhester/lintr lintr] package ([https://en.wikipedia.org/wiki/Lint_(software) lint]).
== Continuous Integration ==
<syntaxhighlight lang='rsplus'>
=== [https://travis-ci.org/ Travis-CI] (Linux, Mac) ===
install.packages("lintr")
* [http://juliasilge.com/blog/Beginners-Guide-to-Travis/ A Beginner's Guide to Travis-CI]
lintr:::lin_package()
* http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html#61_travis
</syntaxhighlight>


== Rcpp ==
=== Continuous Integration: [https://www.appveyor.com/ Appveyor] (Windows) ===
[https://arxiv.org/abs/1911.06416 Thirteen Simple Steps for Creating An R Package with an External C++ Library]
* Appveyor is a continuous integration service that builds projects on Windows machines.
* http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html#62_appveyor


== C library ==
=== Github Actions ===
[https://working-with-data.mazamascience.com/2021/07/16/using-r-packaging-a-c-library-in-15-minutes/ Using R — Packaging a C library in 15 minutes]
* See [[#usethis_package|usethis]] package.
* [https://jozef.io/r922-github-actions-r-packages/ Automating R package checks across platforms with GitHub Actions and Docker in a portable way]. No need to use "usethis" package.


== Data package ==
=== precommit ===
[https://rstudio4edu.github.io/rstudio4edu-book/data-pkg.html Chapter 12 Create a data package] from rstudio4edu
* https://cran.r-project.org/web/packages/precommit/index.html
* [https://lorenzwalthert.netlify.app/post/pre-commit-hooks-for-r/ precommit initial CRAN release]


== Minimal R package for submission ==
== Submit packages to cran ==
https://stat.ethz.ch/pipermail/r-devel/2013-August/067257.html and [http://cran.r-project.org/web/packages/policies.html CRAN Repository Policy].
* https://cran.r-project.org/submit.html
* http://f.briatte.org/r/submitting-packages-to-cran
* [https://rmhogervorst.nl/cleancode/blog/2016/07/09/content/post/2016-07-09-submtting-to-cran-first-experience/ Submitting your first package to CRAN, my experience]
* [https://jef.works/blog/2018/06/18/get-your-package-on-cran-in-10-steps/ Get your R package on CRAN in 10 steps] June 2018
* [http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html Preparing Your Package for for Submission] by John Muschelli
* https://builder.r-hub.io/. See [[R#Introducing_R-hub_.28rhub_package.29.2C_the_R_package_builder_service|here]].
* [https://neonira.github.io/op2 The most annoying warning for CRAN submission]
* [https://www.mzes.uni-mannheim.de/socialsciencedatalab/article/r-package/ How to write your own R package and publish it on CRAN] '''devtools::release()''', July 2020
* [https://blog.r-hub.io/2020/12/01/url-checks/?s=09 A NOTE on URL checks of your R package]
* [http://r-pkgs.had.co.nz/release.html Releasing a package] from the book "R packages" by Hadley Wickham.
* [https://www.johnmackintosh.net/blog/2021-11-23-cran-success/ I got my first package onto CRAN, and YOU CAN TOO]
* [https://twitter.com/PipingHotData/status/1554560631382200320?s=20&t=FSsEd2IzMdOuK1iA8iXZHg Submitting vroom to CRAN, LIVE!]


== Create R Windows Binary on non-Windows OS ==
=== Windows ===
* [https://stackoverflow.com/a/16745596 Create R Windows Binary from .tar.gz linux]
[https://blog.r-hub.io/2020/04/01/win-builder/ Everything you should know about WinBuilder]
* [http://win-builder.r-project.org/ win-builder] from r-project.org


== r-hub/rhub package: the R package builder service ==
== Other tips/advice ==
https://github.com/r-hub/proposal
*[https://martinctc.github.io/blog/make-package-even-more-awesome/ Top 10 tips to make your R package even more awesome]
* [https://github.com/MangoTheCat/goodpractice GoodPractice] Advice on R Package Building.


* [https://blog.r-hub.io/2019/04/08/rhub-1.1.1/ rhub 1.1.1 is on CRAN!] 2019/4/8
= C/Fortran =
* [https://blog.r-hub.io/2019/03/26/why-care/ R package developers, why should you care about R-hub?] 2019/3/26
== Rmath.h ==
* [https://r-hub.github.io/rhub/articles/local-debugging.html Local Linux checks with Docker]
For example pnorm5() was used by [https://github.com/cran/survival/commit/3f7bbd52fc24f3fb744c5ca82946291e0735811e#diff-c5e184f4d68df5830324d0d15600667cR6 survS.h] by survival package (old version) .
* https://www.rstudio.com/resources/videos/r-hub-overview/
* http://blog.revolutionanalytics.com/2016/10/r-hub-public-beta.html
* [https://jozef.io/r107-multiplatform-gitlabci-rhub/ Setting up continuous multi-platform R package building, checking and testing with R-Hub, Docker and GitLab CI/CD for free, with a working example] 2019/4/27
* [https://blog.r-hub.io/2019/05/29/keep-up-with-cran/ How to keep up with CRAN policies and processes?] 2019/5/29
* [https://blog.r-hub.io/2019/12/19/r-hub-usage/ R-hub usage in a few figures]


{{Pre}}
== Packages includes Fortran ==
# Today 1/1/2020
* [https://github.com/cran/survC1/tree/master/src survC1]
$ git clone https://github.com/arraytools/rtoy.git
* [https://github.com/cran/nlme/tree/master/src nlme]
$ rm -rf rtoy/.git
* [https://github.com/cran/glmnet/tree/master/src glmnet]
$ rm rtoy/.gitignore rtoy/_config.yml
* [https://github.com/cran/bsamGP/tree/master/src bsamGP] (FORTRAN 90). This is searched by using https://www.r-pkg.org/. I am not able to get what I need using the website.
$ R
> install.packages("devtools", repos = "https://cran.rstudio.com")
> install.packages("rhub")
> devtools::install_github("r-hub/sysreqs") #needed before calling local_check_linux()


> pkg_path <- "~/Downloads/rtoy"
On mac, gfortran (6.1) can be downloaded from [https://cran.rstudio.com/bin/macosx/tools CRAN]. It will be installed onto /usr/local/gfortran. Note that the binary will not be present in PATH. So we need to run the following command to make gfortran avaiialbe.
> chk <- local_check_linux(pkg_path, image = "rhub/debian-gcc-release")
<syntaxhighlight lang='bash'>
sudo ln -s /usr/local/gfortran/bin/gfortran /usr/local/bin/gfortran
</syntaxhighlight>


Building package
A useful tool to find R packages containing Fortran code is [https://github.com/r-hub/pkgsearch pkgsearch] package. Note
* The result is not a comprehensive list of packages containing Fortran code.
* It seems the result is the same as I got from https://www.r-pkg.org
<pre>
> pkg_search("Fortran")
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #    package      version by                    @ title
  1 100 covr        3.3.2  Jim Hester          12d Test Coverage for Packages
  2  91 inline      0.3.15  Dirk Eddelbuettel    1y Functions to Inline C, ...
  3  43 randomForest 4.6.14  Andy Liaw            2y Breiman and Cutler's Ra...
  4  39 deSolve      1.24    Thomas Petzoldt      4M Solvers for Initial Val...
  5 27 mnormt      1.5.5  Adelchi Azzalini    3y The Multivariate Normal...
  6  26 minqa        1.2.4  Katharine M. Mullen  5y Derivative-free optimiz...
  7  24 rgcvpack    0.1.4  Xianhong Xie        6y R Interface for GCVPACK...
  8  22 leaps        3.0    Thomas Lumley        3y Regression Subset Selec...
  9  21 akima        0.6.2  Albrecht Gebhardt    3y Interpolation of Irregu...
10  20 rootSolve    1.7    Karline Soetaert    3y Nonlinear Root Finding,...


Container name: 6dca434d-84f9-42e2-ab83-b8c364594476-2
> more()
It will _not_ be removed after the check.
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #    package    version  by                    @ title
11 15 BB        2019.10.1 Paul Gilbert        11d Solving and Optimizing L...
12 15 limSolve  1.5.5.3  Karline Soetaert    2y Solving Linear Inverse M...
13 14 insideRODE 2.0      YUZHUO PAN          7y insideRODE includes buil...
14 13 earth      5.1.1    Stephen Milborrow    7M Multivariate Adaptive Re...
15 13 cluster    2.1.0    Martin Maechler      4M "Finding Groups in Data"...
16 13 spam      2.3.0.2  ORPHANED            11h SPArse Matrix
17 12 diptest    0.75.7    Martin Maechler      4y Hartigan's Dip Test Stat...
18 10 pbivnorm  0.6.0    Brenton Kenkel      5y Vectorized Bivariate Nor...
19  7 optmatch  0.9.12    Mark M. Fredrickson 17d Functions for Optimal Ma...
20  7 lsei      1.2.0    Yong Wang            2y Solving Least Squares or...
</pre>


R-hub Linux builder script v0.10.0 (c) R Consortium, 2018-2019
Another way is to run rsync to download src/contrib directory from CRAN and then use grep to find these packages. Note: the source packages takes about 8GB space (2019-10-28).
<pre>
mkdir ~/Downloads/cran
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/*.tar.gz ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES.gz ~/Downloads/cran/
cd ~/Downloads/cran
find . -xtype l -delete # remove broken symbolic links


Package: /tmp/RtmpviOCT1/file470972a3c4ab/rtoy_0.1.0.tar.gz
touch tmp
Docker image: rhub/debian-gcc-release
for f in *.gz;
Env vars:
do
R CMD check arguments:
  tar -tzvf $f | grep -E "(\.f|\.f90|\.f95)$"  |& tee -a tmp
Unable to find image 'rhub/debian-gcc-release:latest' locally
done
latest: Pulling from rhub/debian-gcc-release
</pre>
Digest: sha256:a9e01ca57bfd44f20eb6719f0bfecdd8cf0f59610984342598a53f11555b515d
to check if the tarball contains Fortran 77/90 code.
Status: Downloaded newer image for rhub/debian-gcc-release:latest
 
Sysreqs platform: linux-x86_64-debian-gcc
Now to see all packages names we can process it in R
No system requirements
{{Pre}}
library(magrittr)
library(stringr)
x <- read.table("~/Downloads/tmp", stringsAsFactors = F)
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% unique() # 415 packages
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% table() %>% sort() # how many Fortran files in each package


>>>>>==================== Installing system requirements
str_subset(x$V6, "\\.f90$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 90 only packages
8fa4f66e41954b4ed1112eb72e76683c28345863fcc7e260edac6a9a30387fed
str_subset(x$V6, "\\.f95$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 95 only packages


>>>>>==================== Starting Docker container
str_subset(x$V6, "BayesFM") # f95
2dfae75c9de95b598ff8d9cd19d3cfc7ffe7f16edbc3d4f81c896e4e6b956ebd
ls: cannot access '/opt/R-*': No such file or directory
> source('https://bioconductor.org/biocLite.R')
Error: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install
Execution halted
Error in run(bash, c(file.path(wd, "rhub-linux.sh"), args), echo = TRUE,  :
  System command error
</pre>
</pre>
Moreover, it create a new Docker image and a new Docker container. We need to manually clean them:-(


Running a check on its own (remote) server works
= Misc =
{{Pre}}
== Datasets in R packages ==
> check_on_linux("rtoy") # will run remotely.  
https://vincentarelbundock.github.io/Rdatasets/datasets.html
    # We need to verify the email and enter a token.
    # We will get a report and a full build log.
    # The report includes both the linux command and the log from the server.
</pre>


Now I go back to the original method.
== Turn your analysis into a package ==
{{Pre}}
[https://twitter.com/neilgcurrie/status/1572254358397681665 Organise your own analysis]
$ R
> install.packages("tinytex")
> tinytex::install_tinytex()
> q()
$ exit


$ sudo apt-get install texinfo
== Build R package faster using multicore ==
$ R CMD build rtoy
http://www.rexamine.com/2015/07/speeding-up-r-package-installation-process/
$ R CMD check rtoy_0.1.0.tar.gz


# Install pandoc
The idea is edit the '''/lib64/R/etc/Renviron''' file (where /lib64/R/etc/ is the result to a call to the R.home() function in R) and set:
$ wget https://github.com/jgm/pandoc/releases/download/2.9.1/pandoc-2.9.1-1-amd64.deb
<pre>
$ sudo dpkg -i pandoc-2.9.1-1-amd64.deb
MAKE='make -j 8' # submit 8 jobs at once
$ R CMD check --as-cran rtoy_0.1.0.tar.gz
</pre>
# Ignore a 'Note' https://stackoverflow.com/a/23831508
Then build R package as regular, for example,
<pre>
$ time R CMD INSTALL ~/R/stringi --preclean --configure-args='--disable-pkg-config'
</pre>
</pre>


== rcmdcheck ==
== suppressPackageStartupMessages() and .onAttach() ==
Run R CMD check from R and Capture Results
[https://github.com/cran/KernSmooth/blob/master/R/all.R KernSmooth] package example.


The [https://github.com/r-lib/rcmdcheck rcmdcheck] package was used by [https://github.com/r-lib/actions/tree/master/examples Github Actions for R language] from r-lib/R infrastructure.
[http://www.win-vector.com/blog/2019/08/it-is-time-for-cran-to-ban-package-ads/ It is Time for CRAN to Ban Package Ads]


== CRAN check API ==
<syntaxhighlight lang='rsplus'>
* [https://blog.r-hub.io/2019/06/10/cran-checks-api/ Overview of the CRAN checks API] 2019/6/10
suppressPackageStartupMessages(library("dplyr"))
* [https://ropensci.org/technotes/2019/10/09/cran-checks-api-update/ update] 2019/10/9
</syntaxhighlight>


== Continuous Integration ==
== fusen package ==
=== [https://travis-ci.org/ Travis-CI] (Linux, Mac) ===
* [https://www.r-bloggers.com/2021/08/fusen-is-now-available-on-cran/ {fusen} is now available on CRAN!]
* [http://juliasilge.com/blog/Beginners-Guide-to-Travis/ A Beginner's Guide to Travis-CI]
* (Videos) [https://youtu.be/febuOcS70t4?t=1742 How To Build A Package With The "Rmd First" Method], [https://youtu.be/febuOcS70t4?t=2788 fusen::inflate()]
* http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html#61_travis


=== Continuous Integration: [https://www.appveyor.com/ Appveyor] (Windows) ===
== Identifying R Functions & Packages Used in GitHub Repos ==
* Appveyor is a continuous integration service that builds projects on Windows machines.
[https://www.bryanshalloway.com/2022/01/18/identifying-r-functions-packages-used-in-github-repos/ Identifying R Functions & Packages Used in GitHub Repos]: [https://brshallo.github.io/funspotr/ funspotr] package
* http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html#62_appveyor


=== Github Actions ===
== CRANalerts ==
* See [[#usethis_package|usethis]] package.
* https://cranalerts.com/
* [https://jozef.io/r922-github-actions-r-packages/ Automating R package checks across platforms with GitHub Actions and Docker in a portable way]. No need to use "usethis" package.
* [https://deanattali.com/blog/cranalerts/ CRANalerts: Get email alerts when a CRAN package gets updated]


=== precommit ===
== R-universe ==
* https://cran.r-project.org/web/packages/precommit/index.html
* [https://ropensci.org/blog/2021/06/22/setup-runiverse/ How to create your personal CRAN-like repository on R-universe]
* [https://lorenzwalthert.netlify.app/post/pre-commit-hooks-for-r/ precommit initial CRAN release]
* [https://ropensci.org/blog/2023/05/31/runiverse-snapshots/ Downloading snapshots and creating stable R packages repositories using r-universe]
 
== Submit packages to cran ==
* https://cran.r-project.org/submit.html
* http://f.briatte.org/r/submitting-packages-to-cran
* [https://rmhogervorst.nl/cleancode/blog/2016/07/09/content/post/2016-07-09-submtting-to-cran-first-experience/ Submitting your first package to CRAN, my experience]
* [https://jef.works/blog/2018/06/18/get-your-package-on-cran-in-10-steps/ Get your R package on CRAN in 10 steps] June 2018
* [http://johnmuschelli.com/neuroc/getting_ready_for_submission/index.html Preparing Your Package for for Submission] by John Muschelli
* https://builder.r-hub.io/. See [[R#Introducing_R-hub_.28rhub_package.29.2C_the_R_package_builder_service|here]].
* [https://neonira.github.io/op2 The most annoying warning for CRAN submission]
* [https://www.mzes.uni-mannheim.de/socialsciencedatalab/article/r-package/ How to write your own R package and publish it on CRAN] '''devtools::release()''', July 2020
* [https://blog.r-hub.io/2020/12/01/url-checks/?s=09 A NOTE on URL checks of your R package]
* [http://r-pkgs.had.co.nz/release.html Releasing a package] from the book "R packages" by Hadley Wickham.
 
=== Windows ===
[https://blog.r-hub.io/2020/04/01/win-builder/ Everything you should know about WinBuilder]
 
== Other tips ==
[https://martinctc.github.io/blog/make-package-even-more-awesome/ Top 10 tips to make your R package even more awesome]
 
= C/Fortran =
== Rmath.h ==
For example pnorm5() was used by [https://github.com/cran/survival/commit/3f7bbd52fc24f3fb744c5ca82946291e0735811e#diff-c5e184f4d68df5830324d0d15600667cR6 survS.h] by survival package (old version) .
 
== Packages includes Fortran ==
* [https://github.com/cran/survC1/tree/master/src survC1]
* [https://github.com/cran/nlme/tree/master/src nlme]
* [https://github.com/cran/glmnet/tree/master/src glmnet]
* [https://github.com/cran/bsamGP/tree/master/src bsamGP] (FORTRAN 90). This is searched by using https://www.r-pkg.org/. I am not able to get what I need using the website.
 
On mac, gfortran (6.1) can be downloaded from [https://cran.rstudio.com/bin/macosx/tools CRAN]. It will be installed onto /usr/local/gfortran. Note that the binary will not be present in PATH. So we need to run the following command to make gfortran avaiialbe.
<syntaxhighlight lang='bash'>
sudo ln -s /usr/local/gfortran/bin/gfortran /usr/local/bin/gfortran
</syntaxhighlight>
 
A useful tool to find R packages containing Fortran code is [https://github.com/r-hub/pkgsearch pkgsearch] package. Note
* The result is not a comprehensive list of packages containing Fortran code.
* It seems the result is the same as I got from https://www.r-pkg.org
<pre>
> pkg_search("Fortran")
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #    package      version by                    @ title
  1 100 covr        3.3.2  Jim Hester          12d Test Coverage for Packages
  2  91 inline      0.3.15  Dirk Eddelbuettel    1y Functions to Inline C, ...
  3  43 randomForest 4.6.14  Andy Liaw            2y Breiman and Cutler's Ra...
  4  39 deSolve      1.24    Thomas Petzoldt      4M Solvers for Initial Val...
  5  27 mnormt      1.5.5  Adelchi Azzalini    3y The Multivariate Normal...
  6  26 minqa        1.2.4  Katharine M. Mullen  5y Derivative-free optimiz...
  7  24 rgcvpack    0.1.4  Xianhong Xie        6y R Interface for GCVPACK...
  8  22 leaps        3.0    Thomas Lumley        3y Regression Subset Selec...
  9  21 akima        0.6.2  Albrecht Gebhardt    3y Interpolation of Irregu...
10  20 rootSolve    1.7    Karline Soetaert    3y Nonlinear Root Finding,...
 
> more()
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #    package    version  by                    @ title
11 15 BB        2019.10.1 Paul Gilbert        11d Solving and Optimizing L...
12 15 limSolve  1.5.5.3  Karline Soetaert    2y Solving Linear Inverse M...
13 14 insideRODE 2.0      YUZHUO PAN          7y insideRODE includes buil...
14 13 earth      5.1.1    Stephen Milborrow    7M Multivariate Adaptive Re...
15 13 cluster    2.1.0    Martin Maechler      4M "Finding Groups in Data"...
16 13 spam      2.3.0.2  ORPHANED            11h SPArse Matrix
17 12 diptest    0.75.7    Martin Maechler      4y Hartigan's Dip Test Stat...
18 10 pbivnorm  0.6.0    Brenton Kenkel      5y Vectorized Bivariate Nor...
19  7 optmatch  0.9.12    Mark M. Fredrickson 17d Functions for Optimal Ma...
20  7 lsei      1.2.0    Yong Wang            2y Solving Least Squares or...
</pre>
 
Another way is to run rsync to download src/contrib directory from CRAN and then use grep to find these packages. Note: the source packages takes about 8GB space (2019-10-28).
<pre>
mkdir ~/Downloads/cran
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/*.tar.gz ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES.gz ~/Downloads/cran/
cd ~/Downloads/cran
find . -xtype l -delete # remove broken symbolic links
 
touch tmp
for f in *.gz;
do
  tar -tzvf $f | grep -E "(\.f|\.f90|\.f95)$"  |& tee -a tmp
done
</pre>
to check if the tarball contains Fortran 77/90 code.
 
Now to see all packages names we can process it in R
{{Pre}}
library(magrittr)
library(stringr)
x <- read.table("~/Downloads/tmp", stringsAsFactors = F)
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% unique() # 415 packages
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% table() %>% sort() # how many Fortran files in each package
 
str_subset(x$V6, "\\.f90$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 90 only packages
str_subset(x$V6, "\\.f95$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 95 only packages
 
str_subset(x$V6, "BayesFM") # f95
</pre>


= Misc =
== FDA ==
== Datasets in R packages ==
[https://www.r-consortium.org/blog/2021/12/08/successful-r-based-test-package-submitted-to-fda Successful R-based Test Package Submitted to FDA]
https://vincentarelbundock.github.io/Rdatasets/datasets.html
 
== Build R package faster using multicore ==
http://www.rexamine.com/2015/07/speeding-up-r-package-installation-process/
 
The idea is edit the '''/lib64/R/etc/Renviron''' file (where /lib64/R/etc/ is the result to a call to the R.home() function in R) and set:
<pre>
MAKE='make -j 8' # submit 8 jobs at once
</pre>
Then build R package as regular, for example,
<pre>
$ time R CMD INSTALL ~/R/stringi --preclean --configure-args='--disable-pkg-config'
</pre>
 
== suppressPackageStartupMessages() and .onAttach() ==
[https://github.com/cran/KernSmooth/blob/master/R/all.R KernSmooth] package example.
 
[http://www.win-vector.com/blog/2019/08/it-is-time-for-cran-to-ban-package-ads/ It is Time for CRAN to Ban Package Ads]
 
<syntaxhighlight lang='rsplus'>
suppressPackageStartupMessages(library("dplyr"))
</syntaxhighlight>
 
== fusen package ==
* [https://www.r-bloggers.com/2021/08/fusen-is-now-available-on-cran/ {fusen} is now available on CRAN!]
* [https://youtu.be/febuOcS70t4?t=1742 How To Build A Package With The "Rmd First" Method], [https://youtu.be/febuOcS70t4?t=2788 fusen::inflate()]
 
== CRANalerts ==
* https://cranalerts.com/
* [https://deanattali.com/blog/cranalerts/ CRANalerts: Get email alerts when a CRAN package gets updated]
 
== R-universe ==
[https://ropensci.org/blog/2021/06/22/setup-runiverse/ How to create your personal CRAN-like repository on R-universe]

Latest revision as of 14:07, 17 April 2024

R package management

Challenges in Package Management

Challenges in Package Management

Packages loaded at startup

getOption("defaultPackages")

How to install a new package

utils::install.packages()

  • By default, install.packages() will check versions and install uninstalled packages shown in 'Depends', 'Imports' , and 'LinkingTo' (DIL) fields. See R-exts manual.
  • Take advantage of Ncpus parameter in install.packages()
  • If we want to install packages listed in 'Suggests' field, we should specify it explicitly by using dependencies argument:
install.packages(XXXX, dependencies = c("Depends", "Imports", "Suggests", "LinkingTo"))
# OR
install.packages(XXXX, dependencies = TRUE)
For example, if I use a plain install.packages() command to install downloader package, it only installs 'digest' and 'downloader' packages. If I use
install.packages("downloader", dependencies=TRUE)
it will also install 'testhat' package.
  • Even a warning is given when some imports package is not available, it does not stop the installation (trying to install plotROC package on mac R 3.5.3). See also Warning: dependency ‘XXX’ is not available.
    > install.packages("plotROC")
    --- Please select a CRAN mirror for use in this session ---
    Warning: dependency ‘gridSVG’ is not available
    trying URL 'https://cloud.r-project.org/bin/macosx/el-capitan/contrib/3.5/plotROC_2.2.1.tgz'
    Content type 'application/x-gzip' length 1288370 bytes (1.2 MB)
    ==================================================
    downloaded 1.2 MB
    
    The downloaded binary packages are in
    	/var/folders/2q/slryb0rx4tj97t66v7l6pwvr_z6g3s/T//Rtmptrf2ZS/downloaded_packages
    > library(gridSVG)
    Error in library(gridSVG) : there is no package called ‘gridSVG’
    > install.packages("gridSVG", type = "source")
    Warning message:
    package ‘gridSVG’ is not available (for R version 3.5.3) 
    > ap <- available.packages()
    > "gridSVG" %in% rownames(ap) # [1] FALSE
    > dim(ap)  # [1] 14220    17
    > library(plotROC)
    Loading required package: ggplot2
Look at CRAN, we see the latest version (1.7-0) of gridSVG depends on the latest version of R (3.6.0). But why even the source is removed from the old version of R?
  • The install.packages function source code can be found in R -> src -> library -> utils -> R -> packages2.R file from Github repository (put 'install.packages' in the search box).

mirror

local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.r-project.org" 
       options(repos=r)
})
options("repos")

RStudio

On my mac,

  • install.packages() will install the packages to User Library '/Users/USERNAME/Library/R/3.6/library'
  • RStudio GUI method will install the packages to System Library '/Library/Frameworks/R.framework/Versions/3.6/Resources/library'. So if we want to remove a package, we need to specify the lib parameter in remove.packages() or clicking on the adjacent X icon to remove it.

utils:::menuInstallLocal() (Windows OS only)

Packages -> Install package(s) from local files...

It works on tarball file.

pacman: Install and load the package at the same time, installing temporarily

p_load() function from the pacman package.

An example of installing the purrr package.

Also used by BBC Visual and Data Journalism cookbook for R graphics

Also used by Biowulf/NIH

For bioconductor, use, for example,

pacman::p_load("BioManager")
pacman::p_load("DESeq2",try.bioconductor=TRUE,update.bioconductor=TRUE)

PS. Brute force method

mypackages<-c("ggplot2", "dplyr")
 
for (p in mypackages){
  cond <- suppressWarnings(!require(p, character.only = TRUE))
  if(cond){
    try(install.packages(p), silent = TRUE)
    library(p, character.only = TRUE)
  }
}

remotes

devtools depends on 92 (non-base) packages and remotes depends on none.

Download and install R packages stored in 'GitHub', 'BitBucket', or plain 'subversion' or 'git' repositories. This package is a lightweight replacement of the 'install_*' functions in 'devtools'.

To install a package from a local machines with all dependency, run remotes::install_local(path = "MyPackage.tar.gz", dependencies=TRUE) or devtools::install() though the later requires to untar the source first.

remotes::install_github() vs devtools::install_github()

See the answers. remotes is a lighter weight package for those who don't need/want all the other functionality in devtools. The remotes package says it copied most of its functionality from devtools, so the functions are likely the same.

devtools

pak

BiocManager

https://cran.r-project.org/web/packages/BiocManager/index.html

pkgman

pkgman: A fresh approach to package installation - Gábor Csárdi

ccache: Faster R package installation

Faster R package installation

Ubuntu

Posit Package Manager/RStudio Package Manager

  • PPM Admin Guide. It only supports Ubuntu (no Debian), RedHat, OpenSUSE.
  • Posit Package Manager for Linux R Binaries. Put the following in the <.Rprofile> file. If inside the RStudio, we need to quit the current R session first.
    options(HTTPUserAgent = sprintf(
      "R/%s R (%s)", 
      getRversion(), 
      paste(
        getRversion(), 
        R.version["platform"], 
        R.version["arch"], 
        R.version["os"]
      )
    ))
    
    .ppm <- "https://packagemanager.posit.co/cran/__linux__/jammy/latest"
    options(repos = c(CRAN = .ppm))
    
  • It provides pre-compiled binary packages for Linux and Windows OS, but not macOS. When enabled, RStudio Package Manager will serve the appropriate CRAN binaries to R users instead of the source packages, saving R users significant installation time.
  • It is similar to MRAN as it allows users to use particular a snapshot of CRAN as their active repositories within their R session
  • Once configured, users can access RStudio Package Manager through all their familiar tools including base R’s install.packages(), packrat, and devtools.
  • Announcing Public Package Manager and v1.1.6
  • Package Manager allows linux users to install pre-built binaries of the packages which will make install quicker. See an example here.
    # Freeze to Apr 29, 2021 8:00 PM
    options(repos = c(REPO_NAME = "https://packagemanager.rstudio.com/all/2639103"))
    
    # Using Linux Binary Packages
    https://r-pkgs.example.com/our-cran/__linux__/xenial/latest
    https://packagemanager.rstudio.com/cran/__linux__/focal/2021-04-23
    
  • Bring Your Own Binary Packages with RSPM
  • Docker images
    • r-base, rocker/r-base don't use PPM (as of R 4.3.3).
    • Bioconductor (source on github) does take advantage of PPM. In Bioc 3.17/R 4.3.1, Posit's PPM hosts the binaries. As of Bioc 3.18/R 4.3.3, Bioconductor hosts the binaries.
    RStudio will be available on your web browser at http://localhost:8787. The username is "rstudio".
    docker run \
     	-e PASSWORD=bioc \
     	-p 8787:8787 \
     	bioconductor/bioconductor_docker:devel
    R will be available on the terminal
    docker run -it --rm \
        --user rstudio \
        bioconductor/bioconductor_docker:RELEASE_3_18 R

install a package as it existed on a specific date/snapshot: mran repository

Look at https://cran.microsoft.com/snapshot/, it seems the snapshot went back 2014 for R 3.1.1.

For example,

options(repos = c(CRAN = "https://mran.microsoft.com/snapshot/2022-10-01"))

install.packages("xgboost", repos="https://mran.microsoft.com/snapshot/2022-10-01/")

This trick works great when I try to install an R package (glmnet 3.0-2) on an old version of R. The current glmnet requires R 3.6.0. So even the source package is not available for older versions of R. If I install a package that does not require a very new version of R, that it works. The same problem happens on Windows OS. (today is 2020-05-05)

$ docker run --rm -it r-base:3.5.3
...
> install.packages("glmnet")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning message:
package ‘glmnet’ is not available (for R version 3.5.3)
> install.packages("glmnet", repos="https://mran.microsoft.com/snapshot/2019-09-20/")
...
* DONE (glmnet)

The downloaded source packages are in
	‘/tmp/RtmpoWVWFQ/downloaded_packages’
> packageVersion("glmnet")
[1] ‘2.0.18’
> options()$repos
                         CRAN
"https://cloud.r-project.org"
> install.packages('knitr') # OK.

install a package on an old version of R

  • Currently R is 4.2.2
  • I have build R 4.1.3 and R 4.0.5 from source on Ubuntu 22.04. P.S. We need to include/enable "Source code" URIs Error :: You must put some 'source' URIs in your sources.list.
    sudo apt update
    sudo apt-get build-dep r-base
    
    tar -xzvf R-4.1.3.tar.gz
    cd R-4.1.3
    ./configure
    time make -j2
    bin/R
    
  • I'm trying to install DESEq2 on R 4.1.3 and R 4.0.5
  • On R 4.1.3, the installation is successful.
    install.packages("BiocManager")
    BiocManager::install("DESeq2")   
    packageVersion("DESeq2")  # 1.34.0
    
  • On R 4.0.5, the installation of "locfit" (current version 1.5-9.7) requires R >= 4.1.0. Check on CRAN, we see the locfit version is 1.5-6 for R 4.0.5 (2021-3-31). So I try this version first.
    install.packages("BiocManager")
    install.packages("remotes")
    remotes::install_version("locfit", "1.5-6")
    # Error: a 'NAMESPACE' file is required
    
    # Try the next version 1.5-7
    remotes::install_version("locfit", "1.5-7") # Works
    BiocManager::install("DESeq2")   # Works too
    packageVersion("DESeq2")  # 1.30.1
    
  • Now it is a good opportunity to try renv. So I create a new folder 'testProject' and create a new file 'test.R'. This R file contains only one line: library(DESeq2).
    > setwd("testProj")
    > install.packages("renv")
    > renv::init()
    * Initializing project ...
    * Discovering package dependencies ... Done!
    * Copying packages into the cache ... [79/79] Done!
    One or more Bioconductor packages are used in your project,
    but the BiocManager package is not available.
    
    Consider installing BiocManager before snapshot.
    
    The following package(s) will be updated in the lockfile:
    ...
    The version of R recorded in the lockfile will be updated:
    - R                      [*] -> [4.0.5]
    
    * Lockfile written to '~/Downloads/testProject/renv.lock'.
    * Project '~/Downloads/testProject' loaded. [renv 0.16.0]
    * renv activated -- please restart the R session.
    > renv::install("BiocManager")  # NOT SURE IF THIS IS NECESSARY
    Retrieving 'https://cloud.r-project.org/src/contrib/BiocManager_1.30.19.tar.gz' ...
    	OK [file is up to date]
    Installing BiocManager [1.30.19] ...
    	OK [built from source]
    Moving BiocManager [1.30.19] into the cache ...
    	OK [moved to cache in 20 milliseconds]
    > q()
    

    Now DESeq2 is in "renv.lock" file!

    Note if we accidentally start a different version of R on an renv-ed project based on an older version of R, we shall see some special message informing us The project library is out of sync with the lockfile. At this time, the "renv.lock" is not touched yet.

    # Bootstrapping renv 0.16.0 --------------------------------------------------
    * Downloading renv 0.16.0 ... OK (downloaded source)
    * Installing renv 0.16.0 ... Done!
    * Successfully installed and loaded renv 0.16.0.
    ! Using R 4.2.2 (lockfile was generated with R 4.0.5)
    * Project '~/Downloads/testProject' loaded. [renv 0.16.0]
    * The project library is out of sync with the lockfile.
    * Use `renv::restore()` to install packages recorded in the lockfile.
    
  • What happened if we start with the same version of R but have a different version of renv in the global environment (vs renv.lock)?
    install.packages("remotes")
    remotes::install_version("renv", "0.15.5")
    renv::init()
    q()
    
    # Start the same R from another directory and update renv to the latest version 0.16.0
    # Now go back to the project directory and start R to see what will happen?
    # Nothing....  This is because the same project has the old & same version of "renv" library 
    # in the "$PROJECT/renv" directory
    

    If we delete the "renv" subdirectory, ".Rprofile" file but keep "renv.lock" file, and start R (remember the 'renv' package version in the global R is different from the version in "renv.lock".

    > renv::restore()
    This project has not yet been activated.
    Activating this project will ensure the project library is used during restore.
    Please see `?renv::activate` for more details.
    
    Would you like to activate this project before restore? [Y/n]: y
    Retrieving 'https://cloud.r-project.org/src/contrib/BiocManager_1.30.19.tar.gz' ...
    	OK [file is up to date]
    Installing BiocManager [1.30.19] ...
    	OK [built from source]
    Moving BiocManager [1.30.19] into the cache ...
    	OK [moved to cache in 27 milliseconds]
    * Project '~/Downloads/testProject' loaded. [renv 0.16.0]
    * The project may be out of sync -- use `renv::status()` for more details.
    The following package(s) will be updated:
    
    # Bioconductor =======================
    - AnnotationDbi          [* -> 1.56.2]
    ...
    # CRAN ===============================
    - renv                   [0.16.0 -> 0.15.5]
    ...
    Do you want to proceed? [y/N]: y
    Retrieving 'https://cloud.r-project.org/src/contrib/Archive/renv/renv_0.15.5.tar.gz' ...
    	OK [downloaded 961.3 Kb in 0.2 secs]
    Installing renv [0.15.5] ...
    	OK [built from source]
    Moving renv [0.15.5] into the cache ...
    	OK [moved to cache in 6.8 milliseconds]
    ...
    The following package(s) have been updated:
    
    	renv [installed version 0.15.5 != loaded version 0.16.0]
    
    Consider restarting the R session and loading the newly-installed packages.
    

    All the R packages are restored in the $PROJECT/renv directory.

Check installed Bioconductor version

Following this post, use tools:::.BioC_version_associated_with_R_version().

Mind the '.' in front of the 'BioC'. It may be possible for some installed packages to have been sourced from a different BioC version.

tools:::.BioC_version_associated_with_R_version() # `3.6'
tools:::.BioC_version_associated_with_R_version() == '3.6'  # TRUE

CRAN Package Depends on Bioconductor Package

For example, if I run install.packages("NanoStringNorm") to install the package from CRAN, I may get

ERROR: dependency ‘vsn’ is not available for package ‘NanoStringNorm’

This is because the NanoStringNorm package depends on the vsn package which is on Bioconductor.

Another example is CRAN's tidyHeatmap that imports ComplexHeatmap in Bioconductor.

Another instance is CRAN's biospear (actually plsRcox) that depends on Bioc's 'survcomp' & 'mixOmics'.

One solution is to run a line setRepositories(ind=1:2). See this post or this one. Note that the default repository list can be found at (Ubuntu) /usr/lib/R/etc/repositories file.

options("repos") # display the available repositories (only CRAN)
setRepositories(ind=1:2)
options("repos") # CRAN and bioc are included
#                                        CRAN
#                "https://cloud.r-project.org"
# "https://bioconductor.org/packages/3.6/bioc"

install.packages("biospear") # it will prompt to select CRAN

install.packages("biospear", repos = "http://cran.rstudio.com") # NOT work since bioc repos is erased

This will also install the BiocInstaller package if it has not been installed before. See also Install Bioconductor Packages.

Bioconductor packages depend on CRAN

For example cowplot shows breakpointR from Bioconductor depends on it.

update.packages()

update.packages(ask="graphics") can open a graphical window to select packages.

only upgrade to binary package versions

Try setting options(pkgType = "binary") before running update.packages(). To make this always be the default behavior, you can add that line to your .Rprofile file.

How about options(install.packages.check.source = "no"), options(install.packages.compile.from.source = "never")?

Binary packages only for two versions of R

Check What Repos You are Using.

CRAN only provides binaries for one version of R prior to the current one. So when CRAN moves to post-3.6.* R most non version-stuck mirrors will not have 3.5.* binary versions of packages.

Install a tar.gz (e.g. an archived package) from a local directory

R CMD INSTALL <package-name>.tar.gz

Or in R:

# Method 1: cannot install dependencies 
install.packages(<pathtopackage>, repos = NULL)
# These paths can be source directories or archives or binary package
# archive files  (as created by ‘R CMD build --binary’).
# (‘http://’ and ‘file://’ URLs are also accepted and the files
# will be downloaded and installed from local copies.)

# Method 2: take care of dependencies from CRAN
devtools::install(<directory to package>, dependencies = TRUE) 
                         # this will use 'R CMD INSTALL' to install the package.
                         # It will try to install dependencies of the package from CRAN,
                         # if they're not already installed.

The installation process can be nasty due to the dependency issue. Consider the 'biospear' package

biospear - plsRcox (archived) - plsRglm (archived) - bipartite
                              - lars
                              - pls
                              - kernlab
                              - mixOmics (CRAN->Bioconductor)
                              - risksetROC
                              - survcomp (Bioconductor)
                              - rms

So in order to install the 'plsRcox' package, we need to do the following steps. Note: plsRcox package is back on 6/2/2018.

# For curl
system("apt update")
system("apt install curl libcurl4-openssl-dev libssl-dev")

# For X11
system("apt install libcgal-dev libglu1-mesa-dev libglu1-mesa-dev")
system("apt install libfreetype6-dev") # https://stackoverflow.com/questions/31820865/error-in-installing-rgl-package
source("https://bioconductor.org/biocLite.R")
biocLite("survcomp") # this has to be run before the next command of installing a bunch of packages from CRAN

install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz", 
                 repos = NULL, type="source")
# ERROR: dependencies ‘pkgconfig’, ‘cobs’, ‘corpcor’, ‘devtools’, ‘glmnet’, ‘grplasso’, ‘mboost’, ‘plsRcox’, 
# ‘pROC’, ‘PRROC’, ‘RCurl’, ‘survAUC’ are not available for package ‘biospear’
install.packages(c("pkgconfig", "cobs", "corpcor", "devtools", "glmnet", "grplasso", "mboost", 
                   "plsRcox", "pROC", "PRROC", "RCurl", "survAUC"))
# optional: install.packages(c("doRNG", "mvnfast"))
install.packages("https://cran.r-project.org/src/contrib/Archive/biospear/biospear_1.0.1.tar.gz", 
                 repos = NULL, type="source")
# OR
# devtools::install_github("cran/biospear")
library(biospear) # verify

To install the (deprecated, bioc) packages 'inSilicoMerging',

biocLite(c('rjson', 'Biobase', 'RCurl'))

# destination directory is required
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz", 
#               "~/Downloads/inSilicoDb_2.7.0.tar.gz")
# download.file("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz", 
#               "~/Downloads/inSilicoMerging_1.15.0.tar.gz")
# ~/Downloads or $HOME/Downloads won't work in untar()
# untar("~/Downloads/inSilicoDb_2.7.0.tar.gz", exdir="/home/brb/Downloads") 
# untar("~/Downloads/inSilicoMerging_1.15.0.tar.gz", exdir="/home/brb/Downloads") 
# install.packages("~/Downloads/inSilicoDb", repos = NULL)
# install.packages("~/Downloads/inSilicoMerging", repos = NULL)
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoDb_2.7.0.tar.gz", 
                 repos = NULL, type = "source")
install.packages("http://www.bioconductor.org/packages/3.3/bioc/src/contrib/inSilicoMerging_1.15.0.tar.gz", 
                 repos = NULL, type = "source")

R CMD INSTALL -l LIB/--library=LIB option

Install a package to a custom location

$ R CMD INSTALL -l /usr/me/localR/library myRPackage.tar.gz

Use a package installed in a custom location

R> library("myRPackage", lib.loc="/usr/me/local/R/library")

# OR include below in .bashrc file
$ export R_LIBS=/usr/me/local/R/library
R> .libPaths() # check
R> library("myRPackage")

Install a specific version of R/Bioconductor package

For packages from CRAN, use something like remotes::install_version("dplyr", "1.0.2")

For packages from Bioconductor, see the two solutions R how to install a specified version of a bioconductor package?

Install multiple/different versions of the same R package

https://stackoverflow.com/a/2989369

install.packages("~/Downloads/foo_0.1.1.tar.gz", lib = "/tmp", repos = NULL)
# a new folder "/tmp/foo" will be created
library(foo, lib.loc="/tmp") # Or use 'lib' to be consistent with install.packages()      

library(foo, lib.loc="~/dev/foo/v1")    ## loads v1
# and
library(foo, lib.loc="~/dev/foo/v2")    ## loads v2

packageVersion("foo", lib.loc = "/tmp")
sessionInfo()
help(package = "foo", lib.loc = "/tmp")

remove.packages("foo", lib = "/tmp")

The same works for install.packages(). help(install.packages)

The install_version() from devtools and remotes will overwrite the existing installation.

Query an R package installed locally

packageDescription("MASS")
packageVersion("MASS")

Query an R package (from CRAN) basic information: available.packages()

packageStatus() # Summarize information about installed packages

available.packages() # List Available Packages at CRAN-like Repositories
                     # Even I use an old version of R, it still return the latest version of the packages
                     # The 'problem' happens on install.packages() too.

The available.packages() command is useful for understanding package dependency. Use setRepositories() or 'RGUI -> Packages -> select repositories' to select repositories and options()$repos to check or change the repositories.

The return result of available.packages() depends on the R version.

The number of packages returned from available.packages() is smaller than the number of packages obtained from rsync CRAN Mirror HOWTO/FAQ. For example, 19473 < 25000. Some archived packages are still available via rsync.

Also the packageStatus() is another useful function for query how many packages are in the repositories, how many have been installed, and individual package status (installed or not, needs to be upgraded or not).

> options()$repos 
                       CRAN 
"https://cran.rstudio.com/" 

> packageStatus() 
Number of installed packages:
                                    
                                      ok upgrade unavailable
  C:/Program Files/R/R-3.0.1/library 110       0           1

Number of available packages (each package counted only once):
                                                                                   
                                                                                    installed not installed
  http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0                            76          4563
  http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.0                                0             5
  http://www.bioconductor.org/packages/2.12/bioc/bin/windows/contrib/3.0                   16           625
  http://www.bioconductor.org/packages/2.12/data/annotation/bin/windows/contrib/3.0         4           686
> tmp <- available.packages()
> str(tmp)
 chr [1:5975, 1:17] "A3" "ABCExtremes" "ABCp2" "ACCLMA" "ACD" "ACNE" "ADGofTest" "ADM3" "AER" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5975] "A3" "ABCExtremes" "ABCp2" "ACCLMA" ...
  ..$ : chr [1:17] "Package" "Version" "Priority" "Depends" ...
> tmp[1:3,]
            Package       Version Priority Depends                     Imports LinkingTo Suggests             
A3          "A3"          "0.9.2" NA       "xtable, pbapply"           NA      NA        "randomForest, e1071"
ABCExtremes "ABCExtremes" "1.0"   NA       "SpatialExtremes, combinat" NA      NA        NA                   
ABCp2       "ABCp2"       "1.1"   NA       "MASS"                      NA      NA        NA                   
            Enhances License      License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation File
A3          NA       "GPL (>= 2)" NA              NA                    NA      NA    NA     NA               NA  
ABCExtremes NA       "GPL-2"      NA              NA                    NA      NA    NA     NA               NA  
ABCp2       NA       "GPL-2"      NA              NA                    NA      NA    NA     NA               NA  
            Repository                                                     
A3          "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCExtremes "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"
ABCp2       "http://watson.nci.nih.gov/cran_mirror/bin/windows/contrib/3.0"

And the following commands find which package depends on Rcpp and also which are from bioconductor repository.

> pkgName <- "Rcpp"
> rownames(tmp)[grep(pkgName, tmp[,"Depends"])]
> tmp[grep("Rcpp", tmp[,"Depends"]), "Depends"]

> ind <- intersect(grep(pkgName, tmp[,"Depends"]), grep("bioconductor", tmp[, "Repository"]))
> rownames(grep)[ind]
NULL
> rownames(tmp)[ind]
 [1] "ddgraph"            "DESeq2"             "GeneNetworkBuilder" "GOSemSim"           "GRENITS"           
 [6] "mosaics"            "mzR"                "pcaMethods"         "Rdisop"             "Risa"              
[11] "rTANDEM"

CRAN vs Bioconductor packages

> R.version # 3.4.3
# CRAN
> x <- available.packages()
> dim(x)
[1] 12581    17

# Bioconductor Soft
> biocinstallRepos()
                                               BioCsoft 
           "https://bioconductor.org/packages/3.6/bioc" 
                                                BioCann 
"https://bioconductor.org/packages/3.6/data/annotation" 
                                                BioCexp 
"https://bioconductor.org/packages/3.6/data/experiment" 
                                                   CRAN 
                            "https://cran.rstudio.com/" 
> y <- available.packages(repos = biocinstallRepos()[1])
> dim(y)
[1] 1477   17
> intersect(x[, "Package"], y[, "Package"])
character(0)
# Bioconductor Annotation
> dim(available.packages(repos = biocinstallRepos()[2]))
[1] 909  17
# Bioconductor Experiment
> dim(available.packages(repos = biocinstallRepos()[3]))
[1] 326  17

# CRAN + All Bioconductor
> z <- available.packages(repos = biocinstallRepos())
> dim(z)
[1] 15292    17

Downloading Bioconductor package with an old R

When I try to download the GenomicDataCommons package using R 3.4.4 with Bioc 3.6 (the current R version is 3.5.0), it was found it can only install version 1.2.0 instead the latest version 1.4.1.

It does not work by running biocLite("BiocUpgrade") to upgrade Bioc from 3.6 to 3.7.

source("https://bioconductor.org/biocLite.R")
biocLite("BiocUpgrade") 
# Error: Bioconductor version 3.6 cannot be upgraded with R version 3.4.4

See some instruction on RStudio package manager website.

Analyzing data on CRAN packages

R package location when they are installed by root

/usr/local/lib/R/site-library

Customizing your package/library location

Add a personal directory to .libPaths()

.libPaths( c( .libPaths(), "~/userLibrary") )

No need to use the assignment operator.

Install personal R packages after upgrade R, .libPaths(), Rprofile.site, R_LIBS_USER

File Example
Rprofile.site/.Rprofile .libPaths(c("/usr/lib/R/site-library",
      "/usr/lib/R/library"))
Renviron.site/.Renviron R_LIB_SITE="/usr/lib/R/site-library:/usr/lib/R/library"

Scenario: We already have installed many R packages under R 3.1.X in the user's directory. Now we upgrade R to a new version (3.2.X). We like to have these packages available in R 3.2.X.

For Windows OS, refer to R for Windows FAQ

The follow method works on Linux and Windows.

Make sure only one instance of R is running

# Step 1. update R's built-in packages and install them on my personal directory
update.packages(ask=FALSE, checkBuilt = TRUE, repos="http://cran.rstudio.com")

# Step 2. update Bioconductor packages
.libPaths() # The first one is my personal directory
# [1] "/home/brb/R/x86_64-pc-linux-gnu-library/3.2"
# [2] "/usr/local/lib/R/site-library"
# [3] "/usr/lib/R/site-library"
# [4] "/usr/lib/R/library"

Sys.getenv("R_LIBS_USER") # may or may not equivalent to .libPaths()[1]
ul <- unlist(strsplit(Sys.getenv("R_LIBS_USER"), "/"))
src <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.1") 
des <- file.path(paste(ul[1:(length(ul)-1)], collapse="/"), "3.2") 
pkg <- dir(src, full.names = TRUE)
if (!file.exists(des)) dir.create(des)  # If 3.2 subdirectory does not exist yet!
file.copy(pkg, des, overwrite=FALSE, recursive = TRUE)
source("http://www.bioconductor.org/biocLite.R")
biocLite(ask = FALSE)

From Robert Kabacoff (R in Action)

  • If you have a customized Rprofile.site file (see appendix B), save a copy outside of R.
  • Launch your current version of R and issue the following statements
oldip <- installed.packages()[,1]
save(oldip, file="path/installedPackages.Rdata")

where path is a directory outside of R.

  • Download and install the newer version of R.
  • If you saved a customized version of the Rprofile.site file in step 1, copy it into the new installation.
  • Launch the new version of R, and issue the following statements
load("path/installedPackages.Rdata")
newip <- installed.packages()[,1]
for(i in setdiff(oldip, newip))
  install.packages(i)

where path is the location specified in step 2.

  • Delete the old installation (optional).

This approach will install only packages that are available from the CRAN. It won’t find packages obtained from other locations. In fact, the process will display a list of packages that can’t be installed For example for packages obtained from Bioconductor, use the following method to update packages

source(http://bioconductor.org/biocLite.R)
biocLite(PKGNAME)

Persistent config and data for R packages with .Rprofile and .Renviron

Persistent config and data for R packages. startup, rappdirs, hoardr, keyring.

Would you like to use a personal library instead?

Some posts from internet

  • Setting R_LIBS & avoiding “Would you like to use a personal library instead?”. Note: I try to create ~/.Renviron to add my personal folder in it. But update.packages() still asks me if I like to use a personal library instead (tested on Ubuntu + R 3.4).
  • automatically create personal library in R. Using suppressUpdates + specify lib in biocLite() or update.packages(Sys.getenv("R_LIBS_USER"), ask = F)
    # create local user library path (not present by default)
    dir.create(path = Sys.getenv("R_LIBS_USER"), showWarnings = FALSE, recursive = TRUE)
    # install to local user library path
    install.packages(p, lib = Sys.getenv("R_LIBS_USER"), repos = "https://cran.rstudio.com/")
    # Bioconductor version
    biocLite(p, suppressUpdates = TRUE, lib = Sys.getenv("R_LIBS_USER"))

The problem can happen if the R was installed to the C:\Program Files\R folder by users but then some main packages want to be upgraded. R will always pops a message 'Would you like to use a personal library instead?'.

To suppress the message and use the personal library always,

Actually the following hints will help us to create a convenient function UpdateMainLibrary() which will install updated main packages in the user's Documents directory without a warning dialog.

  • .libPaths() only returns 1 string "C:/Program Files/R/R-x.y.z/library" on the machines that does not have this problem
  • .libPaths() returns two strings "C:/Users/USERNAME/Documents/R/win-library/x.y" & "C:/Program Files/R/R-x.y.z/library" on machines with the problem.
UpdateMainLibrary <- function() {
  # Update main/site packages
  # The function is used to fix the problem 'Would you like to use a personal library instead?'  
  if (length(.libPaths()) == 1) return()
  
  ind_mloc <- grep("Program", .libPaths()) # main library e.g. 2
  ind_ploc <- grep("Documents", .libPaths()) # personal library e.g. 1
  if (length(ind_mloc) > 0L && length(ind_ploc) > 0L)
     # search outdated main packages
	 old_mloc <- ! old.packages(.libPaths()[ind_mloc])[, "Package"] %in% 
	               installed.packages(.libPaths()[ind_ploc])[, "Package"]
     oldpac <- old.packages(.libPaths()[ind_mloc])[old_mloc, "Package"]
	 if (length(oldpac) > 0L)
        install.packages(oldpac, .libPaths()[ind_ploc])  
}

On Linux,

> update.packages()
...
The downloaded source packages are in
‘/tmp/RtmpBrYccd/downloaded_packages’
Warning in install.packages(update[instlib == l, "Package"], l, contriburl = contriburl,  :
                              'lib = "/opt/R/3.5.0/lib/R/library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) yes
...
> system("ls -lt /home/brb/R/x86_64-pc-linux-gnu-library/3.5 | head")
total 224
drwxrwxr-x  9 brb brb 4096 Oct  3 09:30 survival
drwxrwxr-x  9 brb brb 4096 Oct  3 09:29 mgcv
drwxrwxr-x 10 brb brb 4096 Oct  3 09:29 MASS
drwxrwxr-x  9 brb brb 4096 Oct  3 09:29 foreign

# So new versions of survival, mgc, MASS, foreign are installed in the personal directory
# The update.packages() will issue warnings if we try to run it again. 
# It's OK to ignore these warnings.
> update.packages()
Warning: package 'foreign' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'MASS' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'mgcv' in library '/opt/R/3.5.0/lib/R/library' will not be updated
Warning: package 'survival' in library '/opt/R/3.5.0/lib/R/library' will not be updated

R_LIBS_USER is empty in R 3.4.1

See install.package() error, R_LIBS_USER is empty in R 3.4.1.

List vignettes from a package

vignette(package=PACKAGENAME)

List data from a package

data(package=PACKAGENAME)

sysdata.rda

List all functions of a package

Assume a package is already loaded. Then

ls("package:cowplot")

Getting a list of functions and objects in a package. This also assumes the package in loaded. In addition to functions (separated by primitive and non-primitive), it can show constants and objects.

List installed packages and versions

ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
unique(ip$Priority)
# [1] <NA>        base        recommended
# Levels: base recommended
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)

Query the names of outdated packages

psi <- packageStatus()$inst
subset(psi, Status == "upgrade", drop = FALSE)
#                     Package                                  LibPath     Version    Priority                Depends
# RcppArmadillo RcppArmadillo C:/Users/brb/Documents/R/win-library/3.2 0.5.100.1.0        <NA>                   <NA>
# Matrix               Matrix       C:/Program Files/R/R-3.2.0/library       1.2-0 recommended R (>= 2.15.2), methods
#                                             Imports LinkingTo                 Suggests
# RcppArmadillo                      Rcpp (>= 0.11.0)      Rcpp RUnit, Matrix, pkgKitten
# Matrix        graphics, grid, stats, utils, lattice      <NA>               expm, MASS
#                                            Enhances    License License_is_FOSS License_restricts_use OS_type MD5sum
# RcppArmadillo                                  <NA> GPL (>= 2)            <NA>                  <NA>    <NA>   <NA>
# Matrix        MatrixModels, graph, SparseM, sfsmisc GPL (>= 2)            <NA>                  <NA>    <NA>   <NA>
#               NeedsCompilation Built  Status
# RcppArmadillo              yes 3.2.0 upgrade
# Matrix                     yes 3.2.0 upgrade

The above output does not show the package version from the latest packages on CRAN. So the following snippet does that.

psi <- packageStatus()$inst
pl <- unname(psi$Package[psi$Status == "upgrade"])  # List package names
ap <- as.data.frame(available.packages()[, c(1,2,3)], stringsAsFactors = FALSE)
out <- cbind(subset(psi, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
rownames(out) <- NULL
out
#         Package  OldVersion  NewVersion
# 1 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
# 2        Matrix       1.2-0       1.2-1

To consider also the packages from Bioconductor, we have the following code. Note that "3.1" means the Bioconductor version and "3.2" is the R version. See Bioconductor release versions page.

psic <- packageStatus(repos = c(contrib.url(getOption("repos")),
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))$inst
subset(psic, Status == "upgrade", drop = FALSE)
pl <- unname(psic$Package[psic$Status == "upgrade"])

ap   <- as.data.frame(available.packages(c(contrib.url(getOption("repos")),
                                "http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2",
                                "http://www.bioconductor.org/packages/3.1/data/annotation/bin/windows/contrib/3.2"))[, c(1:3)], 
                      stringAsFactors = FALSE)

out <- cbind(subset(psic, Status == "upgrade")[, c("Package", "Version")], ap[match(pl, ap$Package), "Version"])
colnames(out)[2:3] <- c("OldVersion", "NewVersion")
rownames(out) <- NULL
out
#         Package  OldVersion  NewVersion
# 1         limma      3.24.5      3.24.9
# 2 RcppArmadillo 0.5.100.1.0 0.5.200.1.0
# 3        Matrix       1.2-0       1.2-1

Searching for packages in CRAN

METACRAN (www.r-pkg.org) - Search and browse all CRAN/R packages

cranly visualisations and summaries for R packages

Exploring R packages with cranly

Query top downloaded packages, download statistics

cranlogs

cranlogs package - Download Logs from the RStudio CRAN Mirror. Suitable on R console.

library(cranlogs)
out <- cran_top_downloads("last-week", 100) # 100 is the maximum limit
out$package

packageRank

packageRank package: Computing and Visualizing CRAN Downloads. Suitable to run on RStudio cloud. Include both CRAN and Bioconductor.

> plot(cranDownloads(packages = c("packageRank", "limma"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), when = "last-month"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019", to ="2019"))
> plot(cranDownloads(packages = c("shiny", "glmnet"), from = "2019-12", to ="2019-12"))
> plot(bioconductorDownloads(packages = c("edgeR", "DESeq2", "Rsubread", "limma"), when = "last-year"))

BiocPkgTools

For Bioconductor packages, try BiocPkgTools. See the paper.

dlstats

dlstats. Monthly download stats of 'CRAN' and 'Bioconductor' packages.

installation path not writeable from running biocLite()

When I ran biocLite() to install a new package, I got a message (the Bioc packages are installed successfully anyway)

...
* DONE (curatedOvarianData)

The downloaded source packages are in
	‘/tmp/RtmpHxnH2K/downloaded_packages’
installation path not writeable, unable to update packages: rgl, rJava,
  codetools, foreign, lattice, MASS, spatial, survival

However, if I uses install.package() it can update the package

> packageVersion("survival")
[1] ‘2.42.3’
> update.packages("survival")  # Not working though no error message
> packageVersion("survival")
[1] ‘2.42.3’
> install.packages("survival")
Installing package into ‘/home/brb/R/x86_64-pc-linux-gnu-library/3.4’
...
* DONE (survival)

The downloaded source packages are in
	‘/tmp/RtmpHxnH2K/downloaded_packages’
> packageVersion("survival")
[1] ‘2.42.6’
> library(survival)
> sessionInfo() # show survival package 2.42-6 was attached

It makes sense to always use personal directory when we install packages. See .libPaths().

Warning: cannot remove prior installation of package

http://stackoverflow.com/questions/15932152/unloading-and-removing-a-loaded-package-withouth-restarting-r

Instance 1.

# Install the latest hgu133plus2cdf package
# Remove/Uninstall hgu133plus2.db package
# Put/Install an old version of IRanges (eg version 1.18.2 while currently it is version 1.18.3)
# Test on R 3.0.1
library(hgu133plus2cdf) # hgu133pluscdf does not depend or import IRanges
source("http://bioconductor.org/biocLite.R")
biocLite("hgu133plus2.db", ask=FALSE) # hgu133plus2.db imports IRanges
# Warning:cannot remove prior installation of package 'IRanges'
# Open Windows Explorer and check IRanges folder. Only see libs subfolder.

Note:

  • In the above example, all packages were installed under C:\Program Files\R\R-3.0.1\library\.
  • In another instance where I cannot reproduce the problem, new R packages were installed under C:\Users\xxx\Documents\R\win-library\3.0\. The different thing is IRanges package CAN be updated but if I use packageVersion("IRanges") command in R, it still shows the old version.
  • The above were tested on a desktop.

Instance 2.

# On a fresh R 3.2.0, I install Bioconductor's depPkgTools & lumi packages. Then I close R, re-open it, 
# and install depPkgTools package again.
> source("http://bioconductor.org/biocLite.R")
Bioconductor version 3.1 (BiocInstaller 1.18.2), ?biocLite for help
> biocLite("pkgDepTools")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘pkgDepTools’
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/pkgDepTools_1.34.0.zip'
Content type 'application/zip' length 390579 bytes (381 KB)
downloaded 381 KB

package ‘pkgDepTools’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘pkgDepTools’

The downloaded binary packages are in
        C:\Users\brb\AppData\Local\Temp\RtmpYd2l7i\downloaded_packages
> library(pkgDepTools)
Error in library(pkgDepTools) : there is no package called ‘pkgDepTools’

The pkgDepTools library folder appears in C:\Users\brb\Documents\R\win-library\3.2, but it is empty. The weird thing is if I try the above steps again, I cannot reproduce the problem.

Warning: dependency ‘XXX’ is not available

How should I deal with “package 'xxx' is not available (for R version x.y.z)” warning?

Error: there is no package called XXX

The error happened when I try to run library() command on a package that was just installed. R 3.6.0. 'biospear' version is 1.0.2. macOS.

> library(biospear)
Loading required package: pkgconfig
Error: package or namespace load failed for ‘biospear’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vIi):
 there is no package called ‘mixOmics’

Note

  • The package mixOmics was removed from CRAN. It is now available on Bioconductor.
  • Tested to install on a docker container: docker run --net=host -it --rm r-base
    ERROR: dependency ‘car’ is not available for package ‘plsRglm’
    * removing ‘/usr/local/lib/R/site-library/plsRglm’
    ERROR: dependencies ‘plsRglm’, ‘mixOmics’, ‘survcomp’ are not available for package ‘plsRcox’
    * removing ‘/usr/local/lib/R/site-library/plsRcox’
    ERROR: dependencies ‘devtools’, ‘plsRcox’, ‘RCurl’ are not available for package ‘biospear’
    * removing ‘/usr/local/lib/R/site-library/biospear’
    The car package looks OK on CRAN. survcomp was moved from CRAN to Bioconductor too.
  • As we can see above, the official r-base image does not contain libraries enough to install RCurl/devtools packages. Consider the tidyverse image (based on RStudio image) from the rocker project.
    docker pull rocker/tidyverse:3.6.0
    docker run --net=host -it --rm -e PASSWORD=password -p 8787:8787 rocker/tidyverse:3.6.0
    # the default username is 'rstudio'
    # Open a browser, log in. Run 'install.packages("RCurl")'. It works.
  • Testing on Mint linux also shows errors about dependencies of mixOmics and survcomp.
  • The best practice to install a package that may depend on packages located/moved to Bioconductor: Run setRepositories(ind=1:2) before calling install.packages(). However, it does not remedy the situation that the 1st level imports package (eg plsRcox) was installed before but the 2nd level imports package (eg mixOmics) was not installed.
  • dependsOnPkgs(): Find Reverse Dependencies. It seems it only return packages that have been installed locally. For example, tools::dependsOnPkgs("RcppEigen", "LinkingTo")

Warning: Unable to move temporary installation

The problem seems to happen only on virtual machines (Virtualbox).

  • Warning: unable to move temporary installation `C:\Users\brb\Documents\R\win-library\3.0\fileed8270978f5\quadprog` to `C:\Users\brb\Documents\R\win-library\3.0\quadprog` when I try to run 'install.packages("forecast").
  • Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’ to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’ when I try to run 'biocLite("lumi")'. The other dependency packages look fine although I am not sure if any unknown problem can happen (it does, see below).

Here is a note of my trouble shooting.

  1. If I try to ignore the warning and load the lumi package. I will get an error.
  2. If I try to run biocLite("lumi") again, it will only download & install lumi without checking missing 'plyr' package. Therefore, when I try to load the lumi package, it will give me an error again.
  3. Even I install the plyr package manually, library(lumi) gives another error - missing mclust package.
> biocLite("lumi")
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/BiocInstaller_1.18.2.zip'
Content type 'application/zip' length 114097 bytes (111 KB)
downloaded 111 KB
...
package ‘lumi’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
Old packages: 'BiocParallel', 'Biostrings', 'caret', 'DESeq2', 'gdata', 'GenomicFeatures', 'gplots', 'Hmisc', 'Rcpp', 'RcppArmadillo', 'rgl',
  'stringr'
Update all/some/none? [a/s/n]: a
also installing the dependencies ‘Rsamtools’, ‘GenomicAlignments’, ‘plyr’, ‘rtracklayer’, ‘gridExtra’, ‘stringi’, ‘magrittr’

trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/Rsamtools_1.20.1.zip'
Content type 'application/zip' length 8138197 bytes (7.8 MB)
downloaded 7.8 MB
...
package ‘Rsamtools’ successfully unpacked and MD5 sums checked
package ‘GenomicAlignments’ successfully unpacked and MD5 sums checked
package ‘plyr’ successfully unpacked and MD5 sums checked
Warning: unable to move temporary installation ‘C:\Users\brb\Documents\R\win-library\3.2\file5e0104b5b49\plyr’ 
         to ‘C:\Users\brb\Documents\R\win-library\3.2\plyr’
package ‘rtracklayer’ successfully unpacked and MD5 sums checked
package ‘gridExtra’ successfully unpacked and MD5 sums checked
package ‘stringi’ successfully unpacked and MD5 sums checked
package ‘magrittr’ successfully unpacked and MD5 sums checked
package ‘BiocParallel’ successfully unpacked and MD5 sums checked
package ‘Biostrings’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘Biostrings’
package ‘caret’ successfully unpacked and MD5 sums checked
package ‘DESeq2’ successfully unpacked and MD5 sums checked
package ‘gdata’ successfully unpacked and MD5 sums checked
package ‘GenomicFeatures’ successfully unpacked and MD5 sums checked
package ‘gplots’ successfully unpacked and MD5 sums checked
package ‘Hmisc’ successfully unpacked and MD5 sums checked
package ‘Rcpp’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘rgl’ successfully unpacked and MD5 sums checked
package ‘stringr’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
> library(lumi)
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vIi) : 
  there is no package called ‘plyr’
Error: package or namespace load failed for ‘lumi’
> search()
 [1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"       "package:BiocGenerics"  "package:parallel"      "package:stats"        
 [7] "package:graphics"      "package:grDevices"     "package:utils"         "package:datasets"      "package:methods"       "Autoloads"            
[13] "package:base"         
> biocLite("lumi")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘lumi’
trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/lumi_2.20.1.zip'
Content type 'application/zip' length 18185326 bytes (17.3 MB)
downloaded 17.3 MB

package ‘lumi’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages
> search()
 [1] ".GlobalEnv"            "package:BiocInstaller" "package:Biobase"       "package:BiocGenerics"  "package:parallel"      "package:stats"        
 [7] "package:graphics"      "package:grDevices"     "package:utils"         "package:datasets"      "package:methods"       "Autoloads"            
[13] "package:base"         
> library(lumi)
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vIi) : 
  there is no package called ‘plyr’
Error: package or namespace load failed for ‘lumi’
> biocLite("plyr")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.2), R version 3.2.0.
Installing package(s) ‘plyr’
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/plyr_1.8.2.zip'
Content type 'application/zip' length 1128621 bytes (1.1 MB)
downloaded 1.1 MB

package ‘plyr’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\brb\AppData\Local\Temp\RtmpyUjsJD\downloaded_packages

> library(lumi)
Error in loadNamespace(j <- i1L, c(lib.loc, .libPaths()), versionCheck = vIj) : 
  there is no package called ‘mclust’
Error: package or namespace load failed for ‘lumi’

> ?biocLite
Warning messages:
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
  cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
2: In find.package(if (is.null(package)) loadedNamespaces() else package,  :
  there is no package called ‘Biostrings’
> library(lumi)
Error in loadNamespace(j <- i1L, c(lib.loc, .libPaths()), versionCheck = vIj) : 
  there is no package called ‘mclust’
In addition: Warning messages:
1: In read.dcf(file.path(p, "DESCRIPTION"), c("Package", "Version")) :
  cannot open compressed file 'C:/Users/brb/Documents/R/win-library/3.2/Biostrings/DESCRIPTION', probable reason 'No such file or directory'
2: In find.package(if (is.null(package)) loadedNamespaces() else package,  :
  there is no package called ‘Biostrings’
Error: package or namespace load failed for ‘lumi’

Other people also have the similar problem. The possible cause is the virus scanner locks the file and R cannot move them.

Some possible solutions:

  1. Delete ALL folders under R/library (e.g. C:/Progra~1/R/R-3.2.0/library) folder and install the main package again using install.packages() or biocLite().
  2. For specific package like 'lumi' from Bioconductor, we can find out all dependency packages and then install them one by one.
  3. Find out and install the top level package which misses dependency packages.
    1. This is based on the fact that install.packages() or biocLite() sometimes just checks & installs the 'Depends' and 'Imports' packages and won't install all packages recursively
    2. we can do a small experiment by removing a package which is not directly dependent/imported by another package; e.g. 'iterators' is not dependent/imported by 'glment' directly but indirectly. So if we run remove.packages("iterators"); install.packages("glmnet"), then the 'iterator' package is still missing.
    3. A real example is if the missing packages are 'Biostrings', 'limma', 'mclust' (these are packages that 'minfi' directly depends/imports although they should be installed when I run biocLite("lumi") command), then I should just run the command remove.packages("minfi"); biocLite("minfi"). If we just run biocLite("lumi") or biocLite("methylumi"), the missing packages won't be installed.

Error in download.file(url, destfile, method, mode = "wb", ...)

HTTP status was '404 Not Found'

Tested on an existing R-3.2.0 session. Note that VariantAnnotation 1.14.4 was just uploaded to Bioc.

> biocLite("COSMIC.67")
BioC_mirror: http://bioconductor.org
Using Bioconductor version 3.1 (BiocInstaller 1.18.3), R version 3.2.0.
Installing package(s) ‘COSMIC.67’
also installing the dependency ‘VariantAnnotation’

trying URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
Error in download.file(url, destfile, method, mode = "wb", ...) : 
  cannot open URL 'http://bioconductor.org/packages/3.1/bioc/bin/windows/contrib/3.2/VariantAnnotation_1.14.3.zip'
In addition: Warning message:
In download.file(url, destfile, method, mode = "wb", ...) :
  cannot open: HTTP status was '404 Not Found'
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
  download of package ‘VariantAnnotation’ failed
installing the source package ‘COSMIC.67’

trying URL 'http://bioconductor.org/packages/3.1/data/experiment/src/contrib/COSMIC.67_1.4.0.tar.gz'
Content type 'application/x-gzip' length 40999037 bytes (39.1 MB)

However, when I tested on a new R-3.2.0 (just installed in VM), the COSMIC package installation is successful. That VariantAnnotation version 1.14.4 was installed (this version was just updated today from Bioconductor).

The cause of the error is the available.package() function will read the rds file first from cache in a tempdir (C:\Users\XXXX\AppData\Local\Temp\RtmpYYYYYY). See lines 51-55 of <packages.R>.

 dest <- file.path(tempdir(),
                   paste0("repos_", URLencode(repos, TRUE), ".rds"))
 if(file.exists(dest)) {
    res0 <- readRDS(dest)
 } else {
    ...
 }  

Since my R was opened 1 week ago, the rds file it reads today contains old information. Note that Bioconductor does not hold the source code or binary code for the old version of packages. This explains why biocLite() function broke. When I restart R, the original problem is gone.

If we look at the source code of available.packages(), we will see we could use cacheOK option in download.file() function.

download.file(url, destfile, method, cacheOK = FALSE, quiet = TRUE, mode ="wb")

Another case: Error in download.file(url, destfile, method, mode = "wb", ...)

> install.packages("quantreg")

  There is a binary version available but the source version is later:
         binary source needs_compilation
quantreg   5.33   5.34              TRUE

Do you want to install from sources the package which needs compilation?
y/n: n
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
Warning in install.packages :
  cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz': HTTP status was '404 Not Found'
Error in download.file(url, destfile, method, mode = "wb", ...) : 
  cannot open URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.4/quantreg_5.33.tgz'
Warning in install.packages :
  download of package ‘quantreg’ failed

It seems the binary package cannot be found on the mirror. So the solution here is to download the package from the R main server. Note that after I have successfully installed the binary package from the main R server, I remove the package in R and try to install the binary package from rstudio.com server agin and it works this time.

> install.packages("quantreg", repos = "https://cran.r-project.org")
trying URL 'https://cran.r-project.org/bin/macosx/el-capitan/contrib/3.4/quantreg_5.34.tgz'
Content type 'application/x-gzip' length 1863561 bytes (1.8 MB)
==================================================
downloaded 1.8 MB

Another case: Error in download.file() on Windows 7

For some reason, IE 8 cannot interpret https://ftp.ncbi.nlm.nih.gov though it understands ftp://ftp.ncbi.nlm.nih.gov.

This is tested using R 3.4.3.

> download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
trying URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
Error in download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz",  : 
  cannot open URL 'https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
In addition: Warning message:
In download.file("https://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz",  :
  InternetOpenUrl failed: 'An error occurred in the secure channel support'

> download.file("ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz", "test.soft.gz")
trying URL 'ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE7nnn/GSE7848/soft/GSE7848_family.soft.gz'
downloaded 9.1 MB

ERROR: failed to lock directory

Follow the suggestion to remove the LOCK file. See the post.

It could happened in calling install.packages(), biocLite() or devtools::install_github(), and so on.

Error in unloadNamespace(package)

> d3heatmap(mtcars, scale = "column", colors = "Blues")
Error: 'col_numeric' is not an exported object from 'namespace:scales'
> packageVersion("scales")
[1] ‘0.2.5’
> library(scales)
Error in unloadNamespace(package) : 
  namespace ‘scales’ is imported by ‘ggplot2’ so cannot be unloaded
In addition: Warning message:
package ‘scales’ was built under R version 3.2.1 
Error in library(scales) : 
  Package ‘scales’ version 0.2.4 cannot be unloaded
> search()
 [1] ".GlobalEnv"             "package:d3heatmap"      "package:ggplot2"       
 [4] "package:microbenchmark" "package:COSMIC.67"      "package:BiocInstaller" 
 [7] "package:stats"          "package:graphics"       "package:grDevices"     
[10] "package:utils"          "package:datasets"       "package:methods"       
[13] "Autoloads"              "package:base" 

If I open a new R session, the above error will not happen!

The problem occurred because the 'scales' package version required by the d3heatmap package/function is old. See this post. And when I upgraded the 'scales' package, it was locked by the package was imported by the ggplot2 package.

Unload a package

Add unload = TRUE option to unload the namespace. See detach().

require(splines)
detach(package:splines, unload=TRUE)

crantastic

https://crantastic.org/. A community site for R packages where you can search for, review and tag CRAN packages.

https://github.com/hadley/crantastic

New R packages as reported by CRANberries

http://blog.revolutionanalytics.com/2015/07/mranspackages-spotlight.html

#----------------------------
# SCRAPE CRANBERRIES FILES TO COUNT NEW PACKAGES AND PLOT
#
library(ggplot2)
# Build a vextor of the directories of interest
year <- c("2013","2014","2015")
month <- c("01","02","03","04","05","06","07","08","09","10","11","12")
span <-c(rep(month,2),month[1:7])
dir <- "http://dirk.eddelbuettel.com/cranberries"

url2013 <- file.path(dir,"2013",month)
url2014 <- file.path(dir,"2014",month)
url2015 <- file.path(dir,"2015",month[1:7])
url <- c(url2013,url2014,url2015)

# Read each directory and count the new packages
new_p <- vector()
for(i in url){
  raw.data <- readLines(i)
  new_p[i] <- length(grep("New package",raw.data,value=TRUE))
}

# Plot
time <- seq(as.Date("2013-01-01"), as.Date("2015-07-01"), by="months")
new_pkgs <- data.frame(time,new_p)

ggplot(new_pkgs, aes(time,y=new_p)) +
  geom_line() + xlab("") + ylab("Number of new packages") + 
  geom_smooth(method='lm') + ggtitle("New R packages as reported by CRANberries") 

R packages being removed

Top new packages in 2015

keep.source.pkgs option

State of R packages in your library. The original code formatting and commenting being removed by default, unless one changes some options for installing packages.

  • options(keep.source.pkgs = TRUE)
  • install.packages("rhub", INSTALL_opts = "--with-keep.source", type = "source")
  • R CMD install --with-keep.source

Package installation speed for packages installed with ‘keep.source’ has been improved. 2021-12-2.

Speeding up package installation

An efficient way to install and load R packages

An efficient way to install and load R packages

# Package names
packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate", "pwr", "psy", "car", "doBy", "imputeMissings", "RcmdrMisc", "questionr", "vcd", "multcomp", "KappaGUI", "rcompanion", "FactoMineR", "factoextra", "corrplot", "ltm", "goeveg", "corrplot", "FSA", "MASS", "scales", "nlme", "psych", "ordinal", "lmtest", "ggpubr", "dslabs", "stringr", "assist", "ggstatsplot", "forcats", "styler", "remedy", "snakecaser", "addinslist", "esquisse", "here", "summarytools", "magrittr", "tidyverse", "funModeling", "pander", "cluster", "abind")

# Install packages not yet installed
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))

Alternatively use the pacman package.

library( , exclude, include.only)

See ?library

library(tidyverse)
library(MASS, exclude='select')
library(thepackage, include.only="thefunction")

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

it needs to be reinstalled for use with this R version. The problem seems to be specific to R 3.5.1 in Ubuntu 16.04. I got this message when I try to install the keras and tidyverse packages. The 'XXX' package includes nlme for installing "tidyverse" and Matrix for installing "reticulate". I have already logged in as root. I need to manually install these packages again though it seems I did not see a version change for these packages.

Same error Error: package was installed by an R version with different internals; it needs to be reinstalled for use with this R version.

Today it also happened when I tried to install "pec" which broke when it was installing "Hmisc". The error message is "Error : package ‘rpart’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version". I am using R 3.5.2. rpart version is ‘4.1.13’. The solution is I install rpart again (under my account is enough) though rpart does not have a new version. Then I can install "Hmisc".

packrat and renv

See Reproducible → packrat/renv

R package dependencies

Depends, Imports, Suggests, Enhances, LinkingTo

See Writing R Extensions and install.packages().

  • Depends: list of package names which this package depends on. Those packages will be attached (so it is better to use Imports instead of Depends as much as you can) before the current package when library or require is called. The ‘Depends’ field can also specify a dependence on a certain version of R.
  • Imports: lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached.
  • Suggests: lists packages that are not necessarily needed. This includes packages used only in examples, tests or vignettes, and packages loaded in the body of functions.
  • Enhances: lists packages “enhanced” by the package at hand, e.g., by providing methods for classes from these packages, or ways to handle objects from these packages.
  • LinkingTo: A package that wishes to make use of header files in other packages needs to declare them as a comma-separated list in the field ‘LinkingTo’ in the DESCRIPTION file.

Package related functions from package 'utils'

  1. inst - a data frame with columns as the matrix returned by installed.packages plus "Status", a factor with levels c("ok", "upgrade"). Note: the manual does not mention "unavailable" case (but I do get it) in R 3.2.0?
  2. avail - a data frame with columns as the matrix returned by available.packages plus "Status", a factor with levels c("installed", "not installed", "unavailable"). Note: I don't get the "unavailable" case in R 3.2.0?
> x <- packageStatus()
> names(x)
[1] "inst"  "avail"
> dim(x'inst')
[1] 225  17
> x'inst'[1:3, ]
              Package                            LibPath Version Priority               Depends Imports
acepack       acepack C:/Program Files/R/R-3.1.2/library 1.3-3.3     <NA>                  <NA>    <NA>
adabag         adabag C:/Program Files/R/R-3.1.2/library     4.0     <NA> rpart, mlbench, caret    <NA>
affxparser affxparser C:/Program Files/R/R-3.1.2/library  1.38.0     <NA>          R (>= 2.6.0)    <NA>
           LinkingTo                                                        Suggests Enhances
acepack         <NA>                                                            <NA>     <NA>
adabag          <NA>                                                            <NA>     <NA>
affxparser      <NA> R.oo (>= 1.18.0), R.utils (>= 1.32.4),\nAffymetrixDataTestFiles     <NA>
                      License License_is_FOSS License_restricts_use OS_type MD5sum NeedsCompilation Built
acepack    MIT + file LICENSE            <NA>                  <NA>    <NA>   <NA>              yes 3.1.2
adabag             GPL (>= 2)            <NA>                  <NA>    <NA>   <NA>               no 3.1.2
affxparser        LGPL (>= 2)            <NA>                  <NA>    <NA>   <NA>             <NA> 3.1.1
                Status
acepack             ok
adabag              ok
affxparser unavailable
> dim(x'avail')
[1] 6538   18
> x'avail'[1:3, ]
                Package Version Priority                        Depends        Imports LinkingTo
A3                   A3   0.9.2     <NA> R (>= 2.15.0), xtable, pbapply           <NA>      <NA>
ABCExtremes ABCExtremes     1.0     <NA>      SpatialExtremes, combinat           <NA>      <NA>
ABCanalysis ABCanalysis   1.0.1     <NA>                    R (>= 2.10) Hmisc, plotrix      <NA>
                       Suggests Enhances    License License_is_FOSS License_restricts_use OS_type Archs
A3          randomForest, e1071     <NA> GPL (>= 2)            <NA>                  <NA>    <NA>  <NA>
ABCExtremes                <NA>     <NA>      GPL-2            <NA>                  <NA>    <NA>  <NA>
ABCanalysis                <NA>     <NA>      GPL-3            <NA>                  <NA>    <NA>  <NA>
            MD5sum NeedsCompilation File                                      Repository        Status
A3            <NA>             <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
ABCExtremes   <NA>             <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed
ABCanalysis   <NA>             <NA> <NA> http://cran.rstudio.com/bin/windows/contrib/3.1 not installed

download.packages() for source package

Consider the esimate package hosted on r-forge.

download.packages("estimate", destdir = "~/Downloads", 
   repos = "https://R-Forge.R-project.org")

tools package

db <- tools::CRAN_package_db()
nRcpp <- length(tools::dependsOnPkgs("Rcpp", recursive=FALSE, installed=db) )
nCompiled <- table(db[, "NeedsCompilation"])[["yes"]]
propRcpp <- nRcpp / nCompiled * 100
  • package.dependencies(), pkgDepends(), etc are deprecated now, mostly in favor of package_dependencies() which is both more flexible and efficient. See R 3.3.0 News. For example, tools::package_dependencies(c("remotes", "devtools"), recursive=TRUE) shows remotes has only a few dependencies while devtools has a lot.

crandep package

https://cran.r-project.org/web/packages/crandep/index.html. Useful to find reverse dependencies. ?get_dep. Consider the abc package:

get_dep("abc", "depends") # abc depends on these packages
                          # note my computer does not have 'abc' installed 
#   from       to    type reverse
# 1  abc abc.data depends   FALSE
# 2  abc     nnet depends   FALSE
# 3  abc quantreg depends   FALSE
# 4  abc     MASS depends   FALSE
# 5  abc   locfit depends   FALSE

get_dep("abc", "reverse_depends")
#    from       to    type reverse
# 1  abc abctools depends    TRUE
# 2  abc  EasyABC depends    TRUE

x <- get_dep("RcppEigen", c("reverse linking to")) 
dim(x)
# [1] 331   4
head(x, 3)
#        from      to       type reverse
# 1 RcppEigen   abess linking to    TRUE
# 2 RcppEigen    acrt linking to    TRUE
# 3 RcppEigen ADMMnet linking to    TRUE

How does this package depend on this other package

How does this package depend on this other package?, pak::pkg_deps_explain()

pkgndep

remotes

remotes::local_package_deps(dependencies=TRUE) will find and return all dependent packages based on the "DESCRIPTION" file. See an example here.

Bioconductor's pkgDepTools package

The is an example of querying the dependencies of the notorious 'lumi' package which often broke the installation script. I am using R 3.2.0 and Bioconductor 3.1.

The getInstallOrder function is useful to get a list of all (recursive) dependency packages.

source("http://bioconductor.org/biocLite.R")
if (!require(pkgDepTools)) {
  biocLite("pkgDepTools", ask = FALSE)
  library(pkgDepTools)
}
MkPlot <- FALSE

library(BiocInstaller)
biocUrl <- biocinstallRepos()["BioCsoft"]
biocDeps <- makeDepGraph(biocUrl, type="source", dosize=FALSE) # pkgDepTools defines its makeDepGraph()

PKG <- "lumi"
if (MkPlot) {
  if (!require(Biobase))  {
    biocLite("Biobase", ask = FALSE)
    library(Biobase)
  }
  if (!require(Rgraphviz))  {
    biocLite("Rgraphviz", ask = FALSE) 
    library(Rgraphviz)
  }
  categoryNodes <- c(PKG, names(acc(biocDeps, PKG)1))  
  categoryGraph <- subGraph(categoryNodes, biocDeps) 
  nn <- makeNodeAttrs(categoryGraph, shape="ellipse") 
  plot(categoryGraph, nodeAttrs=nn)   # Complete but plot is too complicated & font is too small.
}

system.time(allDeps <- makeDepGraph(biocinstallRepos(), type="source",
                           keep.builtin=TRUE, dosize=FALSE)) # takes a little while
#    user  system elapsed 
# 175.737  10.994 186.875 
# Warning messages:
# 1: In .local(from, to, graph) : edges replaced: ‘SNPRelate|gdsfmt’
# 2: In .local(from, to, graph) :
#   edges replaced: ‘RCurl|methods’, ‘NA|bitops’

# When needed.only=TRUE, only those dependencies not currently installed are included in the list.
x1 <- sort(getInstallOrder(PKG, allDeps, needed.only=TRUE)$packages); x1
 [1] "affy"                              "affyio"                           
 [3] "annotate"                          "AnnotationDbi"                    
 [5] "base64"                            "beanplot"                         
 [7] "Biobase"                           "BiocParallel"                     
 [9] "biomaRt"                           "Biostrings"                       
[11] "bitops"                            "bumphunter"                       
[13] "colorspace"                        "DBI"                              
[15] "dichromat"                         "digest"                           
[17] "doRNG"                             "FDb.InfiniumMethylation.hg19"     
[19] "foreach"                           "futile.logger"                    
[21] "futile.options"                    "genefilter"                       
[23] "GenomeInfoDb"                      "GenomicAlignments"                
[25] "GenomicFeatures"                   "GenomicRanges"                    
[27] "GEOquery"                          "ggplot2"                          
[29] "gtable"                            "illuminaio"                       
[31] "IRanges"                           "iterators"                        
[33] "labeling"                          "lambda.r"                         
[35] "limma"                             "locfit"                           
[37] "lumi"                              "magrittr"                         
[39] "matrixStats"                       "mclust"                           
[41] "methylumi"                         "minfi"                            
[43] "multtest"                          "munsell"                          
[45] "nleqslv"                           "nor1mix"                          
[47] "org.Hs.eg.db"                      "pkgmaker"                         
[49] "plyr"                              "preprocessCore"                   
[51] "proto"                             "quadprog"                         
[53] "RColorBrewer"                      "Rcpp"                             
[55] "RCurl"                             "registry"                         
[57] "reshape"                           "reshape2"                         
[59] "rngtools"                          "Rsamtools"                        
[61] "RSQLite"                           "rtracklayer"                      
[63] "S4Vectors"                         "scales"                           
[65] "siggenes"                          "snow"                             
[67] "stringi"                           "stringr"                          
[69] "TxDb.Hsapiens.UCSC.hg19.knownGene" "XML"                              
[71] "xtable"                            "XVector"                          
[73] "zlibbioc"                         

# When needed.only=FALSE the complete list of dependencies is given regardless of the set of currently installed packages.
x2 <- sort(getInstallOrder(PKG, allDeps, needed.only=FALSE)$packages); x2
 [1] "affy"                              "affyio"                            "annotate"                         
 [4] "AnnotationDbi"                     "base64"                            "beanplot"                         
 [7] "Biobase"                           "BiocGenerics"                      "BiocInstaller"                    
[10] "BiocParallel"                      "biomaRt"                           "Biostrings"                       
[13] "bitops"                            "bumphunter"                        "codetools"                        
[16] "colorspace"                        "DBI"                               "dichromat"                        
[19] "digest"                            "doRNG"                             "FDb.InfiniumMethylation.hg19"     
[22] "foreach"                           "futile.logger"                     "futile.options"                   
[25] "genefilter"                        "GenomeInfoDb"                      "GenomicAlignments"                
[28] "GenomicFeatures"                   "GenomicRanges"                     "GEOquery"                         
[31] "ggplot2"                           "graphics"                          "grDevices"                        
[34] "grid"                              "gtable"                            "illuminaio"                       
[37] "IRanges"                           "iterators"                         "KernSmooth"                       
[40] "labeling"                          "lambda.r"                          "lattice"                          
[43] "limma"                             "locfit"                            "lumi"                             
[46] "magrittr"                          "MASS"                              "Matrix"                           
[49] "matrixStats"                       "mclust"                            "methods"                          
[52] "methylumi"                         "mgcv"                              "minfi"                            
[55] "multtest"                          "munsell"                           "nleqslv"                          
[58] "nlme"                              "nor1mix"                           "org.Hs.eg.db"                     
[61] "parallel"                          "pkgmaker"                          "plyr"                             
[64] "preprocessCore"                    "proto"                             "quadprog"                         
[67] "RColorBrewer"                      "Rcpp"                              "RCurl"                            
[70] "registry"                          "reshape"                           "reshape2"                         
[73] "rngtools"                          "Rsamtools"                         "RSQLite"                          
[76] "rtracklayer"                       "S4Vectors"                         "scales"                           
[79] "siggenes"                          "snow"                              "splines"                          
[82] "stats"                             "stats4"                            "stringi"                          
[85] "stringr"                           "survival"                          "tools"                            
[88] "TxDb.Hsapiens.UCSC.hg19.knownGene" "utils"                             "XML"                              
[91] "xtable"                            "XVector"                           "zlibbioc" 

> sort(setdiff(x2, x1)) # Not all R's base packages are included; e.g. 'base', 'boot', ...
 [1] "BiocGenerics"  "BiocInstaller" "codetools"     "graphics"      "grDevices"    
 [6] "grid"          "KernSmooth"    "lattice"       "MASS"          "Matrix"       
[11] "methods"       "mgcv"          "nlme"          "parallel"      "splines"      
[16] "stats"         "stats4"        "survival"      "tools"         "utils"  

Lumi rgraphviz.svg

Bioconductor BiocPkgTools

Collection of simple tools for learning about Bioc Packages. Functionality includes access to :

  • Download statistics
  • General package listing
  • Build reports
  • Package dependency graphs
  • Vignettes

Overview of BiocPkgTools & Dependency graphs

BiocPkgTools: Toolkit for Mining the Bioconductor Package Ecosystem in biorxiv.org.

miniCRAN package

miniCRAN package can be used to identify package dependencies or create a local CRAN repository. It can be used on repositories other than CRAN, such as Bioconductor.

Before we go into R, we need to install some packages from Ubuntu terminal. See here.

# Consider glmnet package (today is 4/29/2015)
# Version:	2.0-2
# Depends:	Matrix (≥ 1.0-6), utils, foreach
# Suggests:	survival, knitr, lars
if (!require("miniCRAN"))  {
  install.packages("miniCRAN", dependencies = TRUE, repos="http://cran.rstudio.com") # include 'igraph' in Suggests.
  library(miniCRAN)
}
if (!"igraph" %in% installed.packages()[,1]) install.packages("igraph")

tags <- "glmnet"
pkgDep(tags, suggests=TRUE, enhances=TRUE) # same as pkgDep(tags)
#  [1] "glmnet"    "Matrix"    "foreach"   "codetools" "iterators" "lattice"   "evaluate"  "digest"   
#  [9] "formatR"   "highr"     "markdown"  "stringr"   "yaml"      "mime"      "survival"  "knitr"    
# [17] "lars"   

dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE) # miniCRAN defines its makeDepGraph()
plot(dg, legendPosition = c(-1, 1), vertex.size=20)

MiniCRAN dep.svg PkgDepTools dep.svg Glmnet dep.svg

We can also display the dependence for a package from the Bioconductor repository.

tags <- "DESeq2"
# Depends	S4Vectors, IRanges, GenomicRanges, Rcpp (>= 0.10.1), RcppArmadillo (>= 0.3.4.4)
# Imports	BiocGenerics(>= 0.7.5), Biobase, BiocParallel, genefilter, methods, locfit, geneplotter, ggplot2, Hmisc
# Suggests	RUnit, gplots, knitr, RColorBrewer, BiocStyle, airway,\npasilla (>= 0.2.10), DESeq, vsn
# LinkingTo     Rcpp, RcppArmadillo
index <- function(url, type="source", filters=NULL, head=5, cols=c("Package", "Version")){
  contribUrl <- contrib.url(url, type=type)
  available.packages(contribUrl, type=type, filters=filters)
}

bioc <- local({
  env <- new.env()
  on.exit(rm(env))
  evalq(source("http://bioconductor.org/biocLite.R", local=TRUE), env)
  biocinstallRepos() # return URLs
})

bioc
#                                               BioCsoft 
#            "http://bioconductor.org/packages/3.0/bioc" 
#                                                BioCann 
# "http://bioconductor.org/packages/3.0/data/annotation" 
#                                                BioCexp 
# "http://bioconductor.org/packages/3.0/data/experiment" 
#                                              BioCextra 
#           "http://bioconductor.org/packages/3.0/extra" 
#                                                   CRAN 
#                                "http://cran.fhcrc.org" 
#                                              CRANextra 
#                   "http://www.stats.ox.ac.uk/pub/RWin" 
str(index(bioc["BioCsoft"])) # similar to cranJuly2014 object 

system.time(dg <- makeDepGraph(tags, suggests=TRUE, enhances=TRUE, availPkgs = index(bioc["BioCsoft"]))) # Very quick!
plot(dg, legendPosition = c(-1, 1), vertex.size=20)

Deseq2 dep.svg Lumi dep.svg

The dependencies of GenomicFeature and GenomicAlignments are more complicated. So we turn the 'suggests' option to FALSE.

tags <- "GenomicAlignments"
dg <- makeDepGraph(tags, suggests=FALSE, enhances=FALSE, availPkgs = index(bioc["BioCsoft"]))
plot(dg, legendPosition = c(-1, 1), vertex.size=20)

Genomicfeature dep dep.svg Genomicalignments dep.svg

Github repository

Submitting R package to CRAN

cranlike

https://github.com/r-hub/cranlike

cranlike keeps the package data in a SQLite database, in addition to the PACKAGES* files. This database is the canonical source of the package data. It can be updated quickly, to add and remove packages. The PACKAGES* files are generated from the database.

MRAN (CRAN only) & checkpoint package

According to the snapsot list here, the oldest version is 2014-08-18 which corresponds to R 3.1.0.

checkpoint package

library(checkpoint)
checkpoint("2015-03-31")

Note the Bioconductor packages have no similar solution.

groundhog package

  • groundhog: Version-Control for CRAN, GitHub, and GitLab Packages (Bioconductor?)
  • It seems groundhog is like checkpoint but groundhog does not depend on MRAN and it will figure out the package dependencies by itself.
  • MRAN is getting shutdown - what else is there for reproducibility with R, or why reproducibility is on a continuum? The author provides an example where we can integrate "groundhog" in the Dockerfile for reproducibility. Pay attention to the sentence (appear 2 times) “why use Docker at all? Since it’s easy to install older versions of R on Windows and macOS, wouldn’t an renv.lock file suffice? Or even just {groundhog} which is arguably even easier to use?”
  • MRAN is getting shutdown #593 related to Rocker project. Rocker now uses RSPM (PPM now) from RStudio.
  • groundhog.library() differs from the library() function: 1) it installed and loaded packages in one step, 2) it allowed to install multiple packages.

Note I use the docker's R since that's the easiest way to use an old version of R (e.g. for some old R script) in Linux. Note that it will create a new folder R_groundhog folder in the working directory (see the message below).

$ docker run --rm -it -v $(pwd):/home/docker \
    -w /home/docker -u docker r-base:4.0.2 R

> install.packages("groundhog")
> library(groundhog")
groundhog needs authorization to save files to '/home/docker/R_groundhog'
Enter 'OK' to provide authorization
OK
The groundhog folder path is now:
/home/docker/R_groundhog/groundhog_library/
Loaded 'groundhog' (version:2.1.0) using R-4.0.2
Tips and troubleshooting: https://groundhogR.com

> groundhog.library("
  library(ggplot2)",
    "2020-10-10")
> library()
# Packages in library ‘/usr/local/lib/R/site-library’:
> packageVersion("ggplot2")
[1] ‘3.3.2’   # the latest version is 3.4.0 on R 4.2.2
> library(ggplot2)

(Current groundhog is v3.1.0) If I don't specify a user in docker run, I'll need to call groundhog.library() twice in order to install packages (eg. "DT" package). In this case I can use groundhog:::save.cookie("copy_instead_of_renaming") before calling groundhog.library().

$ docker run -it --rm rocker/verse:4.3.0 bash
root@58978695ec12:/# R
R version 4.3.0 (2023-04-21) -- "Already Tomorrow"
...
> install.packages("groundhog"); library(groundhog)

> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/local/lib/R/library"
> get.groundhog.folder()
[1] "/root/R_groundhog/groundhog_library/"

> groundhog:::save.cookie("copy_instead_of_renaming") 
groundhog needs authorization to save files to '/root/R_groundhog/'
Enter 'OK' to provide authorization, and 'NO' not to.
|   >OK

Groundhog folder set to: '/root/R_groundhog/groundhog_library/

--- You may change it with`set.groundhog.folder(<path>)`---
Downloading database with information for all CRAN packages ever published
trying URL 'http://s3.wasabisys.com/groundhog/cran.toc.rds'
Content type 'application/octet-stream' length 2010504 bytes (1.9 MB)
==================================================
downloaded 1.9 MB

Downloading database with installation times for all source packages on CRAN
trying URL 'http://s3.wasabisys.com/groundhog/cran.times.rds'
Content type 'application/octet-stream' length 803912 bytes (785 KB)
==================================================
downloaded 785 KB
> groundhog.library("DT", "2023-4-23")
> find.package("DT")

rang

rang: make ancient R code run again

cranly

R package dependence trees

sessioninfo

tmp = session_info("sessioninfo")
dim(tmp$packages) # [1]  7 11

tmp = session_info("tidyverse")
dim(tmp$packages) # [1] 95 11

Reverse dependence

Install packages offline

http://www.mango-solutions.com/wp/2017/05/installing-packages-without-internet/

Install a packages locally and its dependencies

It's impossible to install the dependencies if you want to install a package locally. See Windows-GUI: "Install Packages from local zip files" and dependencies

A minimal R package (for testing purpose)

An R package that does not require others during install.packages()

Create a new R package, namespace, documentation

Package structure

http://r-pkgs.had.co.nz/package.html. On Linux/macOS, use tree -d DIRNAME to show the directories only. At a minimum, we will have R and man directories.

  • ChangeLog
  • DESCRIPTION
  • MD5
  • NAMESPACE
  • R/
    • zzz.R
  • build/
    • Package.pdf (eg dplyr)
    • vignette.rds
  • data/
  • demo/
  • inst/
    • extdata/
    • doc/
      • FileName.R
      • FileName.Rmd
      • FileName.html
    • include/
    • othersYouInclude/
    • tinytest/
    • CITATION
  • man/
    • figures/
  • src/
  • tests/
    • testthat
  • vignettes/

NAMESPACE and DESCRIPTION

Namespace dependencies not required. If you use import or importFrom in your NAMESPACE file, you should have an entry for that package in the Imports section of your DESCRIPTION file (unless there is a reason that you need to use Depends in which case the package should have an entry in Depends, and not Imports).

license

Install software for PDF output

Windows: Rtools

Installing RTools for Compiled Code via Rcpp. Just remember to check the option to include some paths in the PATH environment variable.

R CMD

R Installation and Administration

  • R CMD build XXX. Note this will not create pdf files for vignettes. The output is a tarball.
  • R CMD INSTALL
  • R CMD REMOVE
  • R CMD SHLIB files. For example, "Rcmd shlib *.f *.c -o surv.dll" on Windows.
  • R CMD make
  • R CMD check XXX. Useful if we want to create reference manual (PDF file). See R create reference manual with R CMD check.
  • R CMD javareconf

usethis package

Github Actions

To use the Github Actions for continuous integration/CI checks,

  • I first follow this to run usethis::use_github_action_check_release(). Once I commit and push the files to Github, Github Actions are kicked off. The R console also tell me to copy and paste a line to add a workflow's badge to README.md.
  • Then I modify the yaml file to become this to run a standard check. This took 3m24s to run.
  • Then I further delete the 'devel' line (line 23) to reduce one more platform to check. This took 3m to check. My example is on Github (rtoy).

I also try to follow Github Actions with R and create a pkgdown/package documentation web page.

  • usethis::use_github_actions("pkgdown") Now go to Github repo's Settings -> Options and scroll down until you see Github Pages. For Source, the page site should be set to being built from the root folder of the gh-pages.
  • I have used Jekyll to create a gh-page. I don't need to delete anything for this new gh-pages. I just need to go to the repository setting and (scroll down until we see Github Pages) change the Source of Github Pages to 'gh-pages branch' from 'master branch'.
  • The files on the gh-pages branch are generated by Github Actions; these files are not available on my local machine. My location machine only has .github/workflows/pkgdown.yaml file.

Question:

  • The workflow file specifies R version and OS platform.
  • Right now the workflow file (like pkgdown) is using "r-lib/actions/setup-r@master" that has an "action.yml" file. The r-version is '3.x' only. What about if R 4.0.0 is released?

Packages, webpages and Github

Tutorials by Lisa DeBruine

biocthis

biocthis, slides

R package depends vs imports

In the namespace era Depends is never really needed. All modern packages have no technical need for Depends anymore. Loosely speaking the only purpose of Depends today is to expose other package's functions to the user without re-exporting them.

load = functions exported in myPkg are available to interested parties as myPkg::foo or via direct imports - essentially this means the package can now be used

attach = the namespace (and thus all exported functions) is attached to the search path - the only effect is that you have now added the exported functions to the global pool of functions - sort of like dumping them in the workspace (for all practical purposes, not technically)

import a function into a package = make sure that this function works in my package regardless of the search path (so I can write fn1 instead of pkg1::fn1 and still know it will come from pkg1 and not someone's workspace or other package that chose the same name)


The distinction is between "loading" and "attaching" a package. Loading it (which would be done if you had MASS::loglm, or imported it) guarantees that the package is initialized and in memory, but doesn't make it visible to the user without the explicit MASS:: prefix. Attaching it first loads it, then modifies the user's search list so the user can see it.

Loading is less intrusive, so it's preferred over attaching. Both library() and require() would attach it.

import() and importFrom()

If our package depends on a package, we need to made some changes. Below we assume the package glmnet in our new package.

  • DESCRIPTION: Imports: glmnet
  • NAMESPACE: either import(glmnet) to import all functions from glmnet or importFrom(glmnet, cv.glmnet) to import 'cv.glmnet' only
  • hello.R: nothing needs to be added

For more resource, see

R package suggests

stringr has suggested htmlwidgets. An error will come out if the suggested packages are not available.

> library(stringr)
> str_view(c("abc", "a.c", "bef"), "a\\.c")
Error in loadNamespace(name) : there is no package called ‘htmlwidgets’

Useful functions for accessing files in packages

> system.file(package = "batr")
[1] "f:/batr"
> system.file("extdata", "logo.png", package = "cowplot") # Mac
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/cowplot/extdata/logo.png"

> path.package("batr")
[1] "f:\\batr"
> path.package("ggplot2") # Mac
[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/ggplot2"

# sometimes it returns the forward slash format for some reason; C:/Program Files/R/R-3.4.0/library/batr
# so it is best to add normalizePath().
> normalizePath(path.package("batr"))

> file.path("f:", "git", "surveyor")
[1] "f:/git/surveyor"

Internal functions

RStudio shortcuts

RStudio Build.png

available package

available. Check if a given package name is available to use. It checks the name's validity. Checks if it is used on 'GitHub', 'CRAN' and 'Bioconductor'.

Create an R package

Your first R package in 1 hour

Using usethis

New R Package 'foo' -- Updated

  1. setwd()
  2. usethis::create_package("foo")
  3. usethis::use_git(); usethis_github()
  4. usethis::use_mit_license("Your name")
  5. usethis::use_r("foo_function")
  6. usethis::use_package("dplyr") # specify import dependency, e.g. dplyr package
  7. usethis::use_testthat()
  8. usethis::use_test("firsttest")
  9. setwd("..")
  10. roxygen2::roxygenise()

Using devtools and roxygen2

How to use devtools::load_all("FolderName"). load_all() loads any modified R files, and recompile and reload any modified C or Fortran files.

# Step 1
library(devtools)

# Step 2
dir.create(file.path("MyCode", "R"), recursive = TRUE)
cat("foo=function(x){x*2}", file = file.path("MyCode", "R", "foo.R"))
write.dcf(list(Package = "MyCode", Title = "My Code for this project", Description = "To tackle this problem", 
    Version = "0.0", License = "For my eyes only", Author = "First Last <[email protected]>", 
    Maintainer = "First Last <[email protected]>"), file = file.path("MyCode", "DESCRIPTION"))
# OR
# create("path/to/package/pkgname")
# create() will create R/ directory, DESCRIPTION and NAMESPACE files.

# Step 3 (C/Fortran code, optional)
dir.create(file.path("MyCode", "src"))
cat("void cfoo(double *a, double *b, double *c){*c=*a+*b;}\n", file = file.path("MyCode", 
    "src", "cfoo.c"))
cat("useDynLib(MyCode)\n", file = file.path("MyCode", "NAMESPACE"))

# Step 4 
load_all("MyCode")

# Step 5
# Modify R/C/Fortran code and run load_all("MyCode")

# Step 6 (Automatically generate the documentation from R source code, 
#         in the man/ folder and export the function in NAMESPACE file.
#         optional, repeat if any function's help has changed)
document("MyCode")

# Step 7 (check the package, optional)
check("MyCode")

# Step 8 (Deployment, create a tarball, 
#         optional, repeat if necessary)
build("MyCode")

# Step 9 (Install the package, optional)
install("MyCode")

Note:

  1. load_all("FolderName") will make the FolderName to become like a package to be loaded into the current R session so the 2nd item returned from search() will be "package:FolderName". However, the FolderName does not exist under Program Files/R/R-X.Y.Z/library nor Documents/R/win-library/X.Y/ (Windows OS).
  2. build("FolderName") will create a tarball in the current directory. User can install the new package for example using Packages -> Install packages from local files on Windows OS. This will build/run vignettes so it may take some time. The tarball will contain a build folder containing 'vignette.rds' file. It'll also create a new folder inst/doc containing 3 files (MyPkg.html, MyPkg.Rmd and MyPkg.R). The vignettes may contain a new folder MyPkg_cache if we use chunk = TRUE option in Rmd file. Note install.packages() will not run the R code in vignettes.
  3. For the simplest R package, the source code only contains a file <DESCRIPTION> and a folder <R> with individual R files in the text format.

Using RStudio

  • Youtube
  • How to Create and Distribute an R Package.
    • The goal of the article to set up an easily installable R package on Github for others to use via remotes::install_github().
    • The main tools required are RStudio along with the packages roxygen2 and usethis.
    • How to take care of Bioconductor and Github Dependencies
    • How to publish your package to Github
  • Building a Corporate R Package for Pleasure and Profit
  • Toy example*
    1. In RStudio, click "File" - "New Project" - "New Directory" - "R package". Package name = "a1" (this naming will guarantee this package will be shown on top of all R packages in RStudio). Press 'Create Project'. This new project folder will have necessary files for an R package including DESCRIPTION, NAMESPACE, R/hello.R, man/hello.Rd. The "hello.R" file will be opened in RStudio automatically.
    2. Create a new R file under a1/R folder. An example of this R file <add.R> containing roxygen comments can be found under here. Pressing Ctrl/Cmd + Shift + D (or running devtools::document()) will generate a man/add.Rd.
    3. In R, type usethis::use_vignette("my-vignette") to create a new vignette. The new vignette "my-vignette.Rmd" will be saved under "a1/vignettes" subfolder. We can modify the Rmd file as we need.
    4. In RStudio, click "Build" - "Build Source Package". You will see some messages on the "Build" tab of the top-right panel. Eventually, a tarball "/home/$USERNAME/a1_0.1.0.tar.gz" is created (I create the project under /home/$USERNAME directory).
    5. We can install the package in R install.packages("~/a1_0.1.0.tar.gz", repos= NULL, type = "source") if we have already created the tarball. Another method is to use RStudio "Build" - "Install and Restart" (Ctrl + Shift + B).
    6. In RStudio, type help(package = "a1") or click "Packages" tab on the bottom-right panel and click "a1" package. It will show a line "User guides, package vignettes and other documentation". The vignette we just created will be available in HTML, source and R code format.

Using RStudio.cloud

There is a problem. We can use devtools::create("/cloud/project") to create a new package. When it builds the source package, the package file will be located in the root directory of the package. However, if we use a local RStudio to create a source package, the source package will be located in the upper directory.

Binary packages

  • No .R files in the R/ directory. There are 3 files that store the parsed functions in an efficient file format. This is the result of loading all the R code and then saving the functions with save().
  • A Meta/ directory contains a number of Rds files. These files contain cached metadata about the package, like what topics the help files cover and parsed version of the DESCRIPTION file.
  • An html/ directory.
  • libs/ directory if you have any code in the src/' directory
  • The contents of inst/ are moved to the top-level directory.

Building the tarball

  • No matter we uses devtools::build() or the terminal R CMD build MyPkg it will uses run the R code in vignette. Be cautious on the extra time and storage the process incurred.
  • If 'cache = TRUE' is used in vignettes, it will create a new subfolder called MyuPkg_cache under the vignettes folder. This takes a lot of space (eg 1GB in some case).

Building the binary

R CMD INSTALL --build MyPkg.tar.gz
# OR
R CMD INSTALL --build Full_Path_Of_MyPkg 

The binary (on Windows) can be installed by install.packages("Mypkg.zip",repos=NULL)

If the installation is successful, it will overwrite any existing installation of the same package. To prevent changes to the present working installation or to provide an install location with write access, create a suitably located directory with write access and use the -l option to build the package in the chosen location.

R CMD INSTALL -l location --build pkg

R folder

See an example from DuoClustering2018.

#' @importFrom utils read.csv
.onLoad <- function(libname, pkgname) {
  fl <- system.file("extdata", "metadata.csv", package = "DuoClustering2018")
  titles <- utils::read.csv(fl, stringsAsFactors = FALSE)$Title
  ExperimentHub::createHubAccessors(pkgname, titles)
}

Note that the environment of a function from the DuoClustering2018 package is not the package name.

environment(clustering_summary_filteredExpr10_TrapnellTCC_v2)
<environment: 0x7fe01dbe7dd0>

Q: where is the definition of DuoClustering2018::clustering_summary_filteredExpr10_TrapnellTCC_v2()? A: createHubAccessors() - Creating A Hub Package: ExperimentHub or AnnotationHub.

data

http://r-pkgs.had.co.nz/data.html

Three ways to include data in your package.

  • If you want to store binary data and make it available to the user, put it in data/. This is the best place to put example datasets.
  • If you want to store parsed data, but not make it available to the user, put it in R/sysdata.rda. This is the best place to put data that your functions need.
  • If you want to store raw data, put it in inst/extdata. See External data. An example from tximportData package.
    # grep -r extdata /home/brb/R/x86_64-pc-linux-gnu-library/4.0
    logo_file <- system.file("extdata", "logo.png", package = "cowplot")
    orgDBLoc = system.file("extdata", "org.Hs.eg.sqlite", package="org.Hs.eg.db")
    
    # grep -r readRDS /home/brb/R/x86_64-pc-linux-gnu-library/4.0
    patient.data  <- readRDS("assets/coxnet.RDS")
    

How to distribute data with your R package

Rd file

Using Mathjax in Rd Files

Vignette

Long execution for R code in vignette

NEWS

Why and how maintain a NEWS file for your R package?

README.Rmd & README.md files

See Releasing a package from R packages by Hadley Wickham.

How to convert .Rmd into .md in R studio?

Example: ggplot2 repository at Github

It seems RStudio cannot create TOC for *.md files. glmnet package creates TOC of its vignette by itself. Visual Studio Code has an extension to do that.

badge

badger: Badge for R Package

tests folder and testthat package

.Rbuildignore

Non-standard files/directories, Rbuildignore and inst

URL checker

What is a library?

A library is simply a directory containing installed packages.

You can use .libPaths() to see which libraries are currently active.

.libPaths()

lapply(.libPaths(), dir)

Object names

  • Variable and function names should be lower case.
  • Use an underscore (_) to separate words within a name (reserve . for S3 methods).
  • Camel case is a legitimate alternative, but be consistent! For example, preProcess(), twoClassData, createDataPartition(), trainingRows, trainPredictors, testPredictors, trainClasses, testClasses have been used in Applied Predictive Modeling by Kuhn & Johnson.
  • Generally, variable names should be nouns and function names should be verb.

Spacing

  • Add a space around the operators +, -, \ and *.
  • Include a space around the assignment operators, <- and =.
  • Add a space around any comparison operators such as == and <.

Indentation

  • Use two spaces to indent code.
  • Never mix tabs and spaces.
  • RStudio can automatically convert the tab character to spaces (see Tools -> Global options -> Code).

\dontrun{}

formatR and lintr package

Use formatR package to clean up poorly formatted code

install.packages("formatR")
formatR::tidy_dir("R")

Another way is to use the lintr package (lint).

install.packages("lintr")
lintr:::lin_package()

Rcpp

Thirteen Simple Steps for Creating An R Package with an External C++ Library

C library

Using R — Packaging a C library in 15 minutes

Data package

Chapter 12 Create a data package from rstudio4edu

Minimal R package for submission

https://stat.ethz.ch/pipermail/r-devel/2013-August/067257.html and CRAN Repository Policy.

Create R Windows Binary on non-Windows OS

r-hub/rhub package: the R package builder service

https://github.com/r-hub/proposal, R-hub v2 2024/4.

# Today 1/1/2020
$ git clone https://github.com/arraytools/rtoy.git
$ rm -rf rtoy/.git
$ rm rtoy/.gitignore rtoy/_config.yml
$ R
> install.packages("devtools", repos = "https://cran.rstudio.com")
> install.packages("rhub")
> devtools::install_github("r-hub/sysreqs") #needed before calling local_check_linux()

> pkg_path <- "~/Downloads/rtoy"
> chk <- local_check_linux(pkg_path, image = "rhub/debian-gcc-release")

─  Building package

Container name: 6dca434d-84f9-42e2-ab83-b8c364594476-2
It will _not_ be removed after the check.

R-hub Linux builder script v0.10.0 (c) R Consortium, 2018-2019

Package: /tmp/RtmpviOCT1/file470972a3c4ab/rtoy_0.1.0.tar.gz
Docker image: rhub/debian-gcc-release
Env vars:
R CMD check arguments:
Unable to find image 'rhub/debian-gcc-release:latest' locally
latest: Pulling from rhub/debian-gcc-release
Digest: sha256:a9e01ca57bfd44f20eb6719f0bfecdd8cf0f59610984342598a53f11555b515d
Status: Downloaded newer image for rhub/debian-gcc-release:latest
Sysreqs platform: linux-x86_64-debian-gcc
No system requirements

>>>>>==================== Installing system requirements
8fa4f66e41954b4ed1112eb72e76683c28345863fcc7e260edac6a9a30387fed

>>>>>==================== Starting Docker container
2dfae75c9de95b598ff8d9cd19d3cfc7ffe7f16edbc3d4f81c896e4e6b956ebd
ls: cannot access '/opt/R-*': No such file or directory
> source('https://bioconductor.org/biocLite.R')
Error: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install
Execution halted
Error in run(bash, c(file.path(wd, "rhub-linux.sh"), args), echo = TRUE,  :
  System command error

Moreover, it create a new Docker image and a new Docker container. We need to manually clean them:-(

Running a check on its own (remote) server works

> check_on_linux("rtoy") # will run remotely. 
     # We need to verify the email and enter a token.
     # We will get a report and a full build log.
     # The report includes both the linux command and the log from the server.

Now I go back to the original method.

$ R
> install.packages("tinytex")
> tinytex::install_tinytex()
> q()
$ exit

$ sudo apt-get install texinfo
$ R CMD build rtoy
$ R CMD check rtoy_0.1.0.tar.gz

# Install pandoc
$ wget https://github.com/jgm/pandoc/releases/download/2.9.1/pandoc-2.9.1-1-amd64.deb
$ sudo dpkg -i pandoc-2.9.1-1-amd64.deb
$ R CMD check --as-cran rtoy_0.1.0.tar.gz
# Ignore a 'Note' https://stackoverflow.com/a/23831508

rcmdcheck

Run R CMD check from R and Capture Results

The rcmdcheck package was used by Github Actions for R language from r-lib/R infrastructure.

CRAN check API

Continuous Integration

Travis-CI (Linux, Mac)

Continuous Integration: Appveyor (Windows)

Github Actions

precommit

Submit packages to cran

Windows

Everything you should know about WinBuilder

Other tips/advice

C/Fortran

Rmath.h

For example pnorm5() was used by survS.h by survival package (old version) .

Packages includes Fortran

On mac, gfortran (6.1) can be downloaded from CRAN. It will be installed onto /usr/local/gfortran. Note that the binary will not be present in PATH. So we need to run the following command to make gfortran avaiialbe.

sudo ln -s /usr/local/gfortran/bin/gfortran /usr/local/bin/gfortran

A useful tool to find R packages containing Fortran code is pkgsearch package. Note

  • The result is not a comprehensive list of packages containing Fortran code.
  • It seems the result is the same as I got from https://www.r-pkg.org
> pkg_search("Fortran")
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #     package      version by                    @ title
  1 100 covr         3.3.2   Jim Hester          12d Test Coverage for Packages
  2  91 inline       0.3.15  Dirk Eddelbuettel    1y Functions to Inline C, ...
  3  43 randomForest 4.6.14  Andy Liaw            2y Breiman and Cutler's Ra...
  4  39 deSolve      1.24    Thomas Petzoldt      4M Solvers for Initial Val...
  5  27 mnormt       1.5.5   Adelchi Azzalini     3y The Multivariate Normal...
  6  26 minqa        1.2.4   Katharine M. Mullen  5y Derivative-free optimiz...
  7  24 rgcvpack     0.1.4   Xianhong Xie         6y R Interface for GCVPACK...
  8  22 leaps        3.0     Thomas Lumley        3y Regression Subset Selec...
  9  21 akima        0.6.2   Albrecht Gebhardt    3y Interpolation of Irregu...
 10  20 rootSolve    1.7     Karline Soetaert     3y Nonlinear Root Finding,...

> more()
- "Fortran" ------------------------------------ 69 packages in 0.009 seconds -
  #    package    version   by                    @ title
 11 15 BB         2019.10.1 Paul Gilbert        11d Solving and Optimizing L...
 12 15 limSolve   1.5.5.3   Karline Soetaert     2y Solving Linear Inverse M...
 13 14 insideRODE 2.0       YUZHUO PAN           7y insideRODE includes buil...
 14 13 earth      5.1.1     Stephen Milborrow    7M Multivariate Adaptive Re...
 15 13 cluster    2.1.0     Martin Maechler      4M "Finding Groups in Data"...
 16 13 spam       2.3.0.2   ORPHANED            11h SPArse Matrix
 17 12 diptest    0.75.7    Martin Maechler      4y Hartigan's Dip Test Stat...
 18 10 pbivnorm   0.6.0     Brenton Kenkel       5y Vectorized Bivariate Nor...
 19  7 optmatch   0.9.12    Mark M. Fredrickson 17d Functions for Optimal Ma...
 20  7 lsei       1.2.0     Yong Wang            2y Solving Least Squares or...

Another way is to run rsync to download src/contrib directory from CRAN and then use grep to find these packages. Note: the source packages takes about 8GB space (2019-10-28).

mkdir ~/Downloads/cran
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/*.tar.gz ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES ~/Downloads/cran/
rsync -avz --delete cran.r-project.org::CRAN/src/contrib/PACKAGES.gz ~/Downloads/cran/
cd ~/Downloads/cran
find . -xtype l -delete # remove broken symbolic links

touch tmp
for f in *.gz;
do
  tar -tzvf $f | grep -E "(\.f|\.f90|\.f95)$"  |& tee -a tmp
done

to check if the tarball contains Fortran 77/90 code.

Now to see all packages names we can process it in R

library(magrittr)
library(stringr)
x <- read.table("~/Downloads/tmp", stringsAsFactors = F)
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% unique() # 415 packages
strsplit(x$V6, "/") %>% sapply(function(x) x[1]) %>% table() %>% sort() # how many Fortran files in each package

str_subset(x$V6, "\\.f90$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 90 only packages
str_subset(x$V6, "\\.f95$") %>% strsplit("/") %>% sapply(function(x) x[1]) %>% unique() # Fortran 95 only packages

str_subset(x$V6, "BayesFM") # f95 

Misc

Datasets in R packages

https://vincentarelbundock.github.io/Rdatasets/datasets.html

Turn your analysis into a package

Organise your own analysis

Build R package faster using multicore

http://www.rexamine.com/2015/07/speeding-up-r-package-installation-process/

The idea is edit the /lib64/R/etc/Renviron file (where /lib64/R/etc/ is the result to a call to the R.home() function in R) and set:

MAKE='make -j 8' # submit 8 jobs at once

Then build R package as regular, for example,

$ time R CMD INSTALL ~/R/stringi --preclean --configure-args='--disable-pkg-config'

suppressPackageStartupMessages() and .onAttach()

KernSmooth package example.

It is Time for CRAN to Ban Package Ads

suppressPackageStartupMessages(library("dplyr"))

fusen package

Identifying R Functions & Packages Used in GitHub Repos

Identifying R Functions & Packages Used in GitHub Repos: funspotr package

CRANalerts

R-universe

FDA

Successful R-based Test Package Submitted to FDA