1. sp and S4
2. Two types of sp object
In the previous exercises you came across two examples of the types of object the sp package defines: a SpatialPolygons object, and a SpatialPolygonsDataFrame object.
The primary difference? The SpatialPolygonsDataFrame object was just like a SpatialPolygons object except with the addition of a data frame associated with it.
3. SpatialPolygons object
Let's take a closer look at the SpatialPolygons example. When you looked at the structure you saw some unusual things: a statement about a Formal class and that this object has 4 slots.
Despite that being a bit foreign, you can see there is a place for the spatial objects to live, in this case polygons, a place to store coordinate information and some other useful attributes like the bounding box, the range of the spatial coordinates in this data.
4. SpatialPolygonsDataframe object
In the SpatialPolygonsDataFrame object, the only difference is one more slot, a place to store a data frame, with as many rows as there are polygons.
But what is going on with this statement about Formal class and this talk about slots?
5. S4
The objects sp defines are an example of S4 classes. S4 is one of the object oriented systems in R. That is, it is one set of protocols, for defining object classes and methods. Classes and methods are central to object oriented programming. A class defines an object, its attributes and relationships to other classes. Classes aren't very useful without the addition of methods. Methods are simply functions, but functions whose behavior depends on the class of object used as input.
This may all sound a bit new, but you've been working with classes and methods as long as you've been using R. It's this idea that allows the function plot to produce something different when you pass it a matrix, versus a data frame. However, most of the object classes you've come across have been implemented in the S3 system, a little less formal than S4.
Often whether an object is implemented in S3 or S4 won't matter, until you want to access part of the object and you look inside. S3 objects are often lists which you already know how to pull apart. S4 objects are similar but different, there are multiple space to put things, but these places are called slots. You can access slots but not in the same way you can access elements in a list, you'll learn how in a moment. Slots can store anything, so like lists, S4 objects are recursive, you can put S4 objects in the slots of other S4 objects.
If you are interested in learning more about S4, we recommend the chapter on object-oriented programming in Hadley Wickham's Advanced R book.
6. Accessing slots
There are a couple of options for accessing slots in an S4 object. First, for commonly needed information, a method may be provided. This is the case for the coordinate information in sp objects. The function proj4string will return the value stored in the proj4string slot.
If no method is provided, you can access a slot using the "at" symbol followed by the slot name, kind of like using the "dollar sign" with lists. Alternatively, the slot function can be passed an object and slot name in quotes, to extract the corresponding slot from the object.
7. Let's practice!
To practice accessing slots, you will explore the structure of the polygons stored in the SpatialPolygonsDataFrame in the next few exercises.