I have some questions regarding this article

The author found the parameters p, d, q through trial and error minimizing AIC and/or BIC. What are the commands for calculating AIC or BIC in R? What is the philosophy behind choosing p,d,q? What does it mean that d=2 or 3 or 4?