Main r code.