How to deal with “could not find function” error in R?

R ProgrammingServer Side ProgrammingProgramming

The error “could not find function” occurs due to the following reasons −

  • Function name is incorrect. Always remember that function names are case sensitive in R.

  • The package that contains the function was not installed. We have to install packages in R once before using any function contained by them. It can be done as install.packages("package_name")

  • The package was not loaded before using the function. To use the function that is contained in a package we need to load the package and it can be done as library("package_name").

  • Version of R is older where the function you are using does not exist.

If you have installed and loaded many packages but forgot which package contains the function you are using then you can do it by using getAnywhere


> library(ggplot2)
> library(BSDA)
Loading required package: lattice
Attaching package: ‘BSDA’

The following object is masked from ‘package:datasets’ −


> library(purrr)
> getAnywhere(ggplot)
A single object matching ‘ggplot’ was found
It was found in the following places
with value
function (data = NULL, mapping = aes(), ..., environment = parent.frame()){
<bytecode: 0x0000000011201848>
<environment: namespace:ggplot2>

Here we have loaded three packages, named ggplot2, BSDA, and purr. Suppose we wanted to know which package contains ggplot function. So, we used getAnywhere and it returns the package name as ggplot2.

You should make sure that you do not make the mistakes mentioned above. If you use an older version of R but want to perform calculations using a function that is created for a newer version then it would not be possible. But it can become possible if you use package backports to make newly added functions available to older version of R. Also, you need to find the list of function that need to be backported on the git repo of backports. Just remember that R versions older than R3.0.0 are not compatible with packages built for R3.0.0 and later versions.

Published on 06-Jul-2020 18:30:44