1-
1+ #! /usr/bin/env bash
22
33which_r () {
4- # Specify the parent directory
5- parent_dir=" $WORK_DIR /build"
6-
7- # Path to the settings.json file
8- settings_file_path=$WORK_DIR /.vscode/settings.json
4+ parent_dir=" $PWD /build"
5+ settings_file_path=" $PWD /.vscode/settings.json"
6+ launch_script=" $PWD /scripts/launch_r.sh"
97
10- built_in_r_version=$( R --version | grep " ^R version" | awk ' {print $3}' )
8+ built_in_r_version=$( R --version | awk ' / ^R version/ {print $3}' )
119
12- # Ask user which R version to use
1310 echo " Which version of R should be used in new R terminals?"
14- echo " 1. R $built_in_r_version (release version built into this container )"
11+ echo " 1. R $built_in_r_version (built-in )"
1512
16- # Check for additional R versions in subdirectories
13+ # Detect additional R builds
14+ subdirs=()
15+ counter=2
1716 if [ -d " $parent_dir " ]; then
18- # Create an array to store subdirectory names
19- subdirs=()
20-
21- # Loop through subdirectories and print numbered list
22- counter=2 # Start counter at 2 to avoid conflict with built-in R
2317 for dir in " $parent_dir " /* ; do
24- if [ -d " $dir /bin" ] && [ -x " $dir /bin/R" ]; then
25- subdir=$( basename " $dir " )
26- subdirs+=(" $subdir " ) # Populate subdirs array
27- echo " $counter . $subdir "
18+ if [ -x " $dir /bin/R" ]; then
19+ subdirs+=(" $( basename " $dir " ) " )
20+ echo " $counter . ${subdirs[-1]} "
2821 (( counter++ ))
2922 fi
3023 done
3124 fi
3225
33- # If no additional R builds were found
34- if [ ${# subdirs[@]} -eq 0 ]; then
35- range=1
36- echo " No additional R builds available."
37- else
38- range=$(( counter - 1 ))
39- fi
26+ range=$(( counter - 1 ))
27+ [ " ${# subdirs[@]} " -eq 0 ] && echo " No additional R builds found."
4028
41- # Get user choice
42- read -p " Enter the number corresponding to the selected version: " choice
29+ read -p " Enter number (1–$range ): " choice
4330
44- # Define selected version based on choice
45- if [[ " $choice " -eq 1 ]]; then
46- # Use built-in R
47- selected_version=" /usr/bin/R"
48- elif [[ " $choice " -ge 2 ]] && [[ " $choice " -lt " $counter " ]]; then
49- # Use R from chosen subdirectory
50- chosen_subdir=" ${subdirs[((choice - 2))]} "
51- selected_version=" $parent_dir /$chosen_subdir /bin/R"
31+ if [ " $choice " -eq 1 ] 2> /dev/null; then
32+ selected=" /usr/bin/R"
33+ elif [ " $choice " -ge 2 ] && [ " $choice " -le " $range " ] 2> /dev/null; then
34+ idx=$(( choice - 2 ))
35+ selected=" $parent_dir /${subdirs[$idx]} /bin/R"
5236 else
53- # Invalid choice, default to built-in R
54- if [[ $range -eq 1 ]]; then
55- echo " Invalid choice, please enter 1. Defaulting to built-in R version."
56- else
57- echo " Invalid choice, please select options between 1 to $range . Defaulting to built-in R version."
58- fi
59- selected_version=" /usr/bin/R"
37+ echo " Invalid choice; defaulting to built-in"
38+ selected=" /usr/bin/R"
6039 fi
6140
62- # Update settings.json with the chosen R path
63- updated_settings_data=$( cat " $settings_file_path " | jq --arg subdir " $selected_version " ' ."r.rpath.linux"=$subdir' )
64- echo " $updated_settings_data " > " $settings_file_path "
41+ # Update launch_r.sh to call the selected R
42+ sed -i " s|^exec .*/R|exec $selected |" " $launch_script "
43+
44+ # Update VS Code setting if it exists
45+ if [ -f " $settings_file_path " ]; then
46+ jq --arg r " $selected " ' ."r.rpath.linux"=$r' \
47+ " $settings_file_path " > " ${settings_file_path} .tmp" \
48+ && mv " ${settings_file_path} .tmp" " $settings_file_path "
49+ fi
6550
66- echo " R terminal will now use version : $selected_version "
67- echo " To update the HTML help, click \" Reload\" in the VS Code status bar (bottom right) to reload your VS Code window ."
68- }
51+ echo " Now using R at : $selected "
52+ echo " Reload VS Code to apply updates ."
53+ }
0 commit comments