Read Sections 14.1 to 14.4 of R4DS. Some parts of this are tedious but please pay close attention to the examples.