-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_docker_compatible_renv.R
More file actions
143 lines (121 loc) · 4.88 KB
/
setup_docker_compatible_renv.R
File metadata and controls
143 lines (121 loc) · 4.88 KB
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
#!/usr/bin/env Rscript
# Docker-compatible R package installation using renv
# Use renv for version control, then copy to site-library for multi-stage builds
cat("Setting up R environment using renv for Docker compatibility...\n")
# Install renv if not available
if (!require("renv", quietly = TRUE)) {
install.packages("renv", repos = "https://cran.r-project.org")
}
# Initialize renv in current directory if renv.lock exists
if (file.exists("renv.lock")) {
cat("Found renv.lock, initializing renv...\n")
renv::init(bare = TRUE)
# Restore packages from renv.lock
cat("Restoring packages from renv.lock...\n")
renv::restore(confirm = FALSE)
cat("✓ renv restore completed\n")
# Set Bioconductor version for R 4.4
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager", repos = "https://cran.r-project.org")
}
BiocManager::install(version = "3.20", ask = FALSE, update = TRUE)
} else {
cat("No renv.lock found, setting up fresh renv environment...\n")
renv::init(bare = TRUE)
# Install essential packages
essential_packages <- c("BiocManager", "rjson", "GSVA", "remotes", "AUCell")
cat("Installing essential packages via renv...\n")
renv::install("BiocManager")
renv::install("rjson")
renv::install("bioc::GSVA")
renv::install("remotes")
renv::install("bioc::AUCell")
# Install dpGMM from GitHub
cat("Installing dpGMM from GitHub...\n")
renv::install("ZAEDPolSl/dpGMM")
# Create snapshot
cat("Creating renv snapshot...\n")
renv::snapshot()
# Set Bioconductor version for R 4.4
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager", repos = "https://cran.r-project.org")
}
BiocManager::install(version = "3.20", ask = FALSE, update = TRUE)
}
# Now copy packages from renv library to site-library for Docker multi-stage
cat("\nCopying packages from renv to site-library...\n")
# Find renv library path
renv_lib <- renv::paths$library()
cat("renv library path:", renv_lib, "\n")
# Create site-library path
site_lib <- "/usr/local/lib/R/site-library"
dir.create(site_lib, recursive = TRUE, showWarnings = FALSE)
# Get list of installed packages in renv
installed_pkgs <- installed.packages(lib.loc = renv_lib)
pkg_names <- rownames(installed_pkgs)
cat("Copying", length(pkg_names), "packages to site-library...\n")
# Copy each package
copy_count <- 0
for (pkg in pkg_names) {
src_path <- file.path(renv_lib, pkg)
dst_path <- file.path(site_lib, pkg)
if (dir.exists(src_path)) {
# Check if it's a symlink and resolve it
if (Sys.readlink(src_path) != "") {
# It's a symlink, follow it to get the actual path
actual_path <- Sys.readlink(src_path)
if (file.exists(actual_path)) {
# Copy from the actual path, not the symlink
result <- system2("cp", args = c("-rL", actual_path, dst_path), stdout = FALSE, stderr = FALSE)
if (result == 0) {
copy_count <- copy_count + 1
cat(" Copied", pkg, "from", actual_path, "\n")
} else {
cat("Warning: Failed to copy", pkg, "from", actual_path, "\n")
}
} else {
cat("Warning: Symlink target does not exist for", pkg, "->", actual_path, "\n")
}
} else {
# It's a regular directory, copy normally
result <- system2("cp", args = c("-r", src_path, dst_path), stdout = FALSE, stderr = FALSE)
if (result == 0) {
copy_count <- copy_count + 1
cat(" Copied", pkg, "(regular directory)\n")
} else {
cat("Warning: Failed to copy", pkg, "\n")
}
}
}
}
cat("✓ Copied", copy_count, "packages to site-library\n")
# Update library paths to include site-library
.libPaths(c(site_lib, .libPaths()))
cat("Updated library paths:", .libPaths(), "\n")
# Verify essential packages are available
essential_packages <- c("BiocManager", "rjson", "GSVA", "dpGMM", "AUCell")
cat("\nVerifying essential packages:\n")
for (pkg in essential_packages) {
if (require(pkg, quietly = TRUE, character.only = TRUE)) {
version_info <- tryCatch(
{
as.character(packageVersion(pkg))
},
error = function(e) "unknown"
)
cat(sprintf(" ✓ %s (%s)\n", pkg, version_info))
} else {
cat(sprintf(" ✗ %s (failed)\n", pkg))
}
}
# List contents of site-library
cat("\nContents of site-library:\n")
if (dir.exists(site_lib)) {
contents <- list.dirs(site_lib, full.names = FALSE, recursive = FALSE)
for (item in sort(contents)) {
if (item != "") {
cat(sprintf(" - %s\n", item))
}
}
}
cat("\nrenv packages successfully copied to site-library for Docker multi-stage build!\n")