Viewing the Source Code of a Function
Understanding the source code of a function can be immensely helpful for several reasons. It allows you to see the exact steps the function takes to produce its output, which can deepen your understanding of the R Programming Language.
1. Using the print or function Command
For functions that are not primitive (i.e., not written in C or Fortran), you can simply type the function name without parentheses, or use print.
# Example with the lm function
print(lm)
# or just
lm
Output:
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
2. Using the getAnywhere Function
The getAnywhere function can be used to locate and display the source code of a function, even if it is not exported from a namespace.
# Example with the lm function
getAnywhere(lm)
Output:
A single object matching ‘lm’ was found
It was found in the following places
package:stats
namespace:stats
with value
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
{
3. Using the View Function in RStudio
If you are using RStudio, you can use the View function to see the source code in a viewer.
# Example with the lm function
View(lm)
Output:
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...) {
4. Using the trace Function
For functions that are part of a package and might not be directly accessible, you can use the trace function to insert a tracing function that displays the source.
# Example with the lm function
trace(lm)
Output:
if (!qr)
z$qr <- NULL
z
}
<bytecode: 0x0000012c714000d0>
<environment: namespace:stats>
5. Using the deparse and body Functions
You can use deparse along with body to print the source code of a function.
# Example with the lm function
cat(deparse(body(lm)), sep = "\n")
Output:
{
ret.x <- x
ret.y <- y
cl <- match.call()
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "subset", "weights", "na.action",
"offset"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
6. Viewing Source Code of S3 Methods
For S3 methods, you need to use the getS3method function to get the specific method for a class.
# Example with the plot function for lm objects
print(getS3method("plot", "lm"))
Output:
function (x, which = c(1, 2, 3, 5), caption = list("Residuals vs Fitted",
"Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage",
expression("Cook's dist vs Leverage* " * h[ii]/(1 - h[ii]))),
panel = if (add.smooth) function(x, y, ...) panel.smooth(x,
y, iter = iter.smooth, ...) else points, sub.caption = NULL,
main = "", ask = prod(par("mfcol")) < length(which) && dev.interactive(),
..., id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75,
qqline = TRUE, cook.levels = c(0.5, 1), cook.col = 8, cook.lty = 2,
cook.legendChanges = list(), add.smooth = getOption("add.smooth"),
iter.smooth = if (isGlm) 0 else 3, label.pos = c(4, 2), cex.caption = 1,
cex.oma.main = 1.25, extend.ylim.f = 0.08)
{
7. Viewing Source Code of Functions from Packages
For functions that are part of a package, you can load the package and then use one of the above methods.
# Load the package
library(ggplot2)
# Example with the ggplot function
print(ggplot)
Output:
function (x, which = c(1, 2, 3, 5), caption = list("Residuals vs Fitted",
"Q-Q Residuals", "Scale-Location", "Cook's distance", "Residuals vs Leverage",
expression("Cook's dist vs Leverage* " * h[ii]/(1 - h[ii]))),
panel = if (add.smooth) function(x, y, ...) panel.smooth(x,
y, iter = iter.smooth, ...) else points, sub.caption = NULL,
main = "", ask = prod(par("mfcol")) < length(which) && dev.interactive(),
..., id.n = 3, labels.id = names(residuals(x)), cex.id = 0.75,
qqline = TRUE, cook.levels = c(0.5, 1), cook.col = 8, cook.lty = 2,
cook.legendChanges = list(), add.smooth = getOption("add.smooth"),
iter.smooth = if (isGlm) 0 else 3, label.pos = c(4, 2), cex.caption = 1,
cex.oma.main = 1.25, extend.ylim.f = 0.08)
{
How to View the Source Code for a Function in R?
If you’re diving into R programming, there will come a time when you want to look under the hood and see how a function works. Maybe you’re curious about the mechanics, or you want to understand it better to use it more effectively. Here’s a guide to help you view the source code for a function in R.
Contact Us