Re: [R-pkg-devel] modification of warning messages

2016-06-22 Thread Dirk Eddelbuettel

On 22 June 2016 at 18:29, Hadley Wickham wrote:
| Try warningcall(R_NilValue, "message")

Nice one:

  R> cppFunction("void foo(std::string txt) { Rf_warningcall(R_NilValue, 
txt.c_str()); }")
  R> foo("magic!")
  Warning message:
  magic! 
  R> 

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

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


Re: [R-pkg-devel] modification of warning messages

2016-06-22 Thread Hadley Wickham
Try warningcall(R_NilValue, "message")

Hadley

On Wednesday, June 22, 2016, Aaron King  wrote:

> I am in the midst of trying to improve error and warning messages in a
> complex package.  I find myself wanting to modify R's default behavior when
> printing warnings.  Specifically, it frequently happens for me that the
> call that is printed with the warning message is not informative.  When the
> warning arises in R code, I have the ability to suppress the printing of
> the call by setting 'call.=FALSE'.  However, when the warning arises in C
> code (via the 'warning()' function), I have no such option.
>
> 'tryCatch' and 'withCallingHandlers' are of no use (or at least, if they
> are, I haven't figured out how).
>
> Consider the following simple example:
>
> Contents of file 'example.c':
> --
> #include 
>
> void example (int *x) {
>   if (*x>0)
> error("ouch");
>   else if (*x<0) {
> warning("duck!");
> *x = 22;
>   } else {
> *x = 11;
>   }
> }
> --
>
> Compile and load the C code:
> --
> system2(R.home("bin/R"),args=c("CMD","SHLIB","example.c"))
> dyn.load(paste0("example",.Platform$dynlib.ext))
> --
>
> The function 'f' calls 'example' as defined above.  On an error, it
> modifies the error message using 'tryCatch'.
> --
> f <- function (x) {
> y <- tryCatch(
> .C("example",x=as.integer(x))$x,
> error = function (e) {
> stop("in f: ",conditionMessage(e),call.=FALSE)
> }
> )
> y
> }
> --
>
> Get an error:
>
> > f(1)
> Error: in f: ouch!
>
> Get a warning:
>
> > f(-1)
> [1] 22
> Warning message:
> In doTryCatch(return(expr), name, parentenv, handler) : duck!
>
> It's the fact that 'doTryCatch(return(expr), name, parentenv, handler)' is
> meaningless to the user that bothers me.  How can I modify the warning?
>
> Thanks in advance for any insight.
>
> --
> Professor Aaron A. King
> Ecology & Evolutionary Biology
> Mathematics
> Center for the Study of Complex Systems
> University of Michigan
> GPG Public Key: 0x15780975
>
> [[alternative HTML version deleted]]
>
> __
> R-package-devel@r-project.org  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>


-- 
http://hadley.nz

[[alternative HTML version deleted]]

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


[R-pkg-devel] modification of warning messages

2016-06-22 Thread Aaron King
I am in the midst of trying to improve error and warning messages in a
complex package.  I find myself wanting to modify R's default behavior when
printing warnings.  Specifically, it frequently happens for me that the
call that is printed with the warning message is not informative.  When the
warning arises in R code, I have the ability to suppress the printing of
the call by setting 'call.=FALSE'.  However, when the warning arises in C
code (via the 'warning()' function), I have no such option.

'tryCatch' and 'withCallingHandlers' are of no use (or at least, if they
are, I haven't figured out how).

Consider the following simple example:

Contents of file 'example.c':
--
#include 

void example (int *x) {
  if (*x>0)
error("ouch");
  else if (*x<0) {
warning("duck!");
*x = 22;
  } else {
*x = 11;
  }
}
--

Compile and load the C code:
--
system2(R.home("bin/R"),args=c("CMD","SHLIB","example.c"))
dyn.load(paste0("example",.Platform$dynlib.ext))
--

The function 'f' calls 'example' as defined above.  On an error, it
modifies the error message using 'tryCatch'.
--
f <- function (x) {
y <- tryCatch(
.C("example",x=as.integer(x))$x,
error = function (e) {
stop("in f: ",conditionMessage(e),call.=FALSE)
}
)
y
}
--

Get an error:

> f(1)
Error: in f: ouch!

Get a warning:

> f(-1)
[1] 22
Warning message:
In doTryCatch(return(expr), name, parentenv, handler) : duck!

It's the fact that 'doTryCatch(return(expr), name, parentenv, handler)' is
meaningless to the user that bothers me.  How can I modify the warning?

Thanks in advance for any insight.

-- 
Professor Aaron A. King
Ecology & Evolutionary Biology
Mathematics
Center for the Study of Complex Systems
University of Michigan
GPG Public Key: 0x15780975

[[alternative HTML version deleted]]

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