-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
211 lines (177 loc) · 7.41 KB
/
setup.sh
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
#!/bin/bash
# Prompt the user to enter their sudo password and cache it
echo "Please enter your sudo password to continue. It will be cached for a short time."
sudo ls > /dev/null 2>&1 # This will prompt for the password and cache it
# Check if the password was successfully cached
if [ $? -ne 0 ]; then
echo "Failed to cache sudo password. Exiting."
exit 1
fi
# View Ubuntu system information
echo "Viewing Ubuntu system information..."
lsb_release -a
# Check CPU information
echo "Checking CPU information..."
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
# Update system indices
echo "Updating system indices..."
sudo apt update -qq
# Install necessary helper packages
echo "Installing helper packages..."
sudo apt install --no-install-recommends software-properties-common dirmngr -y
# Add the R project signing key
echo "Adding R project signing key..."
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# Check if the previous command succeeded
if [ $? -ne 0 ]; then
echo "WARNING: Failed to add the R project signing key."
read -p "Do you want to continue? (y/n): " continue
if [[ ! "$continue" =~ ^[Yy]$ ]]; then
echo "Exiting script."
exit 1
fi
fi
# Add the R 4.0 repository
echo "Adding R 4.0 repository..."
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# Check if the previous command succeeded
if [ $? -ne 0 ]; then
echo "WARNING: Failed to add the R 4.0 repository."
read -p "Do you want to continue? (y/n): " continue
if [[ ! "$continue" =~ ^[Yy]$ ]]; then
echo "Exiting script."
exit 1
fi
fi
# Install base R
echo "Installing base R..."
sudo apt install --no-install-recommends r-base r-base-dev -y
# Install R package dependencies
echo "Installing R package dependencies..."
sudo apt install libfontconfig1-dev libfreetype6-dev libcurl4-openssl-dev libssl-dev libxml2-dev libharfbuzz-dev libfribidi-dev libicu-dev libpng-dev libtiff5-dev libjpeg-dev libmagick++-dev libblas-dev liblapack-dev libarmadillo-dev -y
# Add ubuntugis unstable PPA
echo "Adding ubuntugis unstable PPA..."
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable -y
# Check if the previous command succeeded
if [ $? -ne 0 ]; then
echo "WARNING: Failed to add ubuntugis unstable PPA."
read -p "Do you want to continue? (y/n): " continue
if [[ ! "$continue" =~ ^[Yy]$ ]]; then
echo "Exiting script."
exit 1
fi
fi
# Install additional R package dependencies
echo "Installing additional R package dependencies..."
sudo apt install libudunits2-dev libgdal-dev libgeos-dev libproj-dev libsqlite3-dev -y
# Ask the user if they want to install RStudio Server
read -p "Do you want to install RStudio Server? (y/n): " install_rstudio
if [[ "$install_rstudio" =~ ^[Yy]$ ]]; then
echo "Installing RStudio Server..."
sudo apt install gdebi-core -y
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2024.12.0-467-amd64.deb
sudo gdebi rstudio-server-2024.12.0-467-amd64.deb -n
else
echo "Skipping RStudio Server installation."
fi
# Set up R package installation path
echo "Setting up R package installation path..."
cd ~
# Check if ~/pkgR exists
if [ -d "~/pkgR" ]; then
echo "Directory ~/pkgR already exists. Using ~/pkgRUserLocal instead."
mkdir -p ~/pkgRUserLocal
R_LIB_PATH="~/pkgRUserLocal"
else
echo "Directory ~/pkgR does not exist. Using ~/pkgR."
mkdir -p ~/pkgR
R_LIB_PATH="~/pkgR"
fi
# Write the .libPaths() configuration to ~/.Rprofile
echo "Configuring .libPaths() in ~/.Rprofile..."
echo ".libPaths(c(\"$R_LIB_PATH\", .libPaths()))" >> ~/.Rprofile
# Verify the .Rprofile configuration
echo "Current ~/.Rprofile content:"
cat ~/.Rprofile
# Restart R to apply the new .libPaths() settings
echo "Restarting R to apply the new library paths..."
Rscript -e 'quit(save="no")'
# Install R packages
echo "Installing R packages..."
# # Function to install an R package
# install_r_package() {
# local package_name=$1 # Get the package name from the first argument
# echo "Installing R package: $package_name..."
# Rscript -e "install.packages('$package_name', repos = 'https://mirrors.bfsu.edu.cn/CRAN/', dep = TRUE)"
# # Check if the installation succeeded
# if [ $? -ne 0 ]; then
# echo "ERROR: Failed to install R package: $package_name."
# read -p "Do you want to retry? (y/n): " retry
# if [[ "$retry" =~ ^[Yy]$ ]]; then
# install_r_package "$package_name" # Retry installation
# else
# echo "Skipping installation of $package_name."
# fi
# else
# echo "R package $package_name installed successfully."
# fi
# }
# Function to install an R package with retry logic
install_r_package() {
local package_name=$1 # Get the package name from the first argument
local max_retries=5 # Maximum number of retries
local attempt=1 # Current attempt
echo "Installing R package: $package_name..."
while [ $attempt -le $max_retries ]; do
# Try to install the package
Rscript -e "install.packages('$package_name', repos = 'https://mirrors.bfsu.edu.cn/CRAN/', dep = TRUE)"
# Check if the installation succeeded
if [ $? -eq 0 ]; then
echo "R package $package_name installed successfully."
return 0 # Exit function after successful installation
fi
# If installation failed
echo "ERROR: Failed to install R package: $package_name (Attempt $attempt of $max_retries)."
# If 5 attempts failed, prompt for continuation
if [ $attempt -eq 5 ]; then
read -p "5 attempts failed. Do you want to continue? (y/n): " continue
if [[ "$continue" =~ ^[Yy]$ ]]; then
echo "Continuing with the next package..."
return 1 # Exit and continue with the next package
else
echo "Skipping installation of $package_name."
return 1 # Exit the function and skip the current package
fi
fi
# Prompt to retry after each failure
if [[ "$attempt" -lt 3 ]]; then
read -p "Do you want to retry? (y/n): " retry
if [[ "$retry" =~ ^[Yy]$ ]]; then
((attempt++)) # Increment the attempt counter
continue # Retry the installation
else
echo "Skipping installation of $package_name."
return 1 # Exit the function and skip the current package
fi
elif [[ "$attempt" -ge 3 && "$attempt" -lt 5 ]]; then
read -p "3 attempts failed. Do you want to continue with the next package? (y/n): " continue
if [[ "$continue" =~ ^[Yy]$ ]]; then
echo "Continuing with the next package..."
return 1 # Exit and continue with the next package
else
echo "Skipping installation of $package_name."
return 1 # Exit the function and skip the current package
fi
fi
done
}
install_r_package "usethis"
install_r_package "devtools"
install_r_package "tidyverse"
install_r_package "sf"
install_r_package "terra"
install_r_package "IRkernel"
install_r_package "sdsfun"
install_r_package "spEDM"
# Rscript -e 'install.packages(c("sdsfun","gdverse","geocomplexity", "spEDM", "itmsa"), dep = TRUE)'
echo "All tasks completed!"