-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathworkshop_setup.R
More file actions
318 lines (262 loc) · 9.96 KB
/
Copy pathworkshop_setup.R
File metadata and controls
318 lines (262 loc) · 9.96 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env Rscript
# ============================================================================
# Workshop Setup Script: R + TensorFlow + Scientific Computing
# ============================================================================
# This script prepares the environment for the workshop by:
# 1. Installing required R packages
# 2. Configuring Python for use with reticulate in Positron
# 3. Installing Python packages to system Python
# 4. Verifying the installation
#
# Designed for Positron on Posit Workbench
# Usage: source("workshop_setup.R")
# ============================================================================
cat("\n")
cat("======================================================================\n")
cat(" Workshop: R + TensorFlow + Scientific Computing\n")
cat(" Setup Script - Installing Dependencies\n")
cat("======================================================================\n\n")
# ============================================================================
# STEP 1: Install Core R Packages
# ============================================================================
cat("STEP 1: Checking and installing R packages...\n")
cat("----------------------------------------------------------------------\n")
# Core packages needed for the workshop
cran_pkgs <- c(
# Python integration
"reticulate",
# Deep learning
"keras",
"tensorflow",
"torch",
# Data manipulation and visualization
"tidyverse",
"ggplot2",
# MLOps and deployment
"vetiver",
"pins",
"plumber",
"rsconnect",
# API consumption
"httr",
"jsonlite",
# Utilities
"config",
"withr",
"stringr",
"glue",
# For demos
"nycflights13",
"rmarkdown",
"knitr",
# Shiny (for apps in part 1)
"shiny",
# Development tools
"devtools",
"remotes"
)
# Check which packages need to be installed
to_install <- setdiff(cran_pkgs, installed.packages()[, "Package"])
if (length(to_install) > 0) {
cat(sprintf("Installing %d CRAN packages: %s\n",
length(to_install),
paste(to_install, collapse=", ")))
# Use system default repos (configured by IT/Posit Package Manager)
install.packages(to_install)
cat("✓ CRAN packages installed\n\n")
} else {
cat("✓ All CRAN packages already installed\n\n")
}
# ============================================================================
# STEP 2: Configure Python for Positron
# ============================================================================
cat("STEP 2: Configuring Python for Positron/reticulate...\n")
cat("----------------------------------------------------------------------\n")
# Find system Python installation
python_paths <- c(
"/opt/python/default/bin/python3",
"/opt/python/3.12.3/bin/python3",
"/usr/bin/python3"
)
python_exe <- NULL
for (py_path in python_paths) {
if (file.exists(py_path)) {
python_exe <- py_path
break
}
}
if (is.null(python_exe)) {
stop("ERROR: Could not find Python installation")
}
cat(sprintf("Found Python: %s\n", python_exe))
# Create .Rprofile to configure Python for reticulate
# This is needed in Positron to prevent reticulate from creating its own Python env
rprofile_content <- sprintf('# Auto-generated by workshop_setup.R
# Configure Python for reticulate in Positron
Sys.setenv(RETICULATE_PYTHON = "%s")
', python_exe)
# Create .Rprofile in workshop root
writeLines(rprofile_content, ".Rprofile")
cat("✓ Created .Rprofile in workshop root\n")
# Create .Rprofile in each workshop directory and subdirectory
workshop_dirs <- c(
"part1",
"part1/reticulated-shiny-app-py2",
"part1/reticulated-shiny-rmd-py2",
"part1/Shiny-Python",
"part2",
"part2/keras_image_recognition",
"part_3",
"part_3/immunotherapy",
"part_4_torch",
"part_4_torch/part1",
"part_4_torch/part2",
"part_4_torch/part3/immunotherapy",
"part_5_vetiver",
"part_5_vetiver/tensorflow_keras",
"part_5_vetiver/torch"
)
for (dir in workshop_dirs) {
if (dir.exists(dir)) {
rprofile_path <- file.path(dir, ".Rprofile")
writeLines(rprofile_content, rprofile_path)
cat(sprintf("✓ Created .Rprofile in %s/\n", dir))
}
}
cat(" This ensures reticulate uses system Python with all packages\n")
cat(" Works from any workshop directory\n")
# Set for current session
Sys.setenv(RETICULATE_PYTHON = python_exe)
cat("✓ Python configured for current session\n\n")
# ============================================================================
# STEP 3: Install GitHub Packages
# ============================================================================
cat("STEP 3: Installing packages from GitHub...\n")
cat("----------------------------------------------------------------------\n")
# ggseqlogo - for sequence logos (used in part 3)
if (!"ggseqlogo" %in% installed.packages()[, "Package"]) {
cat("Installing ggseqlogo from GitHub...\n")
remotes::install_github("omarwagih/ggseqlogo", quiet = TRUE)
cat("✓ ggseqlogo installed\n")
} else {
cat("✓ ggseqlogo already installed\n")
}
# PepTools - peptide tools (used in part 3)
if (!"PepTools" %in% installed.packages()[, "Package"]) {
cat("Installing PepTools from GitHub...\n")
remotes::install_github("leonjessen/PepTools", quiet = TRUE)
cat("✓ PepTools installed\n")
} else {
cat("✓ PepTools already installed\n")
}
cat("\n")
# ============================================================================
# STEP 4: Install Python Packages
# ============================================================================
cat("STEP 4: Installing Python packages...\n")
cat("----------------------------------------------------------------------\n")
library(reticulate)
# Get the system Python that reticulate will use
py_exe <- Sys.which("python3")
if (py_exe == "") {
py_exe <- Sys.which("python")
}
cat(sprintf("Python executable: %s\n", py_exe))
# Check if requirements.txt exists
if (file.exists("requirements.txt")) {
cat("\nFound requirements.txt - installing Python packages from it...\n")
cat("(This may take several minutes)\n\n")
# Install from requirements.txt
install_cmd <- sprintf("%s -m pip install --user -r requirements.txt", py_exe)
cat("Running: pip install --user -r requirements.txt\n\n")
result <- system(install_cmd, intern = FALSE)
if (result == 0) {
cat("\n✓ Python packages from requirements.txt installed\n\n")
} else {
cat("\n⚠ Some packages may have failed (check output above)\n")
cat(" This might be ok if packages are already installed\n\n")
}
} else {
cat("\n⚠ requirements.txt not found\n")
cat(" Installing core packages manually...\n\n")
# Fallback: install core packages
py_packages <- c("pandas", "numpy", "matplotlib", "seaborn", "tensorflow", "keras")
for (pkg in py_packages) {
cat(sprintf(" Installing %s...\n", pkg))
install_cmd <- sprintf("%s -m pip install --user --quiet %s", py_exe, pkg)
system(install_cmd)
}
cat("\n✓ Core Python packages installed\n\n")
}
# ============================================================================
# STEP 5: Verify Installation
# ============================================================================
cat("STEP 5: Verifying installation...\n")
cat("======================================================================\n\n")
# Check R packages
cat("R Packages Status:\n")
cat("------------------\n")
critical_packages <- c("reticulate", "keras", "tensorflow", "torch", "tidyverse",
"vetiver", "pins", "plumber", "httr",
"ggseqlogo", "PepTools", "nycflights13")
all_ok <- TRUE
for (pkg in critical_packages) {
has_pkg <- requireNamespace(pkg, quietly = TRUE)
status <- if (has_pkg) "✓ INSTALLED" else "✗ MISSING"
cat(sprintf(" %-15s %s\n", pkg, status))
if (!has_pkg) all_ok <- FALSE
}
cat("\n")
# Check Python packages
cat("Python Packages Status:\n")
cat("-----------------------\n")
cat("(Restart R session if packages don't show up)\n\n")
# These checks might fail in current session, that's ok
for (pkg in c("pandas", "numpy", "matplotlib", "tensorflow")) {
status <- tryCatch({
py_module_available(pkg)
"✓ available"
}, error = function(e) {
"? check after restart"
})
cat(sprintf(" %-15s %s\n", pkg, status))
}
cat("\n")
# ============================================================================
# Final Instructions
# ============================================================================
cat("======================================================================\n")
if (all_ok) {
cat("✓ Setup complete!\n")
} else {
cat("⚠ Setup complete with some warnings\n")
}
cat("======================================================================\n\n")
cat("IMPORTANT - RESTART R NOW:\n")
cat("--------------------------\n")
cat("In Positron: Session > Restart R Session\n")
cat("Or command: .rs.restartR()\n")
cat("This loads the .Rprofile Python configuration\n\n")
cat("After restarting R:\n")
cat("-------------------\n")
cat("1. The workshop materials are organized in five parts:\n")
cat(" - part1/ : R & Python Integration with reticulate\n")
cat(" - part2/ : Deep Learning with Keras\n")
cat(" - part_3/ : Cancer Immunotherapy Application (Keras)\n")
cat(" - part_4_torch/: Same as parts 1-3 but using Torch R\n")
cat(" - part_5_vetiver/: Modern MLOps with Vetiver (Keras & Torch)\n\n")
cat("2. Start with part1/demo-notebook.Rmd\n\n")
cat("3. Run notebook chunks IN ORDER from top to bottom\n")
cat(" IMPORTANT: Always run the setup chunk first!\n\n")
cat("4. For Shiny apps (runtime: shiny), use:\n")
cat(" rmarkdown::run(\"path/to/app.Rmd\")\n")
cat(" (Do NOT try to render Shiny apps)\n\n")
cat("Tips:\n")
cat("-----\n")
cat("- Python variables are accessible in R via py$variable_name\n")
cat("- R variables are accessible in Python via r.variable_name\n")
cat("- If you get 'module not found' errors, restart R to reload Python config\n")
cat("- The .Rprofile ensures reticulate uses system Python with all packages\n")
cat("- In Positron, reticulate and native Python work side-by-side\n\n")
cat("Setup script completed.\n\n")
invisible(TRUE)