(re-)Installation and Configuration of R on macOS

Image credit: Daniel Romero on Unsplash
Table of Contents

Motivation

Due to the novel coronavirus (SARS-CoV-2) and its related disease 😷 COVID-19 employees and students at Wageningen University & Research are all working from home. Students taking Statistical Courses taught by Mathematical and Statistical Methods at Wageningen University & Research will most likely use R.

The instruction in this post will show how to (re-)install R on a desktop or laptop computer running macOS as operating system.

In the text some symbol combinations are used for shortcuts, the following table explains the meaning of these symbols in relation to specific keys on your keyboard. To use the shortcuts press the keyboard keys simultaneously, e.g. ⇧⌘A means ⇧+⌘+A.

Icon Keyboard Meaning Icon Keyboard Meaning
command caps lock
option (or alt) carriage return (return/enter)
control delete/backspace
fn function forward delete (fn + ⌫)
shift (either left or right) escape

Completely removing R from macOS

If you have previously installed R and wish to re-install the latest version or your current installation is not working as you expect, you first need to delete everything related to R. In macOS a complete removal is somewhat complicated, but doable if you follow all steps precisely as provided in this post.

For the complete removal of everything related to R the Terminal application will be used. In your Finder > Applications (shortcut: ⇧⌘A) there is a Utilities folder as depicted in Figure 1 below.

Finder Utilities Folder.
Finder Utilities Folder.

Within this Utilities folder, which can be directly accessed by using the ⇧⌘U shortcut, the Terminal application is contained. The icon in Figure 2 shows what the Terminal application looks like in the Finder > Applications > Utilities folder.

Icon Terminal Application.
Icon Terminal Application.

The Terminal application can also be started via Lauchpad under the ‘Other’ group.

Steps for a complete removal of R on macOS:

  1. Start the Terminal application. The terminal console prompt, where the commands will be entered, is depicted by a % or a $ sign. Which sign is shown, depends whether your default shell is zsh (% sign) or bash ($ sign).
  2. To delete the R application copy (⌘C) the following line, paste (⌘V) it behind the terminal console prompt and press return (↩) to execute. Provide the macOS password when asked (the typed password will not be visble!).
sudo rm -r /Applications/R.app
  1. To delete the whole framework running behind R copy (⌘C) the following line, paste (⌘V) it behind the console prompt in the terminal and press return (↩) to execute. No password will be asked anymore, as long as you do not close the terminal application.
sudo rm -r /Library/Frameworks/R.framework
  1. To be able to re-install R the installation receipts need to be removed. This is done by copying (⌘C) the following line (including the *), pasting (⌘V) it behind the prompt in the terminal and pressing return (↩) to execute.
sudo rm -r /private/var/db/receipts/org.r-project.*
  1. Delete R application support by copying (⌘C) the following line, pasting (⌘V) it behind the prompt in the terminal and pressing return (↩) to execute.
sudo rm -r ~/Library/Application Support/R
  1. Clean the cache of R by copying (⌘C) the following line, pasting (⌘V) it behind the prompt in the terminal and pressing return (↩) to execute.
sudo rm -r ~/Library/Caches/org.R-project.org
  1. To remove R preferences copy (⌘C) the following line, paste (⌘V) it behind the prompt in the terminal and press return (↩) to execute.
sudo rm ~/Library/Preferences/org.R-project.org.plist
  1. Deletion of all previously installed user packages is achieved by copying (⌘C) the following line, pasting (⌘V) it behind the prompt in the terminal and pressing return (↩) to execute. A notification will be given in case the folder does not exist.
sudo rm -r ~/Library/R
  1. Delete user created environment variables, used at startup of R, copy (⌘C) the following line, paste (⌘V) it behind the prompt in the terminal and press return (↩) to execute. A notification will be given in case the file does not exist.
sudo rm  ~/.Renviron
  1. Finally delete the user folder with additional settings by copying (⌘C) the following line, pasting (⌘V) it behind the prompt in the terminal and pressing return (↩) to execute. A notification will be given in case the folder does not exist.
sudo rm -r ~/.R
Having performed all 10 steps given above, your mac will be ready for a new installation of R. Leave the terminal console open for now, you might need it later on.

Download

At the time this post was written the latest release of R is version 3.6.3. It has been updated to the latest release version 4.1.0 of R for MacOS High Sierra or higher.

For newer versions of R than 3.6.3 the steps described below are the same and still correct, but start with a newer version of the downloaded package file of R. The screenshots in this post have not been updated, therefore what you see during your installation can differ with respect to the version number shown in the screenshots.

For macOS there are three downloads for R available on the R-project website. To see which version of macOS is installed on your mac, click on in the menu bar and select ‘About This Mac’.

NOTE: If you are using a mac with the newest M1 processor, download the Apple Silicon arm64 build!

Download R for your specific version of macOS using one of the following links:

Installation

For installing R on macOS follow these steps:

  1. Open the downloaded file, either R-3.6.3.nn.pkg, R-4.1.0.pkg or R-4.1.0-arm64.pkg depending or your version of macOS or processor (as explained above). This file will most likely reside in Finder > Downloads (shortcut: ⌥⌘L). The file can more easily be found by switching into List view (shortcut: ⌘2). To switch to Icon view use the shortcut: ⌘1. The installer package will resemble the image displayed below in Figure 3 (text underneath can, and probably will, differ!).
Icon R Installer Package.
Icon R Installer Package.
  1. The installler will start and display the introduction as shown below in Figure 4. Click the ‘Continue’ button to proceed.
