1 # R packages required on SGN websites
3 #removes older ones of duplicate packages
4 #installs new ones from CRAN, bioconductor, github
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.')
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
)
45 if (!require(dplyr
, lib
.loc
=rLibsSgn
, quietly
=TRUE, warn
.conflicts
=FALSE)) {
46 install
.packages('dplyr', repos
=cranSite
)
50 if (!require(devtools
, lib
.loc
=rLibsSgn
, quietly
=TRUE, warn
.conflicts
=FALSE)) {
51 install
.packages('devtools', repos
=cranSite
)
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
%>%
73 if (is
.data
.frame(dupPackages
) == FALSE) {
79 dupPackNames
<- function(dupPackDf
) {
82 if(!is
.null(dupPackDf
)) {
83 if (is
.data
.frame(dupPackDf
)) {
84 dupsNames
<- dupPackDf
%>%
89 if (nrow(dupsNames
) == 0) {
92 dupsNames
<- dupsNames$Package
100 removeOlderPackages
<- function (dupPackages
) {
102 dupNames
<- dupPackNames(dupPackages
)
104 for (dN
in dupNames
) {
105 dupsDf
<- dupPackages
%>%
106 filter(Package
== dN
) %>%
109 dupCnt
<- nrow(dupsDf
)
111 while (dupCnt
> 1 ) {
112 message('package ', dN
, ' ', dupCnt
, ' times duplicated')
113 v
<- compareVersion(dupsDf
[1, 'Version'], dupsDf
[2, 'Version'])
116 lb
<- dupsDf$LibPath
[1]
117 message( 'removing double copy ', dN
, ' from ', lb
)
118 remove
.packages(dN
, lib
=lb
)
121 filter(LibPath
!= lb
) %>%
125 lb
<- dupsDf
[2, 'LibPath']
126 message( 'removing older copy ', dN
, ' from ', lb
)
127 remove
.packages(dN
, lib
=lb
)
130 filter(LibPath
!= lb
) %>%
133 } else if (v
== -1) {
134 lb
<- dupsDf
[1, 'LibPath']
135 message( 'removing older copy ', dN
, ' from ', lb
)
136 remove
.packages(dN
, lib
=lb
)
139 filter(LibPath
!= lb
) %>%
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)
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
,
185 message('No new cran packages to install.')
189 if (!is
.null(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
,
205 message('No new github packages to install.')
209 if (length(newBioc
) > 0 ) {
210 source('http://bioconductor.org/biocLite.R')
212 suppressUpdates
=TRUE,
213 suppressAutoUpdate
=TRUE,
219 message('No new bioconductor packages to install.')