Mapping with R on Mac: Installation

Jin
Jin
Jul 8, 2018 · 2 min read

I’m learning geocomputation with R by following the awesome content in this book: https://geocompr.robinlovelace.net/spatial-class.html

It’s well written and easy to follow. But I bumped into a couple issues when I was trying to install the required packages, which were not mentioned in the book. In case anyone else face the same problem, here are the steps I took to make it work.

Step 1: Install Homebrew if you haven’t yet

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2: On Mac, a few requirements must be met to install sf package in R. More information can be found here at github.com/r-spatial/sf.

If you have never installed GDAL before, install it with Homebrew first

brew install gdal

Then, upgrade to gdal 2 so that you can install the sf package later. To do that, nlink gdal 1.x

brew unlink gdal

Then, tap into osgeo4mac

brew tap osgeo/osgeo4mac && brew tap --repair

Then, install GDAL 2.X

brew install proj
brew install geos
brew install udunits
brew install gdal2 --with-armadillo --with-complete --with-libkml --with-unsupported

Finally, link GDAL 2.X

brew link --force gdal2

If you want to verify your installation (version number may vary):

$ gdal-config --version
2.3.1
$ gdal-config --libs
-L/usr/local/Cellar/gdal2/2.3.1/lib -lgdal
$ gdal-config --cflags
-I/usr/local/Cellar/gdal2/2.3.1/include

Step 3: Install packages

When I was trying to install sf in R, it threw me an error saying “tar: Failed to set default locale”. I managed to solve this issue by running system('defaults write org.R-project.R force.LANG en_US.UTF-8')in R console, then restart your R.

Then install all the follow packages. Worked like a charm.

install.packages("sf")
install.packages("raster")
install.packages("spData")
install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/",type = "source")

Now if you run the following the code in R, there shouldn’t be any error.

library(sf)         
library(raster)
library(spData)
library(spDataLarge)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store