Install R for macOS as non-root

R/RStudio/R packages install on macOS without root permission

We will extract the files from the .pkg and install those

  1. Pick "Download R for (Mac) OS X" from the nearest download mirror: UmU | Pick different mirror
  2. Choose the latest binary for the base distribution. Note that you may need to install XQuartz if not already installed, if you need to use X11.
  3. Download the .pkg file (R-4.0.0.pkg) and place it somewhere you can easily find it, like on the Desktop. Also create a new folder and name it something. This folder is just for convenience during extracting.
  4. Open terminal (click on "Spotlight Search" and type "terminal" then hit return).
  5. Type "pkgutil --expand R-4.0.0.pkg" (or type "pkgutil --expand " and drag and drop the downloaded .pkg file to the window), then drag and drop the newly created folder there.
  6. Hit return. It will complain. Take the newly created folder and throw it in the trash. Go to the terminal window and press the "arrow up" key on the keyboard, then press return to rerun the command. 
  7. The package will now have been extracted to the destination folder. Inside you will find some packages, including "R-app.pkg" and "R-fw.pkg". There will also be "tcltk.pkg" and "texinfo.pkg", those can be omitted unless you need the tcltk R package or want to build documentation from sources.
  8. Right-click on each of "R-app.pkg" and "R-fw.pkg" and choose "Show Package Contents". There will be a file called "Payload" in each of them. This is the file you actually want to extract. Do so by repeating the commands from step 5 and 6 on "R-app.pkg" and "R-fw.pkg".
  9. You can now extract the payload file in a terminal window with "tar -xvf " and drag-drop the "Payload" file there. Click "return". It will extract and you can now go into the created folder and find the actual .app file and drag it to wherever you would normally install it, like your "Applications", or just run it from where it is on the desktop.

Some of these steps are explained in this YouTube video.

RStudio

The base version of RStudio is free. It should suffice for the purposes of the course.

  1. Go to the download page for RStudio and pick the "Free" version
  2. Scroll down a little and pick the macOS 10.13+ .dmg file to download. 
  3. When it has downloaded, double-click it to open, and then drag and drop it to your applications folder.

R packages - installing

You will find a list of R packages here. The link goes to the mirror located in Umeå, but you can find other mirrors from this page.

You can see which packages are already installed by starting R and giving the command

installed.packages()

NOTE: The list of packages needed for the course is here.

On macOS install.packages works as it does on other Unix-alike systems, but there is an additional type mac.binary (available for the CRAN distribution but not when compiling R from source) which can be passed to install.packages in order to download and install binary packages from a suitable repository. These binary package files for macOS have the extension ‘.tgz’. The R.APP GUI provides menus for installation of either binary or source packages, from CRAN or local files.

There are several procedure for installing R packages, but the simplest is to do it through R:

  1. Find the name of the package you want, for example from the list of R packages
  2. Start Spotlight to find and start the "R" ("R64" if you have) program
  3. Wait for R to start. Now, in the top menu, click “Packages & Data”, then click “Package Installer”.
  4. Click "Get List". The first time it will ask you to choose a mirror. Pick one that is close to your geographical location. There should be one for "ACC, Umeå University". Pick that if you are in Sweden. You can set it as default to avoid being asked every time.
  5. In the pop-up window, make sure "Install Dependencies" is checked. Click "Update All" and "OK".
  6. Choose the package you wish to install from the list. You can search for it as well. Again, make sure "Install Dependencies" is checked.
    1. If you have admin rights, just click "Install Selected".
    2. If you do not have admin rights, you must change the "Install Location" in the lower left of the window before clicking "Install Selected". You can either pick option "In Other Location" and be asked upon installation or "As defined by .libPaths()", but then you need to set .libPaths() first.
      1. open the Rprofile.site file which is normally located in <path-to-your-R-app>\etc and add

        .First <- function(){
          .libPaths("your path here")
        }

        Then the .libPaths() command will be evaluated just after the start of R.

  7. When your window prompt returns to a ">" the package has finished downloading and installing.

Only do the above once per package.

Another, faster method of installing packages is the following, though it requires that you know the name of the package:

  1. Start the "R" program
  2. Type “install.packages(“RPACKAGE”, dependencies=TRUE)”, where RPACKAGE should be changed to the name of the specific package in question. Then press the Enter/Return key.
  3. If you have not installed other packages during this session, you will be prompted to choose a mirror to download from.
  4. When your window prompt returns to a ">" the package has finished downloading and installing.

R packages - loading into R

  1. Start the "R" program if it is not already running
  2. Type “library(RPACKAGE)”, where RPACKAGE should be changed to the name of the specific R package in question. Then press the Enter/Return key.
  3. The package should now be loaded and available.

 

Also useful for R on macOS: https://github.com/foxlet/macOS-Simple-KVM

Updated: 2024-04-17, 14:47