R cannot find specific function in a package - r

I'm using the randomForest package (v 4.6-7) in R (v 2.15.3) and can easily use the function randomForest to create a model. However, when I try to predict on my test set, the predict.randomForest function cannot be found. I've also tried plotting with plot.randomForest only to get the same error, "could not find function."
I've already tried reinstalling the package (figuring maybe it was out of date) and made sure the spelling is absolutely correct. I cannot figure out what's causing this error, any ideas?

It appears that the functions of interest are not exported from the package.
If you use ls(package:randomForest) you'll get a list of the exported functions.
If you want to see all the functions available the use: ls(getNamespace("randomForest"), all.names=TRUE). Thanks #Joshua.
You'll see the functions you want there.
In order to refer to one of them explicitly, use: randomForest:::predict.randomForest()
or just make a object which inherits the class 'randomForest' and call predict() on it directly.

Related

unrecognized function Nn in R

I am learning R package SimInf to simulate data-driven stochastic epidemiological models. As I was reading the documentation I came across an unrecognized funcion Nn when defining a function for epicurves. Specifically, this line:
j <- sample(seq_len(Nn(model)), 1)
Values of model are integers. My guess is that Nn selects non-negative values, however my R does not recognize this function. From documentation it does not look like they pre-defined Nn either. Can someone please tell if they know what "Nn" is for? Thank you.
A way to go is always taking the package-name and triple-":" it, such that you can find nearly all functions inside the package. Maybe you are familiar with namespacing a function via packageName::functionFrompackageTocall. The packageName::: shows (nearly) all functions defined in this package. If you do this in R-Studio with SimInf:: and SimInf:::, you will see that the latter gives much more functions. But you can only find the functions SimInf:::Nd and SimInf:::Nc, not the Nn-function. Hence you will have to go to the github-sources of the package, in this case https://github.com/stewid/SimInf .Then search for Nn the whole repository. You will see that it seems like it is always an int, but this doesn't help you since you want to get ii as a function, not as a variable. Scrolling further down in the search-results, you will find the NEWS.md-file which mentions The 'Nn' function to determine the number of nodes in a model has been replaced with the S4 method 'n_nodes'. in the https://github.com/stewid/SimInf/blob/fd7eb4a29b82a4a97f64b528bb0e78e5474aa8a5/NEWS.md file under SimInf 8.0.0 (2020-09-13). Hence having a current version of SimInf installed, it shouldn't use the method Nn anymore. If you use it in your code, replace it by n_nodes. If you find it in current package code, you can email the package-maintainer that you found a bug in his code.
TLDR: Nn is an outdated version of n_nodes

can't use dea function of Benchmarking package in R

I'm using Benchmarking package to run a model of data envelopment analysis using dea function on it. In addition, I'm using nonparaeff package, for partial frontier, which has a function called dea too. This similarity in name function produces this "error":
Attaching package: ‘nonparaeff’
The following object is masked from ‘package:Benchmarking’:
So, is it possible to use both packages at the same time or not?
Yes, you can use both packages.
Attaching package: ‘nonparaeff’ The following object is masked from ‘package:Benchmarking’:
This error message basically means, that the package you loaded last (in this case nonparaeff) masks functions from Benchmarking.
The first thing to mention is:
All other functions without naming conflicts will work as expected.
The second thing to take care of is:
When you just call dea() in your code now, you will call the function from the package last loaded. (because the other package's dea() function is masked by this function)
But, to be sure to call the right function you can just write the functions calls like this : Benchmarking::dea() and nonparaeff::dea().
By specifying packagename::funtionname() you make sure to call exactly the function from the specified package. It is not as convenient as calling just dea(), but in case of functions with the same name, I would actually use this for both functions. (to prevent mistakes)
I myself quite often call my functions with packagename::funtionname() - it avoids errors and another positive is, you directly know by looking at the code, from which package a function is.

Function tukey.add.test (asbio) with other package

usually I use the tukey.add.test from the asbio package in order to check interaction between treatmest and block in ANOVA RCBD, the package is now archived so I can no longer use this function or anything from the package.
You guys know another function to check this interaction? Any suggestion?
Done, the agricolae::nonadditivity function does the same test with the exactly same p.values.

Proper way to initialize dependent package in R, example using ggmap

I'm developing a package that uses ggmap as a dependency.
ggmap: https://github.com/dkahle/ggmap
Within my package, I'm calling ggmap function using the recommended approach of including ggmap in the Imports section of the Description file, and calling functions using the :: operator (e.g. ggmap::get_map()). My issue is that ggmap assumes that some options are set upon initialization in .onLoad().
https://github.com/dkahle/ggmap/blob/master/R/attach.R
I believe that, since I'm not calling library() or require(), .onAttach() never gets called, and thus these options never get set. I can't call .onAttach() within my package, because it is not exported.
What is the best practice for initializing a dependent package?
This seems like a general problem in R package development, but I can't find the answer anywhere. And my apologies, this doesn't seem like the kind of question that can have a reproducible example.

How to retrieve R function script from a specific package?

I realize there are generic functions like plot, predict for a list of packages. I am wondering how could I get the R script of these generic functions for a specific package, like the lme4::predict. I tried lme4::predict, but it comes with error:
> lme4::predict
Error: 'predict' is not an exported object from 'namespace:lme4'
Since you state that my suggestion above was helpful I will tell you my process. I used my own co-authored package called pacman. This package was developed because we had a hard time remembering all the obscurely named functions to get information on and work with add on packages.
I used this to figure out what you wanted:
library(pacman)
p_funs(lme4, all=TRUE)
I set all = TRUE as predict is a method for a specific class (like print, summary and plot). Generally, these methods are not exported so p_funs won't return them unless you set all = TRUE. Then I scrolled down to the p section and found only a single predict method: predict.merMod
Next I realized it wasn't exported so :: won't show me the stuff and extra colon power is needed, hence: lme4:::predict.merMod
As pointed out by David and rawr above, some functions can be suborn little snippets (methods etc.), thus methods and getAnywhere are helpful.
Here is an example of that:
library(tm)
dissimilarity #The good stuff is hid
methods(dissimilarity) #I want the good stuff
getAnywhere("dissimilarity.DocumentTermMatrix")
Small end note
And of course you don't need pacman to look at functions for packages, it's what I use and helpful but it just wraps base R things. Use THE SOURCE to figure out exactly what.

Resources