Can I check if roxygenize failed? - r

Is it possible to detect if there was a problem when running roxygenize (package roxygen2)?
I want to automate the process documenting, checking and building a package, and would like to stop when documenting goes wrong.
The roxygenize help says the return value is NULL, and I searched stackoverflow without success. Currently, I need to look at the output and search if there was a line starting with "Error".
Any hint appreciated!

When roxygenize finds an error, for example, you included stop("Raise an error") in your code, then roxygenize will return an error.
The other scenario (which is what you are getting at), is that roxygenise is able to finish, but some aspects of the documentation process are incorrect. In this case, these errors are stored as warnings. So one solution is to change warnings to errors.
For example, suppose you had a file containing the line:
#' #XXX
This would cause:
roxygenise("pkg/")
to raise a warning
Warning: XXX is an unknown key in block AllGenerics.R:5
If we changed warnings to errors:
##All warnings are now errors
options(warn=2)
Then
roxygenise("pkg/")
would raise the error:
Error: (converted from warning) XXX is an unknown key in block AllGenerics.R:5
You can then use the standard tryCatch technique for dealing with errors.

Related

R: Error in as.environment(pos) / Error during wrapup: no item called "package:[package]" on the search list

This is similar to this but with a followup question:
I'm preparing my package for submission to CRAN. In one script, it gives the titular error:
Error in as.environment(pos): no item called "package:shapefiles" on the search list
or
Error during wrapup: no item called "package:shapefiles" on the search list
If, like the similar thread linked above, I add
attachNamespace("shapefiles")
Then I no longer get that error, but instead I sometimes get the error or warning:
namespace(shapefiles) was already taken
I believe this happens if the function/script is run more than once. What's so weird about this to me is that
#' #import shapefiles
is in that script's markdown block,
Which means
import(shapefiles)
is in NAMESPACE, and
Imports:
shapefiles (>= 0.7),
is in DESCRIPTION. I have numerous other package dependencies which are imported the same way (correctly, I believe), and none of them produce this error. I don't see why this is behaving differently therefore, and would like to avoid having either issue before submitting to CRAN. The code that calls this package, in my script, is
shapefiles::read.shapefile(savename)
Script is here, for reference.
Thanks in advance for any ideas!
Edit: steps to reproduce:
setwd("/folder")
library(devtools)
install_github("SimonDedman/gbm.auto")
library(gbm.auto)
gbm.basemap(bounds = c(-81.7, -80.3, 24.7, 25.9),
savedir = "/folder")
I've just removed shapefiles:: from the code. The correct output is produced regardless.
Problem resolved itself after commenting out attachNamespace("shapefiles"). No idea why.

Create my own warning message outside a function?

This should be simple based on posts like this but somehow I cannot get it to work. What is wrong with this example?
x<-1
y<-0
if(x>y){warning("careful, one is greater than zero!")}
It works with stop():
if(x>y){stop("careful, one is greater than zero!")}
So either I'm making a simple syntax mistake or warning is not supposed to be used outside of functions?
Your code works fine with me. I'm using R 3.3.2.
I think a possibile solution to your problem is to check if warning messages are enabled in your session.
If you read ?options, you'll notice that between the values returned by the function there is the warn value.
From the reference:
warn:
sets the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two or larger all warnings are turned into errors.
So, if you have a negative value for warn, you won't see warning messages.
You can enable warning messages in the following way:
options(warn=1)
Try changing this and re-run your code.

How do I get pretty error messages in RStudio?

When working in RStudio (version 0.99.482 and 1.0.136), if I source a file and an error occur I just get the error message without any context. If the file is longer than a few lines of code, that informaiton is largely useless. What I want to know when an error occurs is the following:
What function threw the error? Preferably what function I called from my script, not something burried deep inside a package.
On what line of my file did the error occur?
This seems like a very basic thing for a scripting language to do, but yet I am unable to find an easy solution. Yes, I am aware of the traceback() function, but (a) it is a hassle to call it every time there is an error, and (b) the output is massive and not that helpful.

Print list of all error messages after running an R script

Apologies if there is an obvious answer to this question, but I haven't found one.
All I'm looking for is the error equivalent of warnings() - I want to run a script, then get a list of all the errors that occurred when running the script.
Have had a look at traceback() and it seems like it might do what I want - the help file says "The default display is of the stack of the last uncaught error" - but can't figure out how to make it return all uncaught errors rather than just the last one.
My suggestion would be, save the script (e.g. script.R) then run the whole script using source('script.R'). This will stop at every error. If you just want to see if errors exist, this will be a good way to do it.

Functions called in incorrect sequence, or earlier call was unsuccessful

in my application,
I did some trace statements of a file
For example
if(oldFile.parent.toString()!=file.parent.toString())
There are some other file print statements , and it seems to encounter this error stated below.
But the error does not appear every time. For example i ran my application for 20 times, it will encounter this error once.
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at Error$/throwError()
at flash.filesystem::File/resolveComponents()
at flash.filesystem::File/get parent()
may be you are not checking the existence of the file before calling it. Refer this link might be help full.

Resources