r devtools::test fails but all contexts are OK - r

I am developing a R package using devtools and the associate testthat functionality. When I use devtools::test(), the console lists all the tested contexts (about 20). All of which are executed correctly with OK in 267 cases and 2 Warnings. However, the == Results =================== line summarizes as there being one failed test. Is this an arithmetic mistake by testthat or where did I go wrong?
I know this is not very specific, let alone reproducible. Help for narrowing it down is welcome.

The cause for this error was a script in the testthat folder which contained some failing code which was not wrapped in a test_that("context", {...code...}).

Related

Cannot not find "calc.RL.0" function within "mixstock" package

I am trying to run through a mixed stock analysis in RStudio based on the walkthrough provided by Bolker (https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=043730a02b148396ebd54b2f62e8f6364714b1b2), using the 'mixstock' package and the example 'lahanas98raw' dataset within. However, I am receiving a warning that the 'calc.RL.0' function cannot be found whilst trying to carry out Raftery and Lewis convergence diagnostics (p.14 of Bolker's walkthrough). I am wondering whether one of the packages has not installed properly (i.e., either 'mixstock' or 'coda'), or whether there is another package I can use to run this diagnostic instead.
When I initially tried to install the 'mixstock' package, the following warning came up:
'Warning in install.packages : package ‘mixstock’ is not available for this version of R.'
I tried installing the version of R (2.14.1) described as the 'current version of R' on page 2 of Bolker's walkthrough, but when I then tried to switch to this version of R in RStudio via the options menu, it says that this version of R is not compatible with RStudio. To work around this, I downloaded the 'mixstock' package (version 0.9.5.1) from the CRAN archive (https://cran.r-project.org/src/contrib/Archive/mixstock/) and uploaded it to RStudio this way instead.
This seemed to have worked, as I successfully ran through a significant amount of the code, but a new error arose when I tried to run Raftery and Lewis convergence diagnostics (p.14). When I try to run calc.RL.0(mydata), I receive the following error:
'Error in calc.RL.0(mydata) : could not find function "calc.RL.0"'
However, when I click on 'mixstock' in the package window, everything seems fine and the 'calc.RL.0' function appears, alongside several other 'calc' functions (e.g. 'calc.GR', 'calc.mult.GR', 'calc.mult.RL') that can all be found and run fine. The 'calc.RL.0' function relies on the 'raftery.diag' function within the 'coda' package, so I have also made sure that is installed and called. I have tried a bunch of other methods but nothing seems to be working.
Here is some of my code leading up to the warning message:
## Calculate confidence intervals - i.e., bootstrapping - and plot them
mydata.umlboot = genboot(mydata,"uml")
confint(mydata.umlboot)
plot(mydata.umlboot, ylim=c(0,1))
## Carry out Markov Chain Monte Carlo (MCMC) estimations and plot them
mydata.mcmc = tmcmc(mydata)
mydata.mcmc
confint(mydata.mcmc)
plot(mydata.mcmc, ylim=c(0,1))
## Check that the Markov chains have converged = run Raftery and Lewis diagnostics
library(mixstock)
library(coda)
calc.RL.0(mydata)
'Error in calc.RL.0(mydata) : could not find function "calc.RL.0"'
Could this be something to do with the way the 'mixstock' package was initially installed, or is it likely to be another issue? Is there another way to run Raftery and Lewis diagnostics and still get the outputs I need (diagnostics for the last chain evaluated; the history of how long each suggested chain was)? Any help would be much appreciated - thanks in advance!
The most reliable way to install mixstock, if you have development tools installed on your computer (compilers etc.), is remotes::install_github("bbolker/mixstock") (I don't think I've changed anything/fixed any bugs since the archived version, but if I did the changes would be reflected on GitHub.)
It looks like I forgot to export that function, so
mixstock:::calc.RL.0(mydata)
should work (this is something I can/should fix). Note that the Gelman-Rubin diagnostic (calc.GR(), which is properly exported) is more reliable than Raftery-Lewis anyway ...

Using reticulate with targets

I'm having this weird issue where my target, which interfaces a slightly customized python module (installed with pip install --editable) through reticulate, gives different results when it's being called from an interactive session in R from when targets is being started from the command line directly, even when I make sure the other argument(s) to tar_make are identical (callr_function = NULL, which I use for interactive debugging). The function is deterministic and should be returning the exact same result but isn't.
It's tricky to provide a reproducible example but if truly necessary I'll invest the required time in it. I'd like to get tips on how to debug this and identify the exact issue. I already safeguarded against potential pointer issues; the python object is not getting passed around between different targets/environments (anymore), rather it's immediately used to compute the result of interest. I also checked that the same python version is being used by printing the result of reticulate::pyconfig() to screen. I also verified both approaches are using the same version of the customized module.
Thanks in advance..!

combination of \dontrun and \donttest?

I have bits of code that I want to show in the examples of a package but neither run (when example(my_fun) is run) nor test (when R CMD check is run) because they're slow enough to annoy users who might unthinkingly run them, and definitely slow enough to annoy the CRAN maintainers.
Writing R Extensions says
You can use \dontrun{} for text that should only be shown, but not run ...
and
Finally, there is \donttest, used (at the beginning of a separate line) to mark code that should be run by example() but not by R CMD check.
Should I nest these, i.e.
\donttest
\dontrun{first slow example ...}
\dontrun{second slow example ...}
? That technically seems to go against the wording in WRE (i.e. it says that \donttest code should be run by example() ...) ?
I could just include them in the examples in a commented-out form or using if (FALSE) { ... } if it came to it ... but that seems ugly.
\dontrun subsumes \donttest: code that is marked with the former will neither be run by example(), nor by R CMD check. I know this because my packages for talking to Azure use \dontrun liberally, for examples that assume you have an Azure account.

Determining if there are unused packages in an R script [duplicate]

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.
Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.
Update 2020-04-13
I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.
I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:
Functions matched from package data.table: data.table
**No exported functions matched from iotools**
Have you considered using packrat?
packrat::clean() would remove unused packages, for example.
I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.
My approach always is to close my R script or IDE (i.e. RStudio) and then start it again.
After this I run my function without loading any dependecies/packages beforehand.
This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

How can I tell which packages I am not using in my R script?

As my code evolves from version to version, I'm aware that there are some packages for which I've found better/more appropriate packages for the task at hand or whose purpose was limited to a section of code which I've now phased out.
Is there any easy way to tell which of the loaded packages are actually used in a given script? My header is beginning to get cluttered.
Update 2020-04-13
I've now updated the referenced function to use the abstract syntax tree (AST) instead of using regular expressions as before. This is a much more robust way of approaching the problem (it's still not completely ironclad). This is available from version 0.2.0 of funchir, now on CRAN.
I've just got around to writing a quick-and-dirty function to handle this which I call stale_package_check, and I've added it to my package (funchir).
e.g., if we save the following script as test.R:
library(data.table)
library(iotools)
DT = data.table(a = 1:3)
Then (from the directory with that script) run funchir::stale_package_check('test.R'), we'll get:
Functions matched from package data.table: data.table
**No exported functions matched from iotools**
Have you considered using packrat?
packrat::clean() would remove unused packages, for example.
I've written a command-line script to accomplish this task. You can find it in this Github gist. I'm sure there are edge cases that it misses, but it works pretty well, on both R scripts and Rmd files.
My approach always is to close my R script or IDE (i.e. RStudio) and then start it again.
After this I run my function without loading any dependecies/packages beforehand.
This should result in various warning and error messages telling you which functions couldn't be found and executed. This again will give you hints on what packages are necessary to load beforehand and which one you can leave out.

Resources