Skip to content

Commit 08899cc

Browse files
committed
Add Windows PowerShell R environment check script
- Create checkREnvironment.ps1.jinja for Windows users - Uses PowerShell commands and colored output - Handles Windows-specific paths and R installation checks - Update postCreateCommand.ps1.jinja to call the R check script - Both scripts are conditional on r_data_science selection
1 parent 08144af commit 08899cc

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

template/.devcontainer/postCreateCommand.ps1.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ Invoke-RestMethod https://github.com/j178/prek/releases/download/v0.2.2/prek-ins
1111

1212
# Install pre-commit hooks
1313
prek install --install-hooks
14+
{% endif %}{% if r_data_science %}
15+
# Check R environment and restore if needed
16+
& .\.devcontainer\checkREnvironment.ps1
1417
{% endif %}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# R Environment Check Script for Windows
2+
# This script validates that R, renv, and pak are installed
3+
# and restores the renv environment from lockfile if present
4+
5+
Write-Host "🔍 Checking R environment..." -ForegroundColor Cyan
6+
7+
# Check if R is installed
8+
try {
9+
$rPath = Get-Command R -ErrorAction Stop
10+
Write-Host "✅ R is installed at: $($rPath.Source)" -ForegroundColor Green
11+
} catch {
12+
Write-Host "❌ Error: R is not installed" -ForegroundColor Red
13+
Write-Host "Please install R from https://cran.r-project.org/bin/windows/base/" -ForegroundColor Yellow
14+
exit 1
15+
}
16+
17+
# Check if renv is installed
18+
try {
19+
R --slave -e "library(renv)" 2>$null
20+
if ($LASTEXITCODE -eq 0) {
21+
Write-Host "✅ renv is installed" -ForegroundColor Green
22+
} else {
23+
throw "renv not found"
24+
}
25+
} catch {
26+
Write-Host "❌ Error: renv package is not installed" -ForegroundColor Red
27+
Write-Host "Please install renv: R -e `"install.packages('renv')`"" -ForegroundColor Yellow
28+
exit 1
29+
}
30+
31+
# Check if pak is installed
32+
try {
33+
R --slave -e "library(pak)" 2>$null
34+
if ($LASTEXITCODE -eq 0) {
35+
Write-Host "✅ pak is installed" -ForegroundColor Green
36+
} else {
37+
throw "pak not found"
38+
}
39+
} catch {
40+
Write-Host "❌ Error: pak package is not installed" -ForegroundColor Red
41+
Write-Host "Please install pak: R -e `"install.packages('pak')`"" -ForegroundColor Yellow
42+
exit 1
43+
}
44+
45+
# Check if renv.lock exists and restore if needed
46+
$scriptPath = $PSScriptRoot
47+
$projectRoot = Split-Path (Split-Path $scriptPath -Parent) -Parent
48+
$renvLockPath = Join-Path $projectRoot "renv.lock"
49+
50+
if (Test-Path $renvLockPath) {
51+
Write-Host "📦 Found renv.lock file, restoring environment..." -ForegroundColor Cyan
52+
53+
try {
54+
Rscript -e "setwd('$projectRoot'); renv::restore()"
55+
if ($LASTEXITCODE -eq 0) {
56+
Write-Host "✅ R environment restored successfully" -ForegroundColor Green
57+
} else {
58+
throw "renv::restore() failed"
59+
}
60+
} catch {
61+
Write-Host "❌ Error: Failed to restore R environment from renv.lock" -ForegroundColor Red
62+
Write-Host "Please check the renv.lock file and try again" -ForegroundColor Yellow
63+
exit 2
64+
}
65+
} else {
66+
Write-Host "ℹ️ No renv.lock file found, skipping environment restoration" -ForegroundColor Blue
67+
}
68+
69+
Write-Host "🎉 R environment check completed successfully" -ForegroundColor Green

0 commit comments

Comments
 (0)