Re: [R-pkg-devel] tryCatch defensive programming guidance

2017-03-01 Thread Martin Maechler
> Berry Boessenkool 
> on Wed, 1 Mar 2017 14:52:10 + writes:

> Hi Glenn,


> Better late than never:
> couldn't you simply use try?

> result <- try( log("a") )

> The printing is horrible: people will think an error
> occured (but the function didn't stop!)

> I tend to use it like this (which may be totally
> unintended):


> res <- try(log("a"), silent=TRUE)
> if(inherits(res, "try-error"))
> {
>   message("log failed: ",res,". Now continuing with res=0.")
>  res <- 0
> }

but if you ever looked:  try() is just a wrapper to tryCatch()
and using try(*, silent=TRUE)  is even closer to a pretty simple tryCatch(.)

Historically,  tryCatch() did not exist, but try() did.
So much of "old code" still has try() calls in it.

I consider try() as convenience function for interactive use but
would always use tryCatch() for new code in my packages.


> See here for my version that captures errors/warnings/messages with call 
tracing:

> https://www.rdocumentation.org/packages/berryFunctions/topics/tryStack

I'd recommend you switch to tryCatch(): It is more flexible and
more directly "configurable" than try() -- which also got some historical
flexibility by the "hack" (not functional programming)
of depending on  getOption("show.error.messages")  in the
default case  silent = FALSE  {needed to suppress other
packages' using  try(.) 's printing of error messages as if
*error*s inspite of the fact that they were caught.

 and back to the OP:

I think that's your only small problem that you chose
  error = function(e) print(e)

because that prints "as if" you had an error.

Martin


> Regards,

> Berry


> 
> From: R-package-devel  on behalf 
of Glenn Schultz 
> Sent: Saturday, February 25, 2017 15:50
> To: R Package Development
> Subject: [R-pkg-devel] tryCatch defensive programming guidance

> All,

> I have the following to create a class PriceTypes.  I use try catch on 
the function and it gives me the error

> price <- tryCatch(PriceTypes(price = "100")
> ,error = function(e) print(e)
> ,warning = function(w) print(w))

> 
>> 

> I read the section on tryCatch and withCallingHandlers as well the manual 
but I am still not clear as to how to use tryCatch in the function below I tried
> PriceTypes <- TryCatch(
> function(){}
> ), function(e) print(error)

> but this is obviously wrong as it did not work.  My question can I use 
tryCatch in the function itself or only when I invoke the function.

> Best Regards,
> Glenn

> #' An S4 class representating bond price
> #'
> #' This class is used to create and pass the price types reported to
> #' investors and used in analytics. For example price is often reported as
> #' decimal or fractions 32nds to investors but price basis (price/100) is
> #' used to calculate proceeds and compute metrics like yield, duration, 
and
> #' partial durations.
> #' @slot PriceDecimal A numeric value the price using decimal notation
> #' @slot Price32nds A character the price using 32nds notation
> #' @slot PriceBasis A numeric value price decimal notation in units of 100
> #' @slot PriceDecimalString A character the price using decimal notation
> #' @exportClass PriceTypes
> setClass("PriceTypes",
> representation(
> PriceDecimal = "numeric",
> Price32nds = "character",
> PriceBasis = "numeric",
> PriceDecimalString = "character")
> )

> setGeneric("PriceTypes", function(price = numeric())
> {standardGeneric("PriceTypes")})

> #' A standard generic function get the slot PriceDecimal
> #'
> #' @param object an S4 object
> #' @export PriceDecimal
> setGeneric("PriceDecimal", function(object)
> {standardGeneric("PriceDecimal")})

> #' A standard generic function to set the slot PriceDecimal
> #'
> #' @param object an S4 object
> #' @param value the replacement value of the slot
> #' @export PriceDecimal<-
> setGeneric("PriceDecimal<-", function(object, value)
> {standardGeneric("PriceDecimal<-")})

> #' A standard generic function to get the slot Price32nds
> #'
> #' @param object an S4 object
> #' @export Price32nds
> setGeneric("Price32nds", function(object)
> {standardGeneric("Price32nds")})

> #' A standard generic function to set the slot Price32nds
> #'
> #' @param object an S4 object
> #' @param value the replacement value of the slot
> #' @export Price32nds<-
> setGeneric("Price32nds<-", function(object, value)
> {setGeneric("Price32nds")})

> #' A standard generic to get the slot PriceBasis
> #'
> #' @param object an S4 object
> #' @export PriceBasis
> setGeneric("PriceBasis", 

Re: [R-pkg-devel] .bib file in vignette not found during R CMD check

2017-03-01 Thread Duncan Murdoch

On 01/03/2017 2:17 AM, Henrik Bengtsson wrote:

On Tue, Feb 28, 2017 at 2:47 PM, Duncan Murdoch
 wrote:

On 28/02/2017 5:17 PM, Patrick Schratz wrote:


Thanks Duncan, that was the problem! Although the Rbuildignore entry
pointed to the main dir



No, it contains patterns, not paths, and is matched "case-insensitively
against the file and directory names relative to the top-level package
source directory".  Put ^ at the beginning if you want to refer to the main
dir.

Hadley, reporting what gets omitted might be a good suggestion, but it might
report too much in a package with compiled code (the .o files will generally
be omitted; rgl has 51 of those in src...).


I like this idea.  It could be done such that R CMD build lists the
files omitted based solely on the content of .Rbuildignore, which I
assume is also what Hadley meant.  This way *.o files wouldn't be
listed since they're omitted by the built-in rules of R CMD build
itself.


That makes sense.  Want to submit a patch?

Duncan Murdoch

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel