-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·368 lines (329 loc) · 10.9 KB
/
setup.sh
File metadata and controls
executable file
·368 lines (329 loc) · 10.9 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
#
# Setup script for "Brief Introduction to Data Analysis with R"
# Installs all required dependencies for building course materials
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "======================================"
echo "Setup: Brief Introduction to Data Analysis with R"
echo "======================================"
echo ""
# Detect OS
OS="unknown"
if [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
elif [[ -f /etc/debian_version ]]; then
OS="debian"
elif [[ -f /etc/redhat-release ]]; then
OS="redhat"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
OS="windows"
fi
# Detect architecture
ARCH=$(uname -m)
IS_ARM=false
if [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; then
IS_ARM=true
fi
echo "Detected OS: $OS"
echo "Architecture: $ARCH"
if [ "$IS_ARM" = true ]; then
echo -e "${YELLOW}Note: ARM architecture detected${NC}"
fi
echo ""
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to print status
print_status() {
if [ "$2" = "ok" ]; then
echo -e " $1: ${GREEN}OK${NC}"
elif [ "$2" = "missing" ]; then
echo -e " $1: ${RED}MISSING${NC}"
elif [ "$2" = "optional" ]; then
echo -e " $1: ${YELLOW}MISSING (optional)${NC}"
fi
}
# ============================================================
# Step 1: Check/Install R
# ============================================================
echo "Step 1: Checking R installation..."
if command_exists R && command_exists Rscript; then
R_VERSION=$(R --version 2>/dev/null | head -1)
print_status "R" "ok"
echo " $R_VERSION"
else
print_status "R" "missing"
echo ""
echo "R is required. Install it with:"
echo ""
case $OS in
macos)
if [ "$IS_ARM" = true ]; then
echo " Apple Silicon (ARM64) detected."
echo ""
echo " Option 1 (Homebrew - recommended for ARM):"
echo " brew install r"
echo ""
echo " Option 2 (Official ARM installer):"
echo " Download the arm64 build from https://cran.r-project.org/bin/macosx/"
echo " (Look for 'R-x.x.x-arm64.pkg')"
else
echo " Option 1 (Homebrew):"
echo " brew install r"
echo ""
echo " Option 2 (Official installer):"
echo " Download from https://cran.r-project.org/bin/macosx/"
fi
;;
debian)
if [ "$IS_ARM" = true ]; then
echo " ARM64 Linux detected."
echo ""
fi
echo " sudo apt update"
echo " sudo apt install -y r-base r-base-dev"
if [ "$IS_ARM" = true ]; then
echo ""
echo " Note: Some R packages may need to compile from source on ARM."
echo " This may take longer but should work automatically."
fi
;;
redhat)
echo " sudo dnf install -y R"
;;
windows)
echo " Download and install from https://cran.r-project.org/bin/windows/base/"
;;
*)
echo " Download from https://cran.r-project.org/"
;;
esac
echo ""
# Offer to install on supported systems
if [[ "$OS" == "debian" ]]; then
read -p "Install R now? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo apt update
sudo apt install -y r-base r-base-dev
else
echo "Please install R and run this script again."
exit 1
fi
elif [[ "$OS" == "macos" ]] && command_exists brew; then
read -p "Install R via Homebrew now? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
brew install r
else
echo "Please install R and run this script again."
exit 1
fi
else
echo "Please install R and run this script again."
exit 1
fi
fi
echo ""
# ============================================================
# Step 2: Check/Install Pandoc (required for rmarkdown)
# ============================================================
echo "Step 2: Checking Pandoc installation..."
if command_exists pandoc; then
PANDOC_VERSION=$(pandoc --version | head -1)
print_status "Pandoc" "ok"
echo " $PANDOC_VERSION"
else
print_status "Pandoc" "missing"
echo ""
echo "Pandoc is required for rendering R Markdown. Install it with:"
echo ""
case $OS in
macos)
echo " brew install pandoc"
;;
debian)
echo " sudo apt install -y pandoc"
;;
redhat)
echo " sudo dnf install -y pandoc"
;;
windows)
echo " Download from https://pandoc.org/installing.html"
;;
*)
echo " Download from https://pandoc.org/installing.html"
;;
esac
echo ""
# Offer to install on supported systems
if [[ "$OS" == "debian" ]]; then
read -p "Install Pandoc now? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo apt install -y pandoc
else
echo "Please install Pandoc and run this script again."
exit 1
fi
elif [[ "$OS" == "macos" ]] && command_exists brew; then
read -p "Install Pandoc via Homebrew now? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
brew install pandoc
else
echo "Please install Pandoc and run this script again."
exit 1
fi
else
echo "Please install Pandoc and run this script again."
exit 1
fi
fi
echo ""
# ============================================================
# Step 3: Check/Install LaTeX (for PDF output)
# ============================================================
echo "Step 3: Checking LaTeX installation (for PDF output)..."
if command_exists pdflatex; then
print_status "LaTeX" "ok"
else
print_status "LaTeX" "missing"
echo ""
echo "LaTeX is required for PDF output. Installing TinyTeX (lightweight LaTeX distribution)..."
echo ""
# Install TinyTeX via R
Rscript --vanilla -e "
if (!requireNamespace('tinytex', quietly = TRUE)) {
install.packages('tinytex', repos = 'https://cloud.r-project.org', quiet = TRUE)
}
cat('Installing TinyTeX (this may take a few minutes)...\n')
tinytex::install_tinytex(force = TRUE)
"
# Add TinyTeX to PATH for this session
if [ -d "$HOME/bin" ]; then
export PATH="$HOME/bin:$PATH"
fi
if [ -d "$HOME/.TinyTeX/bin/"* ]; then
export PATH="$HOME/.TinyTeX/bin/"*":$PATH"
fi
# Verify installation
if command_exists pdflatex; then
echo -e "${GREEN}TinyTeX installed successfully${NC}"
else
echo -e "${YELLOW}TinyTeX installed but pdflatex not in PATH yet.${NC}"
echo "You may need to restart your terminal or run:"
echo " export PATH=\"\$HOME/bin:\$PATH\""
fi
fi
# Install additional LaTeX packages needed for the course materials
if command_exists tlmgr || [ -f "$HOME/.TinyTeX/bin/"*/tlmgr ]; then
echo "Installing required LaTeX packages..."
Rscript --vanilla -e "
if (requireNamespace('tinytex', quietly = TRUE)) {
tinytex::tlmgr_install(c('collection-fontsrecommended', 'fancyhdr', 'booktabs', 'caption', 'float', 'lastpage', 'geometry', 'babel-german'))
}
" 2>/dev/null || true
fi
echo ""
# ============================================================
# Step 4: Install R packages
# ============================================================
echo "Step 4: Installing R packages..."
if [ "$IS_ARM" = true ]; then
echo -e "${YELLOW}Note: On ARM, some packages may compile from source (this may take a few minutes)${NC}"
echo ""
fi
# Required packages (must match Makefile)
R_PACKAGES="knitr rmarkdown bookdown ggplot2 readxl haven stargazer ctv gridExtra"
Rscript --vanilla -e "
packages <- c('knitr', 'rmarkdown', 'bookdown', 'ggplot2', 'readxl', 'haven', 'stargazer', 'ctv', 'gridExtra')
# Ensure user library exists
user_lib <- Sys.getenv('R_LIBS_USER')
if (user_lib == '') {
user_lib <- file.path(Sys.getenv('HOME'), 'R', paste0(R.version\$platform, '-library'), paste0(R.version\$major, '.', strsplit(R.version\$minor, '[.]')[[1]][1]))
}
if (!dir.exists(user_lib)) {
dir.create(user_lib, recursive = TRUE, showWarnings = FALSE)
cat('Created user library:', user_lib, '\n')
}
.libPaths(c(user_lib, .libPaths()))
installed <- rownames(installed.packages())
missing <- setdiff(packages, installed)
if (length(missing) > 0) {
cat('Installing:', paste(missing, collapse=', '), '\n')
cat('Library:', user_lib, '\n')
install.packages(missing, lib = user_lib, repos = 'https://cloud.r-project.org', quiet = TRUE)
cat('Done.\n')
} else {
cat('All R packages already installed.\n')
}
# Verify installation
installed_after <- rownames(installed.packages())
still_missing <- setdiff(packages, installed_after)
if (length(still_missing) > 0) {
cat('\nWarning: Failed to install:', paste(still_missing, collapse=', '), '\n')
quit(status = 1)
}
"
echo ""
# ============================================================
# Step 5: Verify setup
# ============================================================
echo "Step 5: Verifying setup..."
echo ""
echo "System: $OS / $ARCH"
echo ""
# Check all dependencies
ALL_OK=true
if command_exists Rscript; then
print_status "Rscript" "ok"
else
print_status "Rscript" "missing"
ALL_OK=false
fi
if command_exists pandoc; then
print_status "Pandoc" "ok"
else
print_status "Pandoc" "missing"
ALL_OK=false
fi
if command_exists latexmk; then
print_status "latexmk" "ok"
else
print_status "latexmk (PDF)" "optional"
fi
# Check R packages
echo ""
echo "R packages:"
Rscript --vanilla -e "
packages <- c('knitr', 'rmarkdown', 'bookdown', 'ggplot2', 'readxl', 'haven', 'stargazer', 'ctv', 'gridExtra')
installed <- rownames(installed.packages())
for (p in packages) {
status <- if (p %in% installed) '\033[0;32mOK\033[0m' else '\033[0;31mMISSING\033[0m'
cat(sprintf(' %-12s %s\n', paste0(p, ':'), status))
}
"
echo ""
echo "======================================"
if [ "$ALL_OK" = true ]; then
echo -e "${GREEN}Setup complete!${NC}"
echo ""
echo "Build commands:"
echo " make # Build all materials (both languages)"
echo " make en # Build English materials only"
echo " make de # Build German materials only"
echo " make html # Build HTML only (no LaTeX needed)"
echo " make -j4 # Parallel build (faster)"
else
echo -e "${RED}Setup incomplete. Please resolve the issues above.${NC}"
exit 1
fi
echo "======================================"