SCM

SCM Repository

[rmetrics] Annotation of /www/installRmetrics.R
ViewVC logotype

Annotation of /www/installRmetrics.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3431 - (view) (download)

1 : chalabi 2646 ################################################################################
2 : chalabi 3170 ## **Install Rmetrics packages**
3 : chalabi 2646 ##
4 : chalabi 3170 ## This script installs Rmetrics packages either from source or from
5 :     ## remote server (i.e. R-Forge). It ensures that all dependent
6 :     ## Rmetrics packages are installed from the same location, i.e. remote
7 :     ## server. This is important to avoid compatibility problem between
8 :     ## development packages and packages available on CRAN.
9 :     ##
10 :     ## *An Example with fSeries*
11 :     ##
12 :     ## _Local packages_
13 :     ##
14 :     ## Open an R process and set its working directory to this directory.
15 : chalabi 2646 ## Then type the following :
16 :     ##
17 :     ## > source("installRmetrics.R")
18 : chalabi 3170 ## > installRmetrics("fSeries")
19 : chalabi 2646 ##
20 : chalabi 3170 ## _Packages at R-Forge_
21 :     ##
22 :     ## > source("installRmetrics.R")
23 :     ## > installRmetrics("fSeries", repos="http://R-Forge.R-project.org")
24 :     ##
25 : chalabi 2646 ################################################################################
26 :    
27 :     installRmetrics <-
28 : chalabi 3170 function(pkgs = "Rmetrics", repos = NULL,
29 :     CRAN = "http://stat.ethz.ch/CRAN/",
30 : chalabi 3431 type = "source", suggests = FALSE, ...)
31 : chalabi 2646 {
32 :    
33 : chalabi 3170 stopifnot(is.character(pkgs))
34 : chalabi 2646
35 : chalabi 3177 # get description of packages
36 :     if (is.null(repos)) {
37 :     type <- "source" # install from local directory
38 :     } else {
39 :     address <- contrib.url(repos, type)
40 : chalabi 3178 try(available <- available.packages(address, method = "auto"),
41 : chalabi 3193 silent = TRUE)
42 : chalabi 3178 if (!NROW(available)) {
43 : chalabi 3177 type <- "source" # try to retrieve source package
44 :     address <- contrib.url(repos, type)
45 : chalabi 3178 try(available <- available.packages(address, method = "auto"),
46 :     silent = TRUE)
47 : chalabi 3177 }
48 : chalabi 3178 if (!NROW(available))
49 : chalabi 3193 stop(gettextf("unable to access index for repository %s", repos),
50 :     call. = FALSE)
51 : chalabi 3177 }
52 : chalabi 3170
53 :     # list of Rmetrics packages
54 : chalabi 3177 infokind <- c("Depends", "Imports", if (suggests) "Suggests")
55 : chalabi 3170 pkgsRmetrics <- getDESCR("Rmetrics", infokind,
56 :     if (!is.null(repos)) available)
57 :     pkgsRmetrics <- c(pkgsRmetrics, "Rmetrics")
58 :    
59 :     # test if requested package is part of Rmetrics
60 :     if (!(pkgs %in% c(pkgsRmetrics, "Rmetrics")))
61 :     stop(gettextf("'%s' is not part of Rmetrics",
62 :     deparse(substitute(pkgs))))
63 :    
64 :     pkgsDepends <- getDepends(pkgs, pkgsRmetrics, infokind,
65 :     if (!is.null(repos)) available)
66 :    
67 : mmaechler 3084 ## remove Rmetrics packages and duplicate entries
68 :     ## --> only "outside dependencies"
69 : chalabi 3170 all <- c(pkgsDepends, pkgs)
70 :     depends <- unique(all[!(all %in% pkgsRmetrics)])
71 :     pkgs <- unique(all[(all %in% pkgsRmetrics)])
72 : chalabi 2646
73 : chalabi 3170 ## Remove Rdonlp2 and Rsocp because they are not available at CRAN server
74 : chalabi 3340 depends <- depends[!(depends %in% c("Rdonlp2", "Rsocp", "Cdonlp2",
75 :     "ClpSolve", "Csocp"))]
76 : chalabi 2646
77 : mmaechler 3084 ## disable unnecessary warning message when package is not installed
78 :     ow <- options(warn = -1)
79 : chalabi 3170 ## install third party packages if not already installed
80 :     for (i in seq_along(depends)) {
81 :     if (!require(depends[i], character.only = TRUE, quietly = TRUE)) {
82 : chalabi 3186 message("\ninstalling package ", depends[i],
83 : chalabi 3170 " from CRAN ", CRAN, " ...")
84 : chalabi 3177 install.packages(depends[i], repos = CRAN, type = type, ...)
85 : chalabi 2646 }
86 :     }
87 : chalabi 2998 ### # Note Rdonlp2 is not part of Rmetrics !!
88 :     ### if (!require(Rdonlp2, quietly = TRUE)) {
89 :     ### install.packages("Rdonlp2", repos = repoRmetrics, type = "source", ...)
90 :     ### }
91 : chalabi 2646 options(ow) # set default warning option
92 :    
93 : chalabi 3170 # pkgs in good order for install
94 :     pkgs <- pkgsRmetrics[sort(match(pkgs, pkgsRmetrics))]
95 : chalabi 2646
96 : chalabi 3170 ## install Rmetrics packages
97 : chalabi 3176 install.packages(pkgs, repos = repos, type = type, ...)
98 : chalabi 2646
99 : mmaechler 3084 ## Return
100 : chalabi 3170 return(TRUE)
101 : chalabi 2646 }
102 : chalabi 2998
103 : chalabi 3170 getDESCR <- function(package, infokind, available = NULL)
104 : chalabi 2998 {
105 :     stopifnot(is.character(package))
106 : chalabi 3170 ans <- unlist(lapply(package, function(pkg)
107 :     {
108 :     if (is.null(available)) {
109 :     # if available NULL try to read from
110 :     # local directroy
111 :     descr <- file.path(pkg, "DESCRIPTION")
112 :     descr <- tools:::.read_description(descr)
113 :     } else {
114 :     descr <- available[pkg, ]
115 :     }
116 :     tools:::.split_description(na.omit(descr))[ infokind ]
117 :     # na.omit important when reading files obtain from
118 :     # available.packages
119 :     }), recursive = TRUE)
120 :     as.character(ans)
121 : chalabi 2998 }
122 :    
123 : chalabi 3170 getDepends <- function(package, group, infokind, available = NULL)
124 :     {
125 :     # extract recursively dependencies of a package which belongs to a
126 :     # specific group of packages
127 :     getDESCR <- match.fun(getDESCR)
128 :     pkgsDepends <- NULL
129 :     pkgsTested <- NULL
130 :     while (length(package)) {
131 :     pkgsDepends <- c(pkgsDepends,
132 :     unlist(getDESCR(package, infokind, available)))
133 :     pkgsTested <- c(pkgsTested, package)
134 :     test <- pkgsDepends[pkgsDepends %in% group]
135 :     package <- test[!(test %in% pkgsTested)]
136 :     }
137 :     unique(as.character(pkgsDepends))
138 :     }

R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge