How to debug R code within a package

My R code fails within a 3rd party package. I would like to debug it to understand what exactly causes the code to fail (and possibly change my input parameters). Is there a way to do it?

The package I have a problem with is mgcv (function gam()).
It runs fine for some formulas I have and fails or takes forever with others.
I wonder if I can debug the code within the package to see where the code fails.