R programming
source code
function
debugging
duplicate question

show source code for function in R

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Inspecting function source in R is essential for debugging unexpected behavior and learning package internals. The exact command depends on whether the target is a regular function, an S3 or S4 method, or a primitive backed by compiled code. A clear inspection workflow saves time and avoids looking at the wrong implementation.

Inspect Plain R Functions

For standard R functions, printing the function object often reveals the source body directly.

r
1# base function
2mean
3
4# user function
5f <- function(x) {
6  x * 2 + 1
7}
8
9f
10body(f)
11formals(f)

body and formals are useful when you only need logic or parameter structure.

Handle S3 Generic Dispatch Correctly

Many functions are S3 generics. Inspecting the generic alone may not show the method actually executed.

r
1# list methods for an S3 generic
2methods("print")
3
4# inspect specific method
5getAnywhere("print.data.frame")

Always check object class first:

r
x <- data.frame(a = 1:3)
class(x)

Then inspect the corresponding method.

Handle S4 Methods

S4 dispatch requires method lookup by signature. Use S4 introspection helpers.

r
showMethods("plot")

For S4-heavy packages, dispatch awareness is mandatory because method resolution can differ by argument class combinations.

Inspect Non-Exported Package Functions

Useful internals are often not exported. For inspection, getAnywhere and namespace access help.

r
getAnywhere("predict.lm")
stats:::predict.lm

Triple-colon namespace access is fine for diagnostics, but avoid depending on non-exported functions in production code when possible.

Resolve Name Collisions Across Packages

Different packages can define functions with the same name. Confirm where your active function is coming from.

r
1find("filter")
2getAnywhere("filter")
3
4# explicit namespace
5stats::filter
6dplyr::filter

This step prevents debugging the wrong implementation.

Primitive and Compiled Functions

Not all functions expose full R bodies. Some are primitives or wrappers around compiled code.

r
is.primitive(sum)
sum

If a function is primitive, you may need package C or C++ source for deep inspection rather than only REPL commands.

Practical Debugging Workflow

A repeatable flow:

  1. Identify runtime object class.
  2. Resolve generic to concrete method.
  3. Inspect function source with getAnywhere or namespace access.
  4. Reproduce behavior with a minimal example.
  5. Confirm version context with session metadata.
r
sessionInfo()

Version context is critical because implementation can differ across R and package releases.

Use Source References and Documentation Together

Source inspection is stronger when paired with docs and source refs.

r
?lm
help("predict.lm")
attr(stats::lm, "srcref")

If source-reference metadata is missing, consult package source repositories matching your installed version.

IDE and Tooling Tips

RStudio and similar IDEs can jump to function definitions and method implementations. Combine IDE navigation with console commands for faster iteration. For package development, trace and debugging hooks can complement source reading by showing runtime path.

Fast Reproducibility Checklist

When sharing source-inspection findings with teammates, include a short checklist in your issue notes:

  • Output of sessionInfo.
  • Object class used during dispatch.
  • Exact method name inspected.
  • Minimal reproducible input.

This keeps collaboration focused and avoids back-and-forth on environment mismatches.

Common Pitfalls

  • Inspecting only a generic and missing class-specific method dispatch.
  • Ignoring package namespace collisions and debugging wrong function.
  • Expecting full R source for primitive compiled functions.
  • Using non-exported APIs in production based on inspection convenience.
  • Skipping sessionInfo and failing to reproduce behavior across machines.

Summary

  • Start by identifying function type and dispatch model.
  • Use direct print, body, and formals for plain functions.
  • Use methods, showMethods, and getAnywhere for dispatched methods.
  • Confirm namespace source before analysis in multi-package sessions.
  • Pair source inspection with version metadata to keep debugging reproducible.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.