Files
Dot-Zsh/scripts/r.sh
2026-05-05 11:18:52 +01:00

79 lines
2.4 KiB
Bash

#!/bin/bash
# Path: scripts/r.sh
set -e
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'
# OS Detection
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
fi
echo -e "${BLUE} LOG:${YELLOW} Detecting R installation strategy for $OS...${NC}"
if [ "$OS" == "arch" ] || [ "$OS" == "manjaro" ]; then
echo -e "${BLUE} LOG:${YELLOW} Arch Linux detected. Using system R (Pacman)...${NC}"
# Install R + Build Tools
sudo pacman -S --noconfirm --needed r gcc-fortran curl tar
echo -e "${GREEN} SUCCESS:${NC} R installed via Pacman."
R_BIN="R"
elif [ "$OS" == "ubuntu" ] || [ "$OS" == "debian" ]; then
echo -e "${BLUE} LOG:${YELLOW} Installing Debian build dependencies...${NC}"
sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y gfortran curl tar ca-certificates
if ! command -v rig >/dev/null 2>&1; then
echo -e "${BLUE} LOG:${YELLOW} Installing Rig for Debian/Ubuntu...${NC}"
DEB_ARCH=$(dpkg --print-architecture)
case "$DEB_ARCH" in
amd64|arm64)
;;
*)
echo -e "${RED} ERROR:${NC} Unsupported Debian architecture for Rig: $DEB_ARCH"
exit 1
;;
esac
LATEST_REL_DATA=$(curl -fsSL https://api.github.com/repos/r-lib/rig/releases/latest)
RIG_URL=$(echo "$LATEST_REL_DATA" | grep -o "https://[^\"]*r-rig_[^\"]*_${DEB_ARCH}\.deb" | head -n 1)
if [ -z "$RIG_URL" ]; then
echo -e "${RED} ERROR:${NC} Could not find Rig .deb download URL for architecture: $DEB_ARCH"
exit 1
fi
TEMP_DEB=$(mktemp --suffix=.deb)
curl -fLsS "$RIG_URL" -o "$TEMP_DEB"
sudo apt-get install -y "$TEMP_DEB"
rm -f "$TEMP_DEB"
fi
TARGET_VER="release"
if ! rig list | grep -q "release"; then
echo -e "${BLUE} LOG:${YELLOW} Installing R (${TARGET_VER})...${NC}"
sudo rig add "$TARGET_VER" --without-cran-mirror
sudo rig default "$TARGET_VER"
fi
R_BIN="rig run"
fi
echo -e "${BLUE} LOG:${YELLOW} Installing 'renv' package globally...${NC}"
$R_BIN -e 'if (!require("renv", quietly=TRUE)) install.packages("renv", repos="https://cloud.r-project.org")'
echo -e "${GREEN} SUCCESS:${NC} R setup completed."