-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwhich_r.sh
More file actions
68 lines (56 loc) · 2.28 KB
/
which_r.sh
File metadata and controls
68 lines (56 loc) · 2.28 KB
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
which_r() {
# Specify the parent directory
parent_dir="$WORK_DIR/build"
# Path to the settings.json file
settings_file_path=$WORK_DIR/.vscode/settings.json
built_in_r_version=$(R --version | grep "^R version" | awk '{print $3}')
# Ask user which R version to use
echo "Which version of R should be used in new R terminals?"
echo " 1. R $built_in_r_version (release version built into this container)"
# Check for additional R versions in subdirectories
if [ -d "$parent_dir" ]; then
# Create an array to store subdirectory names
subdirs=()
# Loop through subdirectories and print numbered list
counter=2 # Start counter at 2 to avoid conflict with built-in R
for dir in "$parent_dir"/*; do
if [ -d "$dir/bin" ] && [ -x "$dir/bin/R" ]; then
subdir=$(basename "$dir")
subdirs+=("$subdir") # Populate subdirs array
echo " $counter. $subdir"
((counter++))
fi
done
fi
# If no additional R builds were found
if [ ${#subdirs[@]} -eq 0 ]; then
range=1
echo "No additional R builds available."
else
range=$((counter - 1))
fi
# Get user choice
read -p "Enter the number corresponding to the selected version: " choice
# Define selected version based on choice
if [[ "$choice" -eq 1 ]]; then
# Use built-in R
selected_version="/usr/bin/R"
elif [[ "$choice" -ge 2 ]] && [[ "$choice" -lt "$counter" ]]; then
# Use R from chosen subdirectory
chosen_subdir="${subdirs[((choice - 2))]}"
selected_version="$parent_dir/$chosen_subdir/bin/R"
else
# Invalid choice, default to built-in R
if [[ $range -eq 1 ]]; then
echo "Invalid choice, please enter 1. Defaulting to built-in R version."
else
echo "Invalid choice, please select options between 1 to $range. Defaulting to built-in R version."
fi
selected_version="/usr/bin/R"
fi
# Update settings.json with the chosen R path
updated_settings_data=$(cat "$settings_file_path" | jq --arg subdir "$selected_version" '."r.rterm.linux"=$subdir | ."r.rpath.linux"=$subdir')
echo "$updated_settings_data" > "$settings_file_path"
echo "R terminal will now use version: $selected_version"
echo "To update the HTML help, click \"Reload\" in the VS Code status bar (bottom right) to reload your VS Code window."
}