|
| 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