Skip to content

Commit 435dc35

Browse files
committed
Add R environment check script for r_data_science projects
- Create checkREnvironment.sh.jinja that validates R, renv, and pak installation - Automatically runs renv::restore() if renv.lock exists - Integrate script into postCreateCommand.sh for r_data_science projects - Provides clear error messages for missing dependencies
1 parent 22acbfe commit 435dc35

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# R Environment Check Script
5+
# This script validates that R, renv, and pak are installed
6+
# and restores the renv environment from lockfile if present
7+
8+
echo "🔍 Checking R environment..."
9+
10+
# Check if R is installed
11+
if ! command -v R >/dev/null 2>&1; then
12+
echo "❌ Error: R is not installed"
13+
echo "Please install R before continuing."
14+
exit 1
15+
fi
16+
17+
echo "✅ R is installed"
18+
19+
# Check if renv is installed
20+
if ! R --slave -e "library(renv)" >/dev/null 2>&1; then
21+
echo "❌ Error: renv package is not installed"
22+
echo "Please install renv: R -e \"install.packages('renv')\""
23+
exit 1
24+
fi
25+
26+
echo "✅ renv is installed"
27+
28+
# Check if pak is installed
29+
if ! R --slave -e "library(pak)" >/dev/null 2>&1; then
30+
echo "❌ Error: pak package is not installed"
31+
echo "Please install pak: R -e \"install.packages('pak')\""
32+
exit 1
33+
fi
34+
35+
echo "✅ pak is installed"
36+
37+
# Check if renv.lock exists and restore if needed
38+
if [[ -f "renv.lock" ]]; then
39+
echo "📦 Found renv.lock file, restoring environment..."
40+
41+
if Rscript -e "renv::restore()"; then
42+
echo "✅ R environment restored successfully"
43+
else
44+
echo "❌ Error: Failed to restore R environment from renv.lock"
45+
echo "Please check the renv.lock file and try again"
46+
exit 2
47+
fi
48+
else
49+
echo "ℹ️ No renv.lock file found, skipping environment restoration"
50+
fi
51+
52+
echo "🎉 R environment check completed successfully"

template/.devcontainer/postCreateCommand.sh.jinja

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/down
1313

1414
# Install pre-commit hooks
1515
prek install --install-hooks
16+
{% endif %}{% if r_data_science %}
17+
# Check R environment and restore if needed
18+
chmod +x ./.devcontainer/checkREnvironment.sh
19+
bash ./.devcontainer/checkREnvironment.sh
1620
{% endif %}

0 commit comments

Comments
 (0)