add a dash in the fieldbook url; recent change by phenoapps team.
[sgn.git] / R / sgnPackages.r
blob7894d5b0462011d13956fc1c28d8680fb0f79061
1 # R packages required on SGN websites
3 #removes older ones of duplicate packages
4 #installs new ones from CRAN, bioconductor, github
5 #in ~/cxgn/R_libs
7 #Leaves behind deps installed in site libraries ("/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library")
8 #To avoid future version conflicts between deps installed in these libraries and 'cxgn/sgn/R_libs', you would be better off
9 #removing them manually.
12 #Set the env variable R_LIBS_USER in /etc/R/Renviron to '~/cxgn/R_libs' in your local system or to '/home/production/cxgn/R_libs' in the production servers.
13 #This will ensure deps you manually install in R will be in the same place as sgn R libraries
14 #and avoid installation of the same R packages in multiple places.
15 #It will also add packages in the \"~/cxgn/R_libs\" to the search path";
17 rLibsUser <- unlist(strsplit(Sys.getenv("R_LIBS_USER"), .Platform$path.sep))
18 rLibsSite <- unlist(strsplit(Sys.getenv("R_LIBS_SITE"), .Platform$path.sep))
19 rLibsSgn <- "~/cxgn/R_libs"
20 cranSite <- 'http://mirror.las.iastate.edu/CRAN/'
21 cranFile <- "~/cxgn/sgn/R_files/cran"
22 gitFile <- "~/cxgn/sgn/R_files/github"
23 bioCFile <- "~/cxgn/sgn/R_files/bioconductor"
25 preRLibsSgn <- grep(rLibsSgn, rLibsUser, perl=TRUE, value=TRUE)
27 if (!is.null(preRLibsSgn) || !file.exists(preRLibsSgn)) {
28 dir.create(rLibsSgn, recursive = TRUE, showWarnings = TRUE)
31 if(!dir.exists(rLibsSgn))
33 stop("SGN R Libs dir ", rLibsSgn, ' Does not exist and failed to create it.')
34 } else {
35 Sys.setenv(R_LIBS_USER=rLibsSgn)
38 .libPaths(c(rLibsSgn, .libPaths()))
40 if (!require(stringr, lib.loc=rLibsSgn, quietly=TRUE, warn.conflicts=FALSE)) {
41 install.packages('stringr', repos=cranSite)
42 library(stringr)
45 if (!require(dplyr, lib.loc=rLibsSgn, quietly=TRUE, warn.conflicts=FALSE)) {
46 install.packages('dplyr', repos=cranSite)
47 library(dplyr)
50 if (!require(devtools, lib.loc=rLibsSgn, quietly=TRUE, warn.conflicts=FALSE)) {
51 install.packages('devtools', repos=cranSite)
52 library(devtools)
56 installedPackages <- function (..., lib.loc=NULL) {
58 pks <- installed.packages(..., lib.loc=lib.loc)
59 pks <- pks[, c('Package', 'LibPath', 'Version')]
60 pks <- data.frame(pks, stringsAsFactors=FALSE)
65 duplicatedPackagesDf <- function(packagesDf) {
67 dupPackages <- packagesDf %>%
68 group_by(Package) %>%
69 filter(n()>1) %>%
70 arrange(Package) %>%
71 data.frame()
73 if (is.data.frame(dupPackages) == FALSE) {
74 dupPackages <- NULL
79 dupPackNames <- function(dupPackDf) {
81 dupsNames <- c()
82 if(!is.null(dupPackDf)) {
83 if (is.data.frame(dupPackDf)) {
84 dupsNames <- dupPackDf %>%
85 select(Package) %>%
86 distinct() %>%
87 data.frame()
89 if (nrow(dupsNames) == 0) {
90 dupsNames <- NULL
91 } else {
92 dupsNames <- dupsNames$Package
96 return(dupsNames)
100 removeOlderPackages <- function (dupPackages) {
102 dupNames <- dupPackNames(dupPackages)
104 for (dN in dupNames) {
105 dupsDf <- dupPackages %>%
106 filter(Package == dN) %>%
107 data.frame
109 dupCnt <- nrow(dupsDf)
111 while (dupCnt > 1 ) {
112 message('package ', dN, ' ', dupCnt, ' times duplicated')
113 v <- compareVersion(dupsDf[1, 'Version'], dupsDf[2, 'Version'])
115 if (v == 0) {
116 lb <- dupsDf$LibPath[1]
117 message( 'removing double copy ', dN, ' from ', lb)
118 remove.packages(dN, lib=lb)
120 dupsDf <- dupsDf %>%
121 filter(LibPath != lb) %>%
122 data.frame
124 } else if (v == 1) {
125 lb <- dupsDf[2, 'LibPath']
126 message( 'removing older copy ', dN, ' from ', lb)
127 remove.packages(dN, lib=lb)
129 dupsDf <- dupsDf %>%
130 filter(LibPath != lb) %>%
131 data.frame
133 } else if (v == -1) {
134 lb <- dupsDf[1, 'LibPath']
135 message( 'removing older copy ', dN, ' from ', lb)
136 remove.packages(dN, lib=lb)
138 dupsDf <- dupsDf %>%
139 filter(LibPath != lb) %>%
140 data.frame
143 dupCnt <- nrow(dupsDf)
149 filterFilePackages <- function (depsF) {
151 fP <- read.dcf(depsF, fields="Depends")
152 fP <- unlist(strsplit(fP, ','))
153 fP <- trimws(fP, 'both')
154 fP <- gsub("\\s*\\(.*\\)", '', fP, perl=TRUE)
155 fP <- fP[fP != 'R']
159 insPacks <- installedPackages(lib.loc=rLibsSgn)
160 dupPackages <- duplicatedPackagesDf(insPacks)
161 removeOlderPackages(dupPackages)
163 cranPacks <- filterFilePackages(cranFile)
164 biocPacks <- filterFilePackages(bioCFile)
165 githubPackPaths <- filterFilePackages(gitFile)
166 githubPacks <- basename(githubPackPaths)
168 allReqPacks <-c(cranPacks, biocPacks, githubPacks)
170 insPacksUni <- unique(insPacks$Package)
172 newCran <- cranPacks[!cranPacks %in% insPacksUni]
173 newGit <- githubPacks[!githubPacks %in% insPacksUni]
174 newBioc <- biocPacks[!biocPacks %in% insPacksUni]
176 #stop('quit before install...')
178 if (length(newCran) > 0) {
179 install.packages(newCran,
180 repos=cranSite,
181 quiet=TRUE,
182 verbose=FALSE,
183 dependencies=TRUE)
184 } else {
185 message('No new cran packages to install.')
188 newGitPaths <- c()
189 if (!is.null(newGit)) {
191 for (ng in newGit) {
192 ngp <- grep(ng, githubPackPaths, value=TRUE)
193 ifelse(is.null(newGitPaths), newGitPaths <- ngp, newGitPaths <- c(newGitPaths, ngp))
197 if (length(newGitPaths) > 0) {
198 withr::with_libpaths(new=rLibsSgn,
199 install_github(newGitPaths,
200 force=TRUE,
201 quiet=TRUE,
202 verbose=FALSE,
203 dependencies=TRUE))
204 } else {
205 message('No new github packages to install.')
209 if (length(newBioc) > 0 ) {
210 source('http://bioconductor.org/biocLite.R')
211 biocLite(newBioc,
212 suppressUpdates=TRUE,
213 suppressAutoUpdate=TRUE,
214 ask=FALSE,
215 quiet=TRUE,
216 verbose=FALSE,
217 siteRepos=cranSite)
218 } else {
219 message('No new bioconductor packages to install.')