Set custom preferences for RStudio Workspaces

Use the instructions below based on your RStudio version, to set custom preferences (such as themes, key mappings, languages, and dictionaries) for RStudio workspace runs. For more information on RStudio session settings, see RStudio’s official documentation site.

RStudio version 1.3+

RStudio versions 1.3+ use /home/ubuntu/.config/rstudio/rstudio-prefs.json to store user preferences. You can use a pre run script in a custom compute environment to modify this file to launch RStudio with custom preferences. To learn more about the schema of rstudio-prefs.json, see RStudio’s official documentation on Customizing Session Settings.

Method 1: Write lines to settings file

If you know how to manually add configuration lines to the rstudio-prefs.json settings file, you can write it in the pre run script. For example:

# Variables
DIRECTORY="/home/ubuntu/.config/rstudio/"
# Check if directory exist, else create the directory
if [ -d "${DIRECTORY}" ];
then
    # Show directory
    echo "directory ${DIRECTORY} exists"
else
    echo "The specified directory does not exist"

    echo 'Creating directory...'
    mkdir -p "${DIRECTORY}"

fi

# Check if the preference file exist, else create the preference file
if [ -f "${DIRECTORY}/rstudio-prefs.json" ]; then
    echo "RStudio-prefs json exists"

else
    # Create Rstudio prefs json
    echo "Creating RStudio Preference json file"
    touch "${DIRECTORY}/rstudio-prefs.json"
fi

# add Rstudio Preferences
echo 'Adding Rstudio Preferences'
cat <<EOF > /home/ubuntu/.config/rstudio/rstudio-prefs.json
{
    "editor_theme": "Cobalt",
    "posix_terminal_shell": "bash"
}
EOF

Method 2: Copy a saved settings file

Use this method to copy the settings from an existing RStudio session. This method is useful if you aren’t familiar with the syntax of rstudio-prefs.json, or if you want to use preferences from an existing preference file.

  1. Run a session and modify the RStudio preferences as needed.

  2. Before you stop the session, use the following R code to copy the rstudio-prefs.json file to the root of your project directory.

    file.copy("/home/ubuntu/.config/rstudio/rstudio-prefs.json", ".")
  3. Add the following lines to the pre run script of your environment definition to load the preferences file (if it exists) on subsequent runs:

    # After setting preferences in an RStudio session, run the following command:
    # file.copy("/home/ubuntu/.config/rstudio/rstudio-prefs.json", '.')
    
    
    DIRECTORY="/home/ubuntu/.config/rstudio/"
    SETTINGS_FILE="./rstudio-prefs.json"
    
    
    if [ -f "${SETTINGS_FILE}" ]; then
        echo "File exists"
        echo "Checking if directory exist"
        if [ -d "${DIRECTORY}" ];
        then
            # Show directory
            echo "Directory '${DIRECTORY}' exists"
    
            # Copy Rstudio prefs json
            echo "Copying '${SETTINGS_FILE}' to '${DIRECTORY}'"
            cp ${SETTINGS_FILE} ${DIRECTORY}
        else
            echo "The specified directory does not exist"
    
            echo 'Creating directory...'
            mkdir -p "${DIRECTORY}"
    
            # Copy Rstudio prefs json
            echo "Copying '${SETTINGS_FILE}' to '${DIRECTORY}'"
            cp ${SETTINGS_FILE} ${DIRECTORY}
    
        fi
    fi

RStudio version 1.2 and lower

Older versions (1.2 and lower) of RStudio use /home/ubuntu/.rstudio/monitored/user-settings/user-settings to store user preferences. To launch RStudio with custom preferences, you can use the pre-setup script in a custom compute environment to modify this file.

Method 1: Write lines to settings file

If you know what to add to the settings file, you can write it in the pre-setup script. For example:

# create the encompassing directory if it doesn't exist
mkdir -p /home/ubuntu/.rstudio/monitored/user-settings/
# write the theme to the preferences file inside the directory
echo 'uiPrefs={"theme" : "Mono Industrial"}' >> /home/ubuntu/ .rstudio/monitored/user-settings/user-settings
chown -R ubuntu:ubuntu /home/ubuntu/.rstudio <b class="conum">(3)</b>
# modifies a Domino script that would overwrite this settings file.
if [ -f .domino/launch-rstudio-server ]; then
    sed -i.bak 's# > ~/.rstudio/monitored/user-settings/user-settings# >> ~/.rstudio/monitored/user-settings/user-settings#' .domino/launch-rstudio-server <b class="conum">(4)</b>
    chown ubuntu:ubuntu .domino/launch-rstudio-server <b class="conum">(3)</b>
fi

Method 2: Copy a saved settings file

Use this method to copy the settings from an existing RStudio session. This method is useful if you aren’t familiar with the syntax of rstudio-prefs.json, or if you want to use preferences from an existing preference file.

  1. Run a session and modify the RStudio preferences as needed.

  2. Before you stop the session, use the following R code to copy the user-settings file to the root of your project directory.

    file.copy("/home/ubuntu/.rstudio/monitored/user-settings/user-settings", ".")
  3. Add the following lines to the pre-setup script of your environment definition to load the preferences file (if it exists) on subsequent runs:

    if [ -f user-settings ]; then
        mkdir -p /home/ubuntu/.rstudio/monitored/user-settings/
        cp user-settings /home/ubuntu/.rstudio/monitored/user-settings
        sed -i.bak '/initialWorkingDirectory=/d' /home/ubuntu/.rstudio/monitored/user-settings/user-settings
        chown -R ubuntu:ubuntu /home/ubuntu/.rstudio
        if [ -f .domino/launch-rstudio-server ]; then
            sed -i.bak 's# > ~/.rstudio/monitored/user-settings/user-settings# >> ~/.rstudio/monitored/user-settings/user-settings#' .domino/launch-rstudio-server
            chown ubuntu:ubuntu .domino/launch-rstudio-server
        fi
    fi
Note
The sed statement that deletes the initialWorkingDirectory variable ensures that your session starts with the correct working directory.