auto_test gives reporter error in testthat - r

I'm trying to force myself to do more unit testing on some data analysis projects where I don't really want to develop a package. So, I've been playing with the testthat R package. I have a code folder and inside it is a src and a test folder. In the src folder I have the file add.R that has this function:
add <- function(x,y){
x+y
}
In the test folder I have the file test-add.R that contains this:
library(testthat)
test_that("adding numbers", {
expect_equal(add(2,3), 5)
expect_equal(add(5,5), 10)
})
The following works fine...
> source('code/src/add.R')
> test_file('code/test/test-add.R')
But I'd like to be able to use the auto_test function since as the project grows source/test_file will get tedious. But when I try auto_test I get this...
> auto_test('code/src', 'code/test')
Error in find_reporter(reporter) : attempt to apply non-function
I'm sure I'm missing something simple, but what?
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] testthat_0.11.0.9000 knitr_1.12.3
loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 R6_2.1.2 tools_3.2.3
[4] withr_1.0.1 memoise_1.0.0 crayon_1.3.1
[7] digest_0.6.9 devtools_1.10.0.9000

It seems that this is a problem of testthat version 0.11.0.9000. If I install this version from github, I get the same error message as you do:
auto_test('code/src', 'code/test')
## Error in find_reporter(reporter) : attempt to apply non-function
But with version 0.11.0 that can be installed from CRAN, your example runs fine:
auto_test('code/src', 'code/test')
## ..
## DONE

Related

Custom R package loads, shows help but does not provide functions

My custom package created with devtools installs, loads, displays help files for the functions, but does not provide the functions. This solution does not seem to be relevant, as my package is installed in .libPaths().
# from the parent directory of the created package
install.packages("mypkg", repos = NULL, type = "source")
# ...
# * DONE (mypkg)
require(mypkg)
# Loading required package: mypkg
?my.fun # displays the function help documentation correctly
my.fun()
Error: could not find function "my.fun"
What causes this behaviour and how to fix it?
sessionInfo()
#R version 3.3.0 (2016-05-03)
#Platform: x86_64-apple-darwin13.4.0 (64-bit)
#Running under: OS X 10.13.1 (unknown)
#locale:
#[1] cs_CZ.UTF-8/cs_CZ.UTF-8/cs_CZ.UTF-8/C/cs_CZ.UTF-8/cs_CZ.UTF-8
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#other attached packages:
#[1] mypkg_0.1 devtools_1.13.4
#loaded via a namespace (and not attached):
#[1] tools_3.3.0 withr_2.1.0 memoise_1.0.0 git2r_0.19.0 digest_0.6.9
Did you export the function? To check if it's in the package run:
mypkg:::my.fun()

New bug in glmmADMB? VarCorr can't locate rdig

I think this is a newly introduced bug. At the very least, it bugged previously working code.
library(glmmADMB)
epil2$subject <- factor(epil2$subject)
fm <- glmmadmb(y~Base*trt+Age+Visit+(Visit|subject),
data=epil2, family="nbinom")
VarCorr(fm)
Produces the following error:
Error in glmmADMB::VarCorr(fm) :
could not find symbol "rdig" in environment of the generic function
VarCorr from nlme still works fine:
library(nlme)
nlme::VarCorr(fm) ##WORKS
glmmADMB::VarCorr(fm) ##Still doesn't work.
Here is my systemInfo if that helps:
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] nlme_3.1-125 glmmADMB_0.8.1 MASS_7.3-45
loaded via a namespace (and not attached):
[1] Matrix_1.2-3 plyr_1.8.3 magrittr_1.5 tools_3.2.3
[5] coda_0.18-1 Rcpp_0.12.3 stringi_1.0-1 grid_3.2.3
[9] stringr_1.0.0 R2admb_0.7.13 lattice_0.20-33
I found a solution to my problem. I used the installation code provided here:
http://glmmadmb.r-forge.r-project.org/ . I am not sure if it was necessary but before running the below code I navigated to my ~R/R-3.2.3/library folder and deleted the glmmADMB folder.
Then I ran the code below.
install.packages("R2admb")
install.packages("glmmADMB",
repos=c("http://glmmadmb.r-forge.r-project.org/repos",
getOption("repos")),
type="source")
This updated my glmmADMB to version 0.8.3.3 which corrected the error (by turning it into a warning).

Access data base import to R-installation of mdb tools on Mac

The plan ist to import an Access database into R to filter out what I need to make statistical analysis.
A colleague suggested me the following code:
library(Hmisc)
d <- mdb.get('140410_db_vegBY1.mdb')
#(data under #https://www.dropbox.com/sh/ra588lailweynrq/AACRVqMKWtd9Zz7fa94O9CAUa)
# ERROR: sh: mdb-tables: command not found
# Fehler in system(paste("mdb-tables -1", file), intern = TRUE) :
# Fehler bei der Ausführung des Kommandos
After my search I started to install mdb tools on my mac OS X 10.9.2 following this link:
http://automatthew.wordpress.com/2008/06/23/how-to-compile-mdbtools-on-mac-os-x-10-4-and-10-5/#comment-124
But it didn't work but the thread seems to be inactive.
Can somebody help me?
The following steps worked for me.
Install the Hmisc package for R.
Install mdbtools version 0.7.1 by homebrew: brew install mdbtools.
Use mdb.get() function in the Hmisc package to read .mdb files into R:
library(Hmisc)
df <- mdb.get('file.mdb')
Here is my setup and package versions:
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] Hmisc_3.17-0 ggplot2_1.0.1 Formula_1.2-1 survival_2.38-3
[5] lattice_0.20-33
I should mention that I first tried to install mdbtools from source to no avail. I suspect that it was because some dependencies were not installed. See https://github.com/brianb/mdbtools for more information on the dependencies.

diff() fails on zoo object

While working on a new project in R, I wrote the following code:
sp500 <- get.hist.quote("^GSPC",start=(today <- Sys.Date())-735,quote="Cl")
lsp500 <- log(sp500)
rlsp500 <- diff(lsp500)
The problem is the diff() function, it produces the following error:
Error in MATCH(x, x) : could not find function "MATCH"
All other code executes without problems. I'm using RStudio and R version 2.15.2 (2012-10-26) -- "Trick or Treat" on Mac OSX 10.8.2.
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tseries_0.10-30
loaded via a namespace (and not attached):
[1] grid_2.15.2 lattice_0.20-10 quadprog_1.5-4 tools_2.15.2 zoo_1.7-9
What am I missing?
tseries::get.hist.quote returns a zoo object by default, but the tseries package doesn't attach zoo, so zoo::MATCH isn't found. I assume zoo::MATCH is used in diff.zoo or one of the functions called by it.
Attaching zoo (via library(zoo)) will fix the problem.

Error: "could not find function.." when using .First()

I have a script in .First(). I saved it to .RData and put it in a folder to which I point in a Windows shortcut of Rterm.exe 64-bit (like described here). When I open the shortcut I get the error "could not find function..", even for base functions..
like so:
.First <- function() {
plot(rnorm(100))
}
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.15.0
Files with .RData extenstions are often assigned as data rather than source files. The problem however is that the .First scripts can only use functions in the base package unlees the other packages are first loaded.
.First <- function() { require(graphics)
plot(rnorm(100))
}
?Startup
Personally, I would name this with a .R extension.

Resources