R Installer Introduction.
R Installer Introduction.
  1. Now the Read Me for the software to be installed as displayed in Figure 5 below. Click the ‘Continue’ button to proceed.
R Installer Read Me.
R Installer Read Me.
  1. Right after the Read Me a Software Licence Agreement will appear. By clicking the ‘Continue’ button you will be asked to agree with this software licence agreement as diplayed below in Figure 6. Click on ‘Agree’ to proceed.
R Installer License Agreement.
R Installer License Agreement.
  1. The installer will select the best destination to install the software for you and will display the Installation Type as shown in Figure 7. Click on the ‘Install’ button to start the software installation.
Do not Customise the installation type, unless you know what you are doing.
R Installer Installation Type.
R Installer Installation Type.
  1. Before the software installation will commence, confirmation of the user is requested as displayed below in Figure 8. Either use the finger print scanner on the touch bar of your mac or confirm using the password of your mac.
Confirm R Software Installation.
Confirm R Software Installation.
  1. The software installer will start installing R onto your mac. When completed the installer will show a summary stating that the installation was successful as shown in Figure 9 below. Click on the ‘Close’ button.
R Software Installation Success.
R Software Installation Success.
  1. The installer will finally ask you whether you want to keep or move to R installer package to the trashbin. Click ‘Move to Bin’ to discard the installer package.
Congratulations 😆, you now have R 3.6.3 or higher installed on your mac! Before actively using the R application, some configuration will be required. The configuration is described in the next section.

Configure the R application on macOS

To configure the R application on macOS perform the following steps:

  1. Start the R application from Finder > Applications (shortcut: ⇧⌘A) or via Launchpad. The icon representing the R application is shown below in Figure 10.
R Application Icon.
R Application Icon.
  1. The R Console will open, as shown in Figure 11 below, and the cursor will be ready for input behind the prompt, indicated by the > sign. In case the R Console displays a non-UTF8 locale warning, than this needs to be remedied first. Go to the section entitled Fix R application non-UTF8 locale warning in this post to resolve this issue.
R Application Console.
R Application Console.
  1. Navigate the mouse pointer to the menu bar click on ‘R’ > ‘Preferences…’ (shortcut: ⌘,) to open the R application preferences. The Preferences window displayed in Figure 12 will appear.
R Application Preferences.
R Application Preferences.
  1. Click on Startup. The Preferences window will change into what is shown in Figure 13.
R Application Startup Preferences.
R Application Startup Preferences.
  1. Match the Startup settings displayed above in Figure 13. To select the Default CRAN mirror click on the ‘Select’ button. The window shown below in Figure 14 will appear. Select ‘0-Cloud [https]’ and click ‘OK’ to confirm. Having matched the settings, close the window using the red ball in the top left corner of the Startup window.
R Application Startup Preferences CRAN Mirror Selection.
R Application Startup Preferences CRAN Mirror Selection.
  1. Quit the R application either by:

    • Typing q() or quit() behind the R Console prompt (indicated by the > sign) and pressing return (↩) to execute.
    • Using the keyboard shortcut: ⌘Q
    • Navigating the mouse pointer to the menu bar and clicking ‘R’ > ‘Quit R’
    • Navigation the mouse pointer to the top left corner of the R Console window and clicking on the red ball
  2. No matter what you choose, you will always be asked whether you want to save a workspace image as shown below in Figure 15. Just click on the ‘Don’t Save’ button to end the R application.

R Application Save Workspace.
R Application Save Workspace.
Having configured the R application, you are now ready to actively start using it!

Fix R application non-UTF8 locale warning

When the R Console displays a non-UTF8 locale warning at the startup of the R application, it will look like the image shown below in Figure 16.

R Application Console with Language Warning.
R Application Console with Language Warning.

The remedy for this issue is not difficult, just perform the following steps:

  1. Go to the open terminal console. If you do not have one yet, open the Terminal application from Finder > Applications > Utilities (shorcut: ⇧⌘U) or via Lauchpad under the ‘Other’ group. The terminal console prompt, where the commands will be entered, is depicted by a % or a $ sign. Which sign is shown, depends whether your default shell is zsh (% sign) or bash ($ sign).
  2. Copy (⌘C) the following line, paste (⌘V) it behind the prompt in the terminal console and press return (↩) to execute the command.
defaults write org.R-project.R force.LANG en_US.UTF-8
  1. Quit the active terminal console by typing exit and pressing return (↩) to execute. To quit the Terminal application completely you can use the keyboard shortcut: ⌘Q or navigate the mouse pointer to the menu bar and click ‘Terminal’ > ‘Quit Terminal’.
  2. Go back to the R Console and quit the R application either by:
    • Typing q() or quit() behind the R Console prompt (indicated by the > sign) and pressing return (↩) to execute.
    • Using the keyboard shortcut: ⌘Q
    • Navigating the mouse pointer to the menu bar and clicking ‘R’ > ‘Quit R’
    • Navigation the mouse pointer to the top left corner of the R Console window and clicking on the red ball
  3. No matter what you choose, you will always be asked whether you want to save a workspace image as shown in Figure 17 below. Just click on the ‘Don’t Save’ button to end the R application.
R Application Save Workspace.
R Application Save Workspace.
  1. Go back to step 1. of the Configure the R application on macOS section.
Maikel Verouden, Ph.D.
Maikel Verouden, Ph.D.
Researcher | Lecturer | IT & Organization contact person

My research interests include Statistics, Teaching and programmable matter (statistical software).