By Joris Meys


2011-08-11 14:04:19 8 Comments

This is meant to be a FAQ question, so please be as complete as possible. The answer is a community answer, so feel free to edit if you think something is missing.

This question was discussed and approved on meta.

I am using R and tried some.function but I got following error message:

Error: could not find function "some.function"

This question comes up very regularly. When you get this type of error in R, how can you solve it?

10 comments

@Gavin Simpson 2011-08-11 14:38:34

Another problem, in the presence of a NAMESPACE, is that you are trying to run an unexported function from package foo.

For example (contrived, I know, but):

> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"

Firstly, you shouldn't be calling S3 methods directly, but lets assume plot.prcomp was actually some useful internal function in package foo. To call such function if you know what you are doing requires the use of :::. You also need to know the namespace in which the function is found. Using getAnywhere() we find that the function is in package stats:

> getAnywhere(plot.prcomp)
A single object matching ‘plot.prcomp’ was found
It was found in the following places
  registered S3 method for plot from namespace stats
  namespace:stats
with value

function (x, main = deparse(substitute(x)), ...) 
screeplot.default(x, main = main, ...)
<environment: namespace:stats>

So we can now call it directly using:

> stats:::plot.prcomp(mod)

I've used plot.prcomp just as an example to illustrate the purpose. In normal use you shouldn't be calling S3 methods like this. But as I said, if the function you want to call exists (it might be a hidden utility function for example), but is in a namespace, R will report that it can't find the function unless you tell it which namespace to look in.

Compare this to the following: stats::plot.prcomp The above fails because while stats uses plot.prcomp, it is not exported from stats as the error rightly tells us:

Error: 'plot.prcomp' is not an exported object from 'namespace:stats'

This is documented as follows:

pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name.

@ErichBSchulz 2014-11-22 12:55:46

thanks - this saved me after upgrading to R 3 for could not find function "anova.lm"... fixed with calling stats:::anova.lm() instead

@NelsonGon 2019-05-07 16:53:58

While not that relevant, the use of ::: has been referred to as a design mistake and that :: is preferred. Cannot readily find the reference.

@Gavin Simpson 2019-05-07 18:47:23

@NelsonGon With all due respect, :: and ::: are different and your edit doesn't work! The plot.prcomp() function isn't exported from the stats namespace so you need to use :::.

@NelsonGon 2019-05-08 15:54:35

@GavinSimpson Right! I took a respected R dev's word for the design error and had never really verified it. Perhaps, it was their personal opinion.

@Joris Meys 2011-08-11 14:07:23

There are a few things you should check :

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  3. Did you attach that package to the workspace ? require(thePackage) or library(thePackage) (this should be done every time you start a new R session)
  4. Are you using an older R version where this function didn't exist yet?

If you're not sure in which package that function is situated, you can do a few things.

  1. If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
  2. find and getAnywhere can also be used to locate functions.
  3. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  4. RSiteSearch("some.function") or searching with rdocumentation or rseek are alternative ways to find the function.

Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.

@Benjamin 2016-05-18 20:45:25

Hi Joris, I have a quick question. I am new in R but I was able to successfully install it. I would like to use the "cosvol" function in the "celestial" package from command-line. Unlike my R which is installed from Fedora repository into my Linux system, I have downloaded my "celestial" package in a different directory in my "home". Each time I am requesting the function "cosvol()", it says, "could not find function "cosdistCoVol"." I am not sure how to let R knows about my director in which all the functions are downloaded in my "celestial" package separately. Your help is appreciated.

@mpag 2018-03-12 18:01:04

If the function is in one of the core/base R libraries, you may need to update that. In my case, I was trying to use the hasName function in utils. However, I was using 3.3.1 and hasName wasn't introduced until 3.4.0. As you can't update utils as a stand-alone library, R/R Studio said I didn't have any libraries to update.

@Joris Meys 2018-03-12 19:30:50

