RStudio Configuration Notesโš‘

See Also: My R Setup Guide

Skip to the setup script

Notes on RStudioโš‘

Installationโš‘

Assuming you already have R installed, next you will need to install an IDE to code with, in this case, RStudio.

Install the free Version of RStudio Desktop for Windows from the RStudio Website here: https://rstudio.com/products/rstudio/download/.

Alternatively, use a package manager such as Chocolatey, Scoop, or Winget:

# chocolatey
cinst rstudio -y

# scoop
scoop install rstudio # --global if want system-wide

# winget
winget install RStudio.RStudio.OpenSource

Daily and Preview Buildsโš‘

You can also install various versions of RStudio such as the daily release using the R Bucket for Scoop:

scoop bucket add r-bucket https://github.com/cderv/r-bucket.git
scoop install r-devel
scoop install rstudio-preview
scoop install rstudio-pro-preview
scoop install rstudio-daily

Additional Installations and Utilitiesโš‘

As an R developer, we come across many other various technologies during development and it can be useful to automate the process of setting up your Developer Environment:

Optional Add On Softwareโš‘

Configure RStudio Settingsโš‘

A range of Project Options and Global Options are available in RStudio from the Tools menu (accessible from the keyboard via Alt+T).

Most of these are self-explanatory but it is worth mentioning a few that can boost your programming efficiency:

Unticking this default prevents loading previously created R objects. This will make starting R quicker and reduce the chance of getting bugs due to previously created objects. For this reason, I recommend you untick this box.

Alternatively you can simply run this code:

require(usethis)
usethis::use_blank_slate(scope = "user")

See ?usethis::use_blank_slate for more information.

Other Preferencesโš‘

Advanced Configurationโš‘

This section discusses more advanced R related configurations such as: - Environment Paths - Detailed System Information - Dotfiles - Common pitfalls

For more advanced R developers you may want to further configure your development environement by customizing you R related dotfiles; specifically, your .Rprofile and .Renviron.

Environment and %PATH% Variablesโš‘

%appdata%\RStudio - RStudio Configuration Directory (Preferences) %localappdata%\RStudio-Desktop - RStudio Desktop Internal State

To add your system [[R-Tools]] %PATH% to your .Renviron (easier than manually configuring within windows system settings) run the code:

cat('PATH = ${RTOOLS40_HOME}\\usr\\bin;${PATH}',
    file = fs::path(Sys.getenv("R_USER"), "/.Renviron"),
    append = TRUE)

alternatively use the command prompt or powershell:

setx PATH "<path to rtools>"
$env:PATH = $env:PATH + "<path to rtools>"

My Setupโš‘

Here is what my minimal setup includes:

Additionally, you can configure keybinding for RStudio addins from RStudio and store them within the .R folder located in your R_USER path. To view this path run Sys.getenv("R_USER").

Setup Scriptโš‘

# ************************************
# *          R Configuration         *
# ************************************

