-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.R
179 lines (158 loc) · 4.5 KB
/
install.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#' Install neuralGAM python requirements
#' @description
#' Creates a conda environment (installing miniconda if required) and set ups the
#' Python requirements to run neuralGAM (Tensorflow and Keras).
#'
#' Miniconda and related environments are generated in the user's cache directory
#' given by:
#'
#' \code{tools::R_user_dir('neuralGAM', 'cache')}
#'
#' @return NULL
#' @export
#' @importFrom reticulate py_module_available conda_binary install_miniconda py_config use_condaenv conda_list conda_create
#' @importFrom tensorflow install_tensorflow
#' @importFrom keras install_keras
install_neuralGAM <- function() {
conda <- .getConda()
if(is.null(conda)){
.installConda()
conda <- .getConda()
}
channel <- NULL
if(.isMac()){
channel <- "apple"
}
reticulate::conda_create(envname = "neuralGAM-env",
conda = conda,
python_version = "3.10",
channel = channel)
packageStartupMessage("Installing tensorflow...")
status4 <- tryCatch(
tensorflow::install_tensorflow(
version = "2.13",
method = "conda",
conda = conda,
envname = "neuralGAM-env",
restart_session = FALSE,
force = TRUE
),
error = function(e) {
packageStartupMessage(e)
return(TRUE)
}
)
if (isTRUE(status4)) {
stop("Error during tensorflow installation.",
call. = FALSE)
}
packageStartupMessage("Installing keras...")
status3 <- tryCatch(
keras::install_keras(
version = "2.13",
method = "conda",
conda = conda,
envname = "neuralGAM-env",
force = TRUE
),
error = function(e) {
packageStartupMessage(e)
return(TRUE)
}
)
if (isTRUE(status3)) {
packageStartupMessage(status3)
stop("Error during keras installation.",
call. = FALSE)
}
packageStartupMessage("Installation completed! Restarting R session...")
}
.setupConda <- function(conda) {
if(is.null(conda)){
packageStartupMessage("NOTE: conda not found... run 'install_neuralGAM()' and load library again...")
}
else{
envs <- reticulate::conda_list(conda)
if("neuralGAM-env" %in% envs$name){
i <- which(envs$name == "neuralGAM-env")
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = 2)
Sys.setenv(RETICULATE_PYTHON = envs$python[i])
reticulate::use_condaenv("neuralGAM-env", conda = conda, required = TRUE)
reticulate::py_config() # ensure python is initialized
tfVersion <- tensorflow::tf$`__version__`
}
else{
packageStartupMessage("NOTE: conda environment not found... run 'install_neuralGAM()' and load library again...")
}
}
}
.installConda <- function() {
packageStartupMessage("No miniconda detected, installing it using reticulate R package")
dir <- tools::R_user_dir("neuralGAM", "cache")
user_dir <- normalizePath(dir, winslash = "\\", mustWork = NA)
status <- tryCatch(
reticulate::install_miniconda(path = user_dir),
error = function(e) {
packageStartupMessage(e)
return(TRUE)
}
)
if (isTRUE(status)) {
stop("Error in Miniconda Installation.", call. = FALSE)
}
return(.getConda())
}
.getCondaDir <- function() {
user_dir <- tools::R_user_dir("neuralGAM", "cache")
# set up conda_dir according to platform:
if (.isWindows()) {
conda_dir <- paste0(user_dir, "/condabin/conda.bat")
}
else {
conda_dir <- paste0(user_dir, "/bin/conda")
}
return(conda_dir)
}
.getConda <- function() {
# Try to find custom conda installation:
conda_dir <- .getCondaDir()
conda <- tryCatch(
reticulate::conda_binary(conda_dir),
error = function(e)
NULL
)
if(is.null(conda)){
# Try to obtain default conda installation
conda <- tryCatch(
reticulate::conda_binary("auto"),
error = function(e)
NULL
)
}
return(conda)
}
.isTensorFlow <- function() {
tfAvailable <- reticulate::py_module_available("tensorflow")
if (tfAvailable) {
tfVersion <- tensorflow::tf$`__version__`
tfAvailable <- utils::compareVersion("2.2", tfVersion) <= 0
}
return(tfAvailable)
}
.isKeras <- function(){
kerasAvailable <- reticulate::py_module_available("keras")
return(kerasAvailable)
}
.isMac <- function() {
sys_info <- Sys.info()
return(sys_info[["sysname"]] == "Darwin")
}
.isWindows <- function() {
sys_info <- Sys.info()
return(sys_info[["sysname"]] == "Windows")
}
.isMacARM <- function() {
sys_info <- Sys.info()
return(sys_info[["sysname"]] == "Darwin" &&
sys_info[["machine"]] == "arm64")
}