R macOS

Intalling R and RStudio - macOS

Intalling R and RStudio - macOS

This is the first post in the series, Installing R and RStudio, where I will describe the simple steps of installing R and RStudio on macOS. In future posts, I will guide you through the installation on Ubuntu Linux and Windows. The idea is to provide some useful information, especially for those who are installing R for the first time.

This post is about installing the latest version of R (currently 4.0) and RStudio (1.2.5042) on a virtual machine with a clean and up-to-date version of macOS (10.15.4).

R Introduction

Assuming you are new to R, below the description about this programming language.

R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. The R language is widely used among statisticians and data miners for developing statistical software and data analysis

Source: Wikipedia

Basically, R is a simple and powerful programming language for statistical computing. The R language is widely used among statisticians and data miners for the development of statistical software and data analysis. For a more detailed description, you can read the about webpage or just visit the official R Project homepage.

Before jumping into the installation, another word/abbreviation that you will frequently encounter when programming using R is CRAN. CRAN stands for Comprehensive R Archive Network. It is a network of ftp and web servers around the world that store identical, up-to-date, versions of code and documentation for R.

Installing R

To install R on macOS, you could go to the R Project homepage and click a few times until you get to the webpage to download the macOS binary package.

This package includes the R framework allowing you to run R commands and programs from terminal. It also includes the 64-bit GUI application R.app and also two libraries, one called Tcl/Tk for X11 and the other is Texinfo. The latter two libraries are optional and can be ommitted via the “custom install” option.

I won’t add a direct link here because the filename does change with each new release. An easier option is to follow the 3 steps described below.

  1. Visit the specific page for macOS at https://cran.r-project.org/bin/macosx/
  2. Look for “Latest release
  3. Click at the link to the installation package (.pkg)
macOS downloads at CRAN R Project

macOS downloads at CRAN R Project

The step 4 in the picture above is there to suggest you to visit the website www.xquartz.org and download the installation package for the X11 library so that you can use the tlctk package, otherwise you won’t be able to load it. With the Tcl/Tk package you can create simple Graphical User Interface for R programs. I haven’t used this package yet and therefore I cannot judge if using Tcl/Tk would be a good idea or not.

The Texinfo package is required in case you would like to build package documentation from sources. To write for example reproducible reports in R, you will most likely use the R packages called R Markdown or Knitr, and depending on the format you are exporting, you probably won’t need Texinfo. Since the amount of disk space required for those libraries is very small, it might be worth to just install it.

By the way, an R package is a collections of R functions, data, and compiled code in a well-defined format. Check Hadley Wickham’s R Packages page for more information about packages.

Below the installation screenshots. In these, I point out the information about the packages described above and also in the “customise” option, as this can be overlooked.

After completing the installation, you can find the R GUI application in Applications and you can run R from the terminal using R or Rscript commands.

R GUI Application and R on Terminal

R GUI Application and R on Terminal

The message “Unable to create Basic Accelerated OpenGL renderer.” appeared due to a technical limitation of macOS running on a VM (more on this at https://kb.parallels.com/124138), but you won’t see this message when running R natively on your macOS computer.

Another recommendation is to install Apple’s Xcode Command Line Tools, as this is something you may need sooner or later.

When installing an R package, you may see the question “Do you want to install from sources the packages which need compilation? (Yes/no/cancel)” as below.

Do you want to install packages from sources?

Do you want to install packages from sources?

In case you select the option Yes to compile, then the installation may fail and you will see this prompt to install the Command Line Developer Tools.

Command Line Developer Tools installation request

Command Line Developer Tools installation request

Going a step further with our installation, I want to point out that specific packages may have additional dependencies that require the installation of extra software. For example, the package xlsx, useful for reading and writing Excel files, require Java.

Note: There are other packages to read and write Excel files with zero dependency (e.g. readxl / writexl and openxlsx).

You are able to install the package xlsx, but you won’t be able to load without having Java installed.

Error Loading Package XLSX Without Java

Error Loading Package XLSX Without Java

Something interesting is that when you check the version of Java installed using terminal (command: java -version), macOS will automatically guide you to visit the Java Development Kit website to download Java.

Clicking on More info… will take you to Oracle’s Java Development Kit download website so you can download the installer.

Oracle's Java JDK Download Website

Oracle's Java JDK Download Website

After downloading and installing Java, you can confirm the version of Java and also load the xlsx library in R.

Loading Package XLSX Successfully

Loading Package XLSX Successfully

RStudio

To develop in R, I highly recommend using RStudio.

RStudio is an integrated development environment (IDE) for R. It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management. RStudio is available in open source and commercial editions and runs on the desktop (Windows, Mac, and Linux) or in a browser connected to RStudio Server or RStudio Server Pro (Debian/Ubuntu, Red Hat/CentOS, and SUSE Linux)

Source: RStudio website

RStudio Website

RStudio Website

RStudio

RStudio

You probably noticed that R and RStudio are two different things. R is the programming language and RStudio is an Integrated Development Environment (IDE) that helps you develop programs in R. You can definitively use R without installing RStudio.

The link with the installation packages for RStudio Desktop (Open Source License) is: https://RStudio.com/products/RStudio/download/#download

I don’t use RStudio in a VM, but I just installed in one for this post. I noticed that you need to have your Graphics settings adjusted to Best for Retina display, you might encounter such issue:

RStudio with Best for Retina setting

RStudio with Best for Retina setting

One solution is to change VM Graphics settings to Scaled

RStudio with Scaled setting

RStudio with Scaled setting

Another alternative is to change an option in RStudio through the menu Tools > Global Options, click on Advanced, and in OS Integration, select the option Software for Rendering engine. To change this option, you will probably need to change the VM Graphics settings, adjust the option in RStudio and change the VM Graphic settings back to Best for Retina display.

RStudio with Best for Retina and Software Rendering

RStudio with Best for Retina and Software Rendering

Summary

I hope this post could clarify possible problems during the installation of R and RStudio on macOS. Happy R coding ;-)