Function not found
Matches: could not find function "([^"]+)"
WHAT THIS ERROR MEANS
R doesn't recognize the function you're trying to call. The package containing it may not be loaded, or the function name is misspelled.
WHY IT HAPPENS — 4 COMMON CAUSES
1. The package is installed but not attached
Installing a package puts it on disk; library() makes its functions visible. These are separate steps and only the second one has to be repeated in every new session. This is why a script that worked before a restart suddenly cannot find ggplot() or mutate().
2. The function name is misspelled or misremembered
R has several near-miss pairs that trip people constantly: length() not len(), nrow() not numrows(), seq_len() not seqlen(), tolower() not toLower(). Base R naming is inconsistent enough that guessing rarely works.
3. The function moved, was renamed, or was removed
Packages evolve. dplyr retired funs() in favour of the .fns argument, tidyr replaced gather() and spread() with pivot_longer() and pivot_wider(), and ggplot2 renamed several arguments. Older tutorials therefore reference functions your installed version no longer exports.
4. It lives in a package you have not installed at all
Blog posts and Stack Overflow answers often omit the library() line. A function like fread() or str_detect() looks like base R if you have not met it before — it belongs to data.table and stringr respectively.
HOW TO DIAGNOSE WHICH ONE IT IS
- Run search() to list every attached package. If the one you expect is missing, that is your answer.
- Run exists("fread") — FALSE confirms nothing by that name is reachable.
- Run ??fread to search across all installed packages, including unattached ones. It will tell you which package to load.
- If it finds nothing, the package is not installed: install.packages("data.table").
- To check whether your installed version still exports it, run ls("package:dplyr") and search the result, or packageVersion("dplyr") to compare against the docs you are following.
HOW TO FIX IT
1. Check the function name for typos.
2. Load the required package with library().
3. Use ?function_name or help(function_name) to verify it exists.
CODE EXAMPLES
# ggplot2 not loaded ggplot(data, aes(x, y)) + geom_point()
library(ggplot2) ggplot(data, aes(x, y)) + geom_point()
MORE SCENARIOS THAT TRIGGER THIS
Function belongs to a package that was never loaded
# stringr not attached str_detect(names, "^A") # Error: could not find function "str_detect"
library(stringr) str_detect(names, "^A") # or skip the attach entirely: stringr::str_detect(names, "^A")
The :: form is worth the extra typing in scripts other people will run — it documents the dependency at the point of use and cannot be broken by load order.
Following a tutorial written for an older package version
library(tidyr) gather(df, key, value, -id) # Error: could not find function "gather"
library(tidyr) pivot_longer(df, cols = -id, names_to = "key", values_to = "value")
gather() and spread() were superseded in tidyr 1.0 and eventually removed. If a tutorial predates 2019, expect several such renames.
PACKAGE-SPECIFIC NOTES
require() differs from library() in a way that matters here: it returns FALSE with a warning instead of stopping when the package is missing, so a script using require() will keep running and then fail later with this error, several lines away from the real cause. Use library() in scripts so the failure is immediate and obvious, and reserve require() for conditional code that genuinely handles the FALSE. In WebR and other browser-based R runtimes, packages must be installed through the runtime’s own installer rather than install.packages(), and not every CRAN package has a WebAssembly build.
FUNCTIONS WORTH KNOWING
library()require()requireNamespace()search()exists()installed.packages()packageVersion()FREQUENTLY ASKED QUESTIONS
Why does it work in the console but not when I knit or render?
Knitting starts a fresh R session that does not inherit your console’s attached packages. Every library() call the document depends on has to appear inside the document itself, normally in a setup chunk.
I installed the package and still get the error. Why?
Either you have not called library() since installing, or the install went to a library path this session is not reading. Compare .libPaths() with where the package landed, and check the install output for a non-fatal warning — a failed compile often prints a warning rather than an error.
What does it mean when the function exists but is "masked"?
Masking is the opposite problem: two attached packages export the same name and the later one wins, so you get the wrong function rather than none. R prints these conflicts when the package attaches. Resolve them with explicit dplyr::filter() rather than relying on load order.
Still stuck?
Paste your code in RChat and the AI will fix this error in context.