@mpag That's because the utils package is integral part of the R release. If you would use RSiteSearch("hasName") literally the first entry is a reference to the backports package that will make that function available in R 3.3.1. See also github.com/r-lib/backports for more info. I've added some info for that case, thx for notifying

@mpag 2018-03-12 22:45:24

@JorisMeys that's very helpful. I'd also like to submit that it should be standard practice to document when a function has been added to R on that function's help page (e.g. ?hasName). E.g. neither https://www.rdocumentation.org/packages/utils/versions/3.4.3‌​/topics/hasName nor https://stat.ethz.ch/R-manual/R-devel/library/utils/html/has‌​Name.html say "introduced in R 3.4.0" I ended up figuring it out by browsing through github repos and look at the blame for utils/R/hasName.R and base/R/match.R

@Joris Meys 2018-03-13 08:53:51

@mpag or you could have opened literally the first hit in RSiteSearch("hasName") and got the same information. That's why I added this years ago to that answer. It's a useful trick to know ;-)

@Tony Cronin 2018-03-26 15:36:56

You may be able to fix this error by name spacing :: the function call

comparison.cloud(colors = c("red", "green"), max.words = 100)

to

wordcloud::comparison.cloud(colors = c("red", "green"), max.words = 100)

@Joris Meys 2018-03-26 20:08:14

The error says "comparision" instead of"comparison". I reckon the namespace wasn't the problem :-)

@Tony Cronin 2018-03-26 21:01:01

Good spot @Joris Meys

@Akshay Vijay Jain 2016-11-14 04:41:03

I got the same, error, I was running version .99xxx, I checked for updates from help menu and updated My RStudio to 1.0x, then the error did not come

So simple solution, just update your R Studio

@Joris Meys 2016-11-18 10:09:23

Could you please elaborate on what the nature was of the error. This might help, but only in very specific cases.

@catastrophic-failure 2016-07-02 12:16:46

If you are using parallelMap you'll need to export custom functions to the slave jobs, otherwise you get an error "could not find function ".

If you set a non-missing level on parallelStart the same argument should be passed to parallelExport, else you get the same error. So this should be strictly followed:

parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")

@Jacob 2012-05-08 01:22:02

If this occurs while you check your package (R CMD check), take a look at your NAMESPACE.

You can solve this by adding the following statement to the NAMESPACE:

exportPattern("^[^\\\\.]")

This exports everything that doesn't start with a dot ("."). This allows you to have your hidden functions, starting with a dot:

.myHiddenFunction <- function(x) cat("my hidden function")

@Andrew 2013-09-18 15:00:05

