← All posts

How to Find Function Origins & Namespace Discipline in RStudio

Clinical Epidemiology ResearchData Analytics or StatisticsR [Data Analytics]
On this page

1. Base R Functions Require No Library

Some functions—such as paste0(), mean(), round(), factor()—belong to base R, which loads automatically.

To verify:

getAnywhere("paste0")

Typical output:

A single object matching ‘paste0’ was found
It was found in:
  package:base

Therefore:paste0() → base package → no library() required.


2. Determine the Origin of Any Function

R exposes two powerful tools for function lookup.

Method 1 — getAnywhere()

Works even for hidden or S3/S4 generic methods.

getAnywhere("tidy")

Method 2 — find()

Lists all packages on the search path that export a function with that name.

find("tidy")

If multiple packages contain a function of the same name, you may see:

"package:broom.mixed"
"package:broom"

This reveals where masking can occur during analysis.


3. Explicit Namespace Calling (Best Practice in CECS Research)

To ensure reproducibility and avoid masks:

❌ Avoid

tidy(model)

✔ Prefer

broom::tidy(model)

Advantages


4. Detect Masks When Loading Packages

Any time you load libraries, R announces conflicts, e.g.:

The following objects are masked from 'package:stats':
    filter, lag

Meaning:

stats::filter()
dplyr::filter()

Namespace discipline is essential in multistage modeling pipelines (GLM → GEE → mixed-effects → survival).


5. CECS Cheat Sheet: Where Common Functions Come From

FunctionPackage
paste0, sprintf, factor, roundbase
mutate, filter, select, case_when, %>%dplyr
pivot_wider, pivot_longertidyr
tidy, glance, augmentbroom
tidy (mixed models)broom.mixed
emmeans, contrastemmeans
glmmTMBglmmTMB
gt, gtsavegt
read_dtahaven

6. How to Know Which tidy() Was Used?

Since both broom and broom.mixed contain tidy():

find("tidy")

Returns something like:

"package:broom.mixed"
"package:broom"

The first on the search path is the one being used.

Therefore: ALWAYS specify namespaces.

broom::tidy(model)
broom.mixed::tidy(model)

7. Why This Matters in Clinical Epidemiology & Statistics

Ambiguous function calls produce:

Explicit namespaces (package::function) eliminate these risks.


8. Ready-to-Use CECS Code Snippets

Identify function origin

getAnywhere("FUNCTION")
find("FUNCTION")

Audit all attached packages

sessionInfo()
search()

Force explicit namespace for entire script

broom::tidy(model)
dplyr::mutate(df, ...)
tidyr::pivot_wider(df)


Conclusion

Function origin identification is not cosmetic — it is a core reproducibility requirement in CECS-level clinical analytics. By using getAnywhere(), find(), and explicit package::function notation, you guarantee:

0
Message for International and Thai ReadersUnderstanding My Medical Context in ThailandRead more →Message for International and Thai ReadersUnderstanding My Broader Content Beyond MedicineRead more →

Comments

No comments yet. Be the first to share your thoughts.

Sign in to comment