R "cannot find function" when trying to use packages

Hi! I'm using a PC with Windows 10. I keep running into this error. For example:

Error in diversity(trees, index = "shannon") : **
** could not find function "diversity"

this was when I was doing something very simple with the vegan package.This has also happened multiple times with ggplot2, ex.

Error in ggplot2(data = data, aes(x = MovieLens.Rating)) : **
** could not find function "ggplot2"

Even why I was trying to install rtools, it came up:

Error in install.Rtools(choose_version = TRUE, check = FALSE, GUI = TRUE, : **
** could not find function "install.Rtools"

I know to re-load packages such as vegan and ggplot2 every time I open a new R session, and I have the other pre-requisite packages loaded before I execute my code (eg. permute for vegan).

Also, I tried running this line of code I found while scouring the internet for help

ggplot2 : : ggplot(mtcars, ggplot2 : : aes(wt, mpg)) + ggplot2 : : geom_point()

and it produced a nice plot, so at least ggplot2 was actually loading.

Any ideas? Has anyone run into this before?

I think you need ti be using the library() function to load a given (installed) packages namespace to save you typing explicit names like ggplot2::ggplot() if you did library(ggplot2) you could access ggplot as simply ggplot()

You need to be aware of scope. In case you can't find a function locally, it's in another environment, just look there, like ggplot2::ggplot. It's as easy as that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

That is usually how I load packages, using the packages tab or just typing the code manually. Hasn’t worked for me unfortunately with this error.