This fails for me in RStudio - Error : '\.' is an unrecognized escape in character string starting ""^[^\."

@Andrew 2013-09-18 15:03:41

exportPattern("^[^\\.]") works

@Andre Luus 2015-06-23 10:02:45

Any suggestions to what I could do if I get the error while using a package I didn't write? The package itself seems to want to use an internal method that isn't defined because presumably the author didn't do the above.

@maj 2015-09-04 12:37:34

Rdocumentation.org has a very handy search function that - among other things - lets you find functions - from all the packages on CRAN, as well as from packages from Bioconductor and GitHub.

enter image description here

@Math 2014-07-11 08:21:08

This error can occur even if the name of the function is valid if some mandatory arguments are missing (i.e you did not provide enough arguments).
I got this in an Rcpp context, where I wrote a C++ function with optionnal arguments, and did not provided those arguments in R. It appeared that optionnal arguments from the C++ were seen as mandatory by R. As a result, R could not find a matching function for the correct name but an incorrect number of arguments.

Rcpp Function : SEXP RcppFunction(arg1, arg2=0) {}
R Calls :
RcppFunction(0) raises the error
RcppFunction(0, 0) does not

@swihart 2013-08-29 00:21:54

I had the error

Error: could not find function some.function

happen when doing R CMD check of a package I was making with RStudio. I found adding

exportPattern(".")

to the NAMESPACE file did the trick. As a sidenote, I had initially configured RStudio to use ROxygen to make the documentation -- and selected the configuration where ROxygen would write my NAMESPACE file for me, which kept erasing my edits. So, in my instance I unchecked NAMESPACE from the Roxygen configuration and added exportPattern(".") to NAMESPACE to solve this error.

@Joris Meys 2013-08-29 09:11:45

You better use roxygen2, that one recognizes edits you make to the namespace files and keeps them intact. I'd also advise strongly against using exportPattern(".") in the namespace file. Use the tag @export instead in your individual files, so you only export the functions that need exporting. Roxygen2 will automatically update the namespace to export all functions that need exporting.

@swihart 2013-11-02 21:06:11

Joris - I really appreciate you taking the time to comment; I agree 100% with what you wrote. I am now using devtools/roxygen2 and am putting the following in all the functions I need exported: #' @export

@Iterator 2011-08-11 14:36:04

I can usually resolve this problem when a computer is under my control, but it's more of a nuisance when working with a grid. When a grid is not homogenous, not all libraries may be installed, and my experience has often been that a package wasn't installed because a dependency wasn't installed. To address this, I check the following:

  1. Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R.
  2. Is Java installed? Are the Java class paths correct?
  3. Check that the package was installed by the admin and available for use by the appropriate user. Sometimes users will install packages in the wrong places or run without appropriate access to the right libraries. .libPaths() is a good check.
  4. Check ldd results for R, to be sure about shared libraries
  5. It's good to periodically run a script that just loads every package needed and does some little test. This catches the package issue as early as possible in the workflow. This is akin to build testing or unit testing, except it's more like a smoke test to make sure that the very basic stuff works.
  6. If packages can be stored in a network-accessible location, are they? If they cannot, is there a way to ensure consistent versions across the machines? (This may seem OT, but correct package installation includes availability of the right version.)
  7. Is the package available for the given OS? Unfortunately, not all packages are available across platforms. This goes back to step 5. If possible, try to find a way to handle a different OS by switching to an appropriate flavor of a package or switch off the dependency in certain cases.

Having encountered this quite a bit, some of these steps become fairly routine. Although #7 might seem like a good starting point, these are listed in approximate order of the frequency that I use them.

@42- 2011-08-11 15:09:45

Useful considerations to be sure, but more an answer for "Why do I get an error when installing a package".

@Iterator 2011-08-11 16:51:02

@DWin: Maybe, but not really. I may have been unclear. These issues come up when a job grinds to a halt on a grid because a package wasn't installed. Maintaining software consistency on a grid isn't hard, but does require a good process for installation, maintenance, and debugging. These are just some of the items that come up from each phase, at least as they relate to the screaching sound that comes when a function isn't available. :)

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

10 Answered Questions

[SOLVED] How can I view the source code for a function?

  • 2013-10-07 13:58:00
  • Joshua Ulrich
  • 231335 View
  • 472 Score
  • 10 Answer
  • Tags:   r function r-faq

37 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

30 Answered Questions

[SOLVED] Is there a built-in function for finding the mode?

  • 2010-03-30 17:55:07
  • Nick
  • 232121 View
  • 349 Score
  • 30 Answer
  • Tags:   r statistics r-faq

10 Answered Questions

[SOLVED] What does the exclamation mark do before the function?

  • 2010-09-20 21:21:51
  • Sebastian Otto
  • 144879 View
  • 1141 Score
  • 10 Answer
  • Tags:   javascript function

3 Answered Questions

[SOLVED] data.table vs dplyr: can one do something well the other can't or does poorly?

  • 2014-01-29 15:21:45
  • BrodieG
  • 101177 View
  • 666 Score
  • 3 Answer
  • Tags:   r data.table dplyr

31 Answered Questions

[SOLVED] What's the difference between a method and a function?

18 Answered Questions

[SOLVED] How can I exclude all "permission denied" messages from "find"?

9 Answered Questions

[SOLVED] Grouping functions (tapply, by, aggregate) and the *apply family

28 Answered Questions

[SOLVED] Is there a better way to do optional function parameters in JavaScript?

Sponsored Content