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.
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?
There are a few things you should check :
install.packages("thePackage")
(this only needs to be done once)require(thePackage)
or library(thePackage)
(this should be done every time you start a new R session)If you're not sure in which package that function is situated, you can do a few things.
help.search("some.function")
or ??some.function
to get an information box that can tell you in which package it is contained.find
and getAnywhere
can also be used to locate functions.findFn
in the sos
package as explained in this answer.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.
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.
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 inutils
. However, I was using 3.3.1 andhasName
wasn't introduced until 3.4.0. As you can't updateutils
as a stand-alone library, R/R Studio said I didn't have any libraries to update.@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
@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
norhttps://stat.ethz.ch/R-manual/R-devel/library/utils/html/hasName.html
say "introduced in R 3.4.0" I ended up figuring it out by browsing through github repos and look at theblame
for utils/R/hasName.R and base/R/match.R@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 ;-)