# Define some helper functions:
Function Test-Installed( $programName ) {
  $x86_check = ((Get-ChildItem "HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall") |
    Where-Object { $_."Name" -like "*$programName*" } ).Length -gt 0;

  if (Test-Path 'HKLM:Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') {
    $x64_check = ((Get-ChildItem "HKLM:Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
      Where-Object { $_."Name" -like "*$programName*" } ).Length -gt 0;
  }
  return $x86_check -or $x64_check;
}


# Ensure R, RStudio, and RTools installed

if (!(Test-Installed("R"))) {
  cinst R.Project
}

if (!(Test-Installed("RStudio"))) {
  cinst R.Studio
}

if (!(Test-Installed("RTools"))) {
  cinst rtools
}

# configure R PATHS
Write-Host "Review System Environment Variables for R Session" -ForegroundColor Magenta
RScript -e "Sys.getenv()"

# Environment Variables
# Note on initial setup of R need to manually set location of .Renviron
$userhome = [System.Environment]::GetEnvironmentVariable("USERPROFILE")
$rconfigdir = "$userhome\.config\R"
$renvironpath = "$rconfigdir\.Renviron"
$rprofilepath = "$rconfigdir\.Rprofile"
$rhistpath = "$rconfigdir\.Rhistory"
$rlibspath = "$rconfigdir\win-library\4.1"

[System.Environment]::SetEnvironmentVariable("R_HOME", $userhome, "User")
[System.Environment]::SetEnvironmentVariable("R_ENVIRON_USER", $renvironpath, "User")
[System.Environment]::SetEnvironmentVariable("R_PROFILE_USER", $rprofilepath, "User")
[System.Environment]::SetEnvironmentVariable("R_LIBS_USER", $rlibspath, "User")

Copy-Item "$env:USERPROFILE\OneDrive\Documents\R\win-library\4.1\*" -Destination "$rlibspath" -Recurse

if (!(Test-Path($rconfigdir))) {
  mkdir $rconfigdir
}

if (!(Test-Path($rlibspath))) {
  mkdir $rlibspath
}

Copy-Item "~/Dev/jimbrig/jimsdots/R/.Renviron" $renvironpath
Copy-Item "~/Dev/jimbrig/jimsdots/R/.Rprofile" $rprofilepath

Copy-Item "~/Dev/jimbrig/jimsdots/R/lib/installation.R" "$rconfigdir\win-library\installation.R"
Copy-Item "~/Dev/jimbrig/jimsdots/R/lib/pkgs.yml" "$rconfigdir\win-library\pkgs.yml"

Copy-Item "$env:APPDATA\RStudio\rstudio-prefs.json" "$env:APPDATA\RStudio\rstudio-prefs-default.json"
Copy-Item "~/Dev/jimbrig/jimsdots/RStudio/rstudio-prefs.json" "$env:APPDATA\RStudio\rstudio-prefs.json"

mkdir "$env:APPDATA\RStudio\themes"
Copy-Item "~/Dev/jimbrig/jimsdots/RStudio/themes/*" -Destination "$env:APPDATA\RStudio\themes"

mkdir "$env:APPDATA\RStudio\keybindings"
Copy-Item "~/Dev/jimbrig/jimsdots/RStudio/keybindings/*" -Destination "$env:APPDATA\RStudio\keybindings"

mkdir "$env:APPDATA\RStudio\snippets"
Copy-Item "~/Dev/jimbrig/jimsdots/RStudio/snippets/*" -Destination "$env:APPDATA\RStudio\snippets"

Copy-Item "$env:LOCALAPPDATA\RStudio\rstudio-desktop.json" "$env:LOCALAPPDATA\RStudio\rstudio-desktop-default.json"
Copy-Item "$env:LOCALAPPDATA\RStudio\rstudio-desktop-default.json" "~/Dev/Github/jimsdots/RStudio/localappdata/rstudio-desktop-default.json"
Copy-Item "~/Dev/jimbrig/jimsdots/RStudio/localappdata/rstudio-desktop.json" "$env:LOCALAPPDATA\RStudio\rstudio-desktop.json"

# Run R script:
Rscript --vanilla "~/Dev/jimbrig/jimsdots/R/r-setup-script.R"

RStudio Dotfilesโš‘

Configuration files to backup: - user-prefs.json - Add-Ins and Hotkeys - Snippets - Themes - Dictionaries - Project Lists

Tips and Tricksโš‘

See RStudio Tips and Tricks

  1. If you have more than one version or architecture of R installed on your machine, you can hold down the Ctrl key when opening RStudio and a dialog box will appear allowing you to select the version of R to run in RStudio's native session.
  2. On major updates, it is best practice to keep your old version of R on your machine until you are comfortable with the updated R version.
  3. When migrating R versions between major patch updates, you will need to re-install you library of R packages. This process can be vastly expedited utilizing installrโ€™s helpful installr::copy.packages.between.libraries() function.

Links: 020 - Development | MOC - R | Visual Studio Code

Sources: