Issues in R package after CRAN asked to replace \dontrun{} by \donttest{} - r

I submitted a package to CRAN and they asked me to replace \dontrun{} by \donttest{} in the Rd-files and resubmit. I was using \dontrun{} to wrap some examples that are supposed to throw error messages.
After replacing \dontrun{} by \donttest{} I conducted some tests and R CMD check still succeeds but it happens that now both devtools::check() and R CMD check --as-cran fail due to the examples wrapped in \donttest{}:
checking examples with --run-donttest ... ERROR
After some browsing I learned that R 4.0.0 has changed R CMD check --as-cran to run \donttest examples. According to the NEWS of R-devel:
"R CMD check --as-cran now runs \donttest examples (which are run by example()) instead of instructing the tester to do so. This can be temporarily circumvented during development by setting environment variable R_CHECK_DONTTEST_EXAMPLES to a false value."
Since I intend to resubmit the package to CRAN, setting _R_CHECK_DONTTEST_EXAMPLES_ to false locally will not help me.
I also found this recent discussion in a devtools issue where Hadley Wickham states that:
"Generally, now if you don't want to run tests on CRAN \dontrun{} is more likely to work, but using \dontrun{} may cause initial submission to fail."
So now I don't know how to proceed because if I resubmit the package with the required changes I already know it will throw an error in R CMD check --as-cran, and hence it will probably fail CRAN's automatic pretests.
EDIT:
As suggested here I tried if(interactive()){} instead of \dontrun{}. This solution succeeds in R CMD check --as-cran and devtools::check() but I don't think it is the most appropriate way to address this problem since it does not work well with example() (throws an error and does not show the remaining examples). \dontrun{} works better with example() as it prints all the examples but comments out the ones wrapped with \dontrun{}.

If you know that something will throw an error, you can wrap it in try().
## example of failing code
try(stop("Here is an error"))

I don't think the package examples are the right place for "examples that are supposed to throw error messages".
Your problem would be easily solved when you move these 'examples' to testthat unit tests.
There is
expect_error()
expect_warning()
to see if your package throws a warning/error as expected.
If you really want to inform users about what they should avoid to input, maybe you can just add it as a comment to the examples or into the other documentation (details, param)
What you see quite regularly in other package examples is the following:
## Example for working
function(x, abc = "5)
## This would give an error because
# function(x, abc = "falsch")
## Working example 2
function(x)
x <- x+y

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 ...

How to get rid of a size warning in R for package development

I am working on submitting an R package and when I run
devtools::check()
I get the following warning:
W checking sizes of PDF files under ‘inst/doc’ (1.6s)
‘gs+qpdf’ made some significant size reductions:
compacted ‘Vignette1.pdf’ from 544Kb to 256Kb
compacted ‘Vignette2.pdf’ from 328Kb to 69Kb
consider running tools::compactPDF(gs_quality = "ebook") on these files
I understand that the size of the PDFs are the issue, and that they need to be resized, but can anyone explain to me where I need to place tools::compactPDF(gs_quality = "ebook") in my code so that when the vignettes are created this is not an issue. You cannot submit an R package that throws a warning so I need to come up with a solution to fix this.
If you do R CMD build --help you will see the two lines
--compact-vignettes= try to compact PDF files under inst/doc:
"no" (default), "qpdf", "gs", "gs+qpdf", "both"
Per recent discussion on the mailing lists, you may want arguments gs+qpdf or both. You may want to experiment with both. Use of the option should render a package that does not trigger the NOTE.
And I presume there is a way to pass that onto devtools as well but I am not familiar with its functions so I can't give you a direct pointer.
As of devtools 2.4.5
devtools::check(build_args = "--compact-vignettes=gs+qpdf")
is the correct parameterization. But note that for building, the corresponding call is now
devtools::build(args = "--compact-vignettes=gs+qpdf")
Note that in neither case do you quote the argument passed down to R CMD BUILD's --compact-vignettes

R package check() warning: A complete check needs the 'checkbashisms' script

Today I have tried to prepare a new CRAN update for a package and have come across a warning, while performing devtools::check().
Since this warning also occurs without my changes I made to the package, I am assuming this is a new check. Unfortunately I don't really get what I am supposed to do.
This is the error message:
A complete check needs the 'checkbashisms' script.
See section ‘Configure and cleanup’ in the ‘Writing R Extensions’
manual.
Of course I took a look at the section ‘Configure and cleanup’ as proposed in the warning. But still, I am not sure what to do exactly. I got, there is a checkbashisms Perl script I can download somewhere on the internet (but this alone won't make this message disappear). How shall I integrate this, that the message actually disappears.
Any hints or comments would be extremely helpful to me.
Here is a link to the repo of the package in case you want to clone it to reproduce the error.

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.

r devtools::test fails but all contexts are OK

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...}).

Resources