R load a method from a package - r

I've got a large package. I'd like just one method (and any dependencies it has) to be loaded without having to load the whole package. Is this possible?

You could call upon the specific function in R with the following operator and form: (package name)::(function). An example would be calling upon the round_any() function in the package plyr such as
> plyr::round_any(134, accuracy = 10)
[1] 130

Related

How to accurately use the epi.kappa() function in R?

I am attempting to perform a kappa statistic test on 2 indices I created. I've found that there are multiple packages in R that have functions for this and am trying to compare two functions: the epi.kappa() function in the epiR package and the cohen.kappa() function from the psych package.
I was able to successfully use the cohen.kappa() function with my data however, I continue to get errors when using the epi.kappa() function. My code is as follows
library(epiR)
kap.dat = matrix(c(275,78,305,154),nrow=2,byrow=TRUE)
colnames(kap.dat) = c("I1-0","I1-1")
rownames(kap.dat) = c("I2-0","I2-1")
epi.kappa(kap.dat,method="cohen",alternative= "two.sided",conf.level=0.95)
The error I get is:
Error in epi.kappa(kap.dat,method="cohen",alternative="two.sided", :
object `pO.p` not found
Check that you've got the latest version of epiR installed and update if necessary. Latest version on CRAN is 2.0.50. Type help(epi.about) once package loaded --- version of package will be shown at the bottom of the page.

R testthat and devtools: why does a minimal unit test break my package?

I'm working on an R package for sparse matrix handling. It kinda works; here's a minimal example to set the stage for my question.
devtools::install_github("ekernf01/MatrixLazyEval", ref = "eef5593ad")
library(Matrix)
library(MatrixLazyEval)
data(CAex)
M = rbind(CAex, CAex)
M = matrix(stats::rnorm(prod(dim(M))), nrow = nrow(M))
M_lazy = AsLazyMatrix( M )
svd_lazy = RandomSVDLazyMatrix(M_lazy)
But, when I run even a minimal unit test, it breaks the package permanently (I have to restart my R session or reinstall the package). The immediate cause is that R can't find some S4 methods from packages I depend on (e.g. for matrix transpose t or colSums from the Matrix package). I run the unit test like this:
devtools::test(filter = "minimal")
svd_lazy = RandomSVDLazyMatrix(M_lazy)
Here's the contents of the test files.
> cat tests/testthat.R
library(testthat)
testthat::test_check("MatrixLazyEval")
> cat tests/testthat/testthat_minimal.R
context("minimal")
Why does this happen? Maybe this is naive, but the unit test shouldn't even do anything.
Edit
Possibly related:
r - data.table and testthat package
https://github.com/r-lib/devtools/issues/192
R data.table breaks in exported functions
You need to import all the generics you're using in your package namespace:
#' #importFrom Matrix t tcrossprod colSums rowMeans
NULL
This will fix the issue that you're observing and you'll be able the tests multiple times in the same session.
Also this will allow other packages that import Matrix::t to consistently use your custom methods. Currently, since you're calling setMethod() in your package, you're creating a new t() generic local to your namespace whenever Matrix is not attached to the search path at load-time (this is why it worked the first time you ran the tests). This prevents other packages using Matrix::t() to access your methods. Importing Matrix::t() explicitly will fix this because you'll never create a local generic for t().

Why this simple test with data.table fails? How to fix it? [duplicate]

I am trying to use the data.table package inside my own package. MWE is as follows:
I create a function, test.fun, that simply creates a small data.table object, and then sums the "Val" column grouping by the "A" column. The code is
test.fun<-function ()
{
library(data.table)
testdata<-data.table(A=rep(seq(1,5), 5), Val=rnorm(25))
setkey(testdata, A)
res<-testdata[,{list(Ct=length(Val),Total=sum(Val),Avg=mean(Val))},"A"]
return(res)
}
When I create this function in a regular R session, and then run the function, it works as expected.
> res<-test.fun()
data.table 1.8.0 For help type: help("data.table")
> res
A Ct Total Avg
[1,] 1 5 -0.5326444 -0.1065289
[2,] 2 5 -4.0832062 -0.8166412
[3,] 3 5 0.9458251 0.1891650
[4,] 4 5 2.0474791 0.4094958
[5,] 5 5 2.3609443 0.4721889
When I put this function into a package, install the package, load the package, and then run the function, I get an error message.
> library(testpackage)
> res<-test.fun()
data.table 1.8.0 For help type: help("data.table")
Error in `[.data.frame`(x, i, j) : object 'Val' not found
Can anybody explain to me why this is happening and what I can do to fix it. Any help is very much appreciated.
Andrie's guess is right, +1. There is a FAQ on it (see vignette("datatable-faq")), as well as a new vignette on importing data.table:
FAQ 6.9: I have created a package that depends on data.table. How do I
ensure my package is data.table-aware so that inheritance from
data.frame works?
Either i) include data.table in the Depends: field of your DESCRIPTION file, or ii) include data.table in the Imports: field of your DESCRIPTION file AND import(data.table) in your NAMESPACE file.
Further background ... at the top of [.data.table (and other data.table functions), you'll see a switch depending on the result of a call to cedta(). This stands for Calling Environment Data Table Aware. Typing data.table:::cedta reveals how it's done. It relies on the calling package having a namespace, and, that namespace Import'ing or Depend'ing on data.table. This is how data.table can be passed to non-data.table-aware packages (such as functions in base) and those packages can use absolutely standard [.data.frame syntax on the data.table, blissfully unaware that the data.frame is() a data.table, too.
This is also why data.table inheritance didn't used to be compatible with namespaceless packages, and why upon user request we had to ask authors of such packages to add a namespace to their package to be compatible. Happily, now that R adds a default namespace for packages missing one (from v2.14.0), that problem has gone away :
CHANGES IN R VERSION 2.14.0
* All packages must have a namespace, and one is created on installation if not supplied in the sources.
Here is the complete recipe:
Add data.table to Imports in your DESCRIPTION file.
Add #import data.table to your respective .R file (i.e., the .R file that houses your function that's throwing the error Error in [.data.frame(x, i, j) : object 'Val' not found).
Type library(devtools) and set your working directory to point at the main directory of your R package.
Type document(). This will ensure that your NAMESPACE file includes a import(data.table) line.
Type build()
Type install()
For a nice primer on what build() and install() do, see: http://kbroman.org/pkg_primer/.
Then, once you close your R session and login next time, you can immediately jump right in with:
Type library("my_R_package")
Type the name of your function that's housed in the .R file mentioned above.
Enjoy! You should no longer receive the dreaded Error in [.data.frame(x, i, j) : object 'Val' not found

Are there known compatibility issues with R package mgcv? Are there general rules for compatibility?

I use R version 2.15.1 (2012-06-22) and mgcv version 1.7-22
I load the following set of packages in R:
library(sqldf)
library(timeDate)
library(forecast)
library(xts)
library(tseries)
library(MASS)
library(mgcv)
It happens that I can not run a simple model (I omit the code). Even the sample code taken from the help pages:
dat = gamSim(1,n=400,dist="normal",scale=2)
b = gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
gives an error:
Error in qr.qty(qrc, sm$S[[l]]) :
NA/NaN/Inf in foreign function call (arg 5)
In addition: Warning message:
In smoothCon(split$smooth.spec[[i]], data, knots, absorb.cons, scale.penalty = scale.penalty, :
number of items to replace is not a multiple of replacement length
Note that everything works fine, if I just load the package mgcv and then use the sample code right away. It also works if I just load all the packages and run the sample code. It just does not work if I
load all packages
do some file reading, sqldf statements, ts operations and some models from package forecast.
if I then apply GAM, it does not work anymore.
Apparently the variable definitions in the general environment mess up the functioning of the package.
Are there any known issues? Are there general rules that I have to obey if I load various packages? Can I write code that "disturbed" the package mgcv?
# Richard there are 2 GAM related packages: gam and mgcv. Loading both libraries at the same time usually causes a conflict.
Loading mgcv as the first package solved my problem ... strange but true.

Passing an entire package to a snow cluster

I'm trying to parallelize (using snow::parLapply) some code that depends on a package (ie, a package other than snow). Objects referenced in the function called by parLapply must be explicitly passed to the cluster using clusterExport. Is there any way to pass an entire package to the cluster rather than having to explicitly name every function (including a package's internal functions called by user functions!) in clusterExport?
Install the package on all nodes, and have your code call library(thePackageYouUse) on all nodes via one the available commands, egg something like
clusterApply(cl, library(thePackageYouUse))
I think the parallel package which comes with recent R releases has examples -- see for example here from help(clusterApply) where the boot package is loaded everywhere:
## A bootstrapping example, which can be done in many ways:
clusterEvalQ(cl, {
## set up each worker. Could also use clusterExport()
library(boot)
cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v)
cd4.mle <- list(m = colMeans(cd4), v = var(cd4))
NULL
})

Resources