I’ve been playing around with the intamap
package recently as I’m interested in how it implements inverse distance weighting interpolation. I heard that the package uses a cross-validation approach to optimize the distance power, but I want to understand how the algorithm works and that means looking at the source code. Most of the time, viewing a function’s source code is straightforward: just type the name of the function in the R prompt, without parentheses. For instance:
gives you the source code for the interpolate function. But sometimes typing the function name doesn’t seem to give you what you want:
If the function or class is overloaded (defined for multiple types of inputs), you need an additional step to view the code for the specific version of the function you’re interested in. You can get a list of the versions of the function using methods()
for S3 classes or showMethods()
for S4 classes (if you’re interested in learning more about these classes, this StackOverflow link will get you started). Once you know which version of the function you want to look at, just type it’s full name to view the source code:
For some functions, methods()
will return a list in which some or all of the entries end with an asterisk (*). Trying to view the source code by just typing the function name yields an error:
The reason for this is that functions marked with an asterisk are non-visible. Meaning, they aren’t defined in the global environment. That doesn’t mean you can’t view the source code though, you just need to specify the namespace as well:
Now you have the basic tools you need to explore R packages and check the source code for functions you’re interested in.
Comments
Want to leave a comment? Visit this post's issue page on GitHub (you'll need a GitHub account).