[Rd] Could generic functions check different S3 methods for an object when one of them produces an error?

2019-06-18 Thread IAGO GINÉ VÁZQUEZ
Hi,


Let's say one has an object with multiple classes, and a generic function to 
apply to it has associated S3 methods for more than one of those classes. 
Further, the method it chooses (I do not know how; some order in the class 
vector?) is not the suitable one and it produces an error. Would there be some 
way to make the generic function to choice the correct method, or in case that 
for any method taken it produces an error, to try another one.


For example (commented in detail 
here):


# object with multiple classes: the output of function `geepack::geeglm`. The 
output of `class(object)`:

```

[1] "geeglm" "gee" "glm" "lm"

```

The generic function: `stats::confint`.

The S3 method chosen: `confint.glm`. It produces an error. The correct method 
in this case would be `broom:::confint.geeglm`.


Thank you!

Iago

[[alternative HTML version deleted]]

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


Re: [Rd] Could generic functions check different S3 methods for an object when one of them produces an error?

2019-06-18 Thread Fox, John
Dear Iago,

The R S3 object system works as expected here, using the first available method 
processing the class vector from left to right. The problem is that the broom 
package doesn't export the confint.geeglm() method but rather reserves it for 
internal use. I can't think why the package authors chose to do that but you 
could ask them. The following therefore works (following on with your example):

> confint.geeglm <- broom:::confint.geeglm
> confint(geefit)
lwr upr
(Intercept) 3607.595981 5204.790222
Frost -2.7233176.097207
Murder   -84.166856   38.838155

I hope this helps,
 John

  -
  John Fox, Professor Emeritus
  McMaster University
  Hamilton, Ontario, Canada
  Web: http::/socserv.mcmaster.ca/jfox

> On Jun 17, 2019, at 3:56 AM, IAGO GINÉ VÁZQUEZ  wrote:
> 
> Hi,
> 
> 
> Let's say one has an object with multiple classes, and a generic function to 
> apply to it has associated S3 methods for more than one of those classes. 
> Further, the method it chooses (I do not know how; some order in the class 
> vector?) is not the suitable one and it produces an error. Would there be 
> some way to make the generic function to choice the correct method, or in 
> case that for any method taken it produces an error, to try another one.
> 
> 
> For example (commented in detail 
> here):
> 
> 
> # object with multiple classes: the output of function `geepack::geeglm`. The 
> output of `class(object)`:
> 
> ```
> 
> [1] "geeglm" "gee" "glm" "lm"
> 
> ```
> 
> The generic function: `stats::confint`.
> 
> The S3 method chosen: `confint.glm`. It produces an error. The correct method 
> in this case would be `broom:::confint.geeglm`.
> 
> 
> Thank you!
> 
> Iago
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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


Re: [Rd] Could generic functions check different S3 methods for an object when one of them produces an error?

2019-06-18 Thread Duncan Murdoch

On 17/06/2019 3:56 a.m., IAGO GINÉ VÁZQUEZ wrote:

Hi,


Let's say one has an object with multiple classes, and a generic function to 
apply to it has associated S3 methods for more than one of those classes. 
Further, the method it chooses (I do not know how; some order in the class 
vector?) is not the suitable one and it produces an error. Would there be some 
way to make the generic function to choice the correct method, or in case that 
for any method taken it produces an error, to try another one.


For example (commented in detail 
here):


# object with multiple classes: the output of function `geepack::geeglm`. The 
output of `class(object)`:

```

[1] "geeglm" "gee" "glm" "lm"

```

The generic function: `stats::confint`.

The S3 method chosen: `confint.glm`. It produces an error. The correct method 
in this case would be `broom:::confint.geeglm`.


There is something wrong here.  If R knows about confint.geeglm (because 
broom is loaded, for example), then confint(x) should call it before it 
calls confint.glm.  It will look for methods confint.geeglm, then 
confint.gee, then confint.glm, then confint.lm.


However, when I run your code and load broom, I see

> methods("confint")
[1] confint.default  confint.glm* confint.lm   confint.lme* 
confint.lmList*  confint.nls* confint.nlsList*

see '?methods' for accessing help and source code


(with no confint.geeglm shown).  So something is wrong, probably in 
broom.  As far as I can see, it never declares confint.geeglm as an 
S3method in its NAMESPACE file.  (I'm looking at the devel version in 
https://github.com/tidymodels/broom/blob/master/NAMESPACE).


Get that fixed, and your problem should go away.

Duncan Murdoch

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


[Rd] making a vignette optional

2019-06-18 Thread Therneau, Terry M., Ph.D. via R-devel
I had added a vignette to the coxme package and all worked well locally, but it 
failed at 
CRAN. The issue is that the vignette involves using coxme for pedigree 
data, it 
doesn't work without the kinship2 package, and I hadn't put in the necessary 
"if 
(require(" logic.

The question is, how do I make the entire vignette conditional? If the package 
isn't 
available, there is nothing to run.  The latex itself will fail when it can't 
find the 
figures  (I float them), and the parts that don't will end up as inane 
discussion of 
material that isn't there.

Terry T.



[[alternative HTML version deleted]]

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


Re: [Rd] making a vignette optional

2019-06-18 Thread Iñaki Ucar
On Tue, 18 Jun 2019 at 19:03, Therneau, Terry M., Ph.D. via R-devel
 wrote:
>
> I had added a vignette to the coxme package and all worked well locally, but 
> it failed at
> CRAN. The issue is that the vignette involves using coxme for pedigree 
> data, it
> doesn't work without the kinship2 package, and I hadn't put in the necessary 
> "if
> (require(" logic.
>
> The question is, how do I make the entire vignette conditional? If the 
> package isn't
> available, there is nothing to run.  The latex itself will fail when it can't 
> find the
> figures  (I float them), and the parts that don't will end up as inane 
> discussion of
> material that isn't there.

This is what I do in my packages:
https://github.com/r-simmer/simmer/blob/master/vignettes/simmer-08-philosophers.Rmd#L21

If SweaveOpts accept code in the same way, you can set something like

\SweaveOpts{eval=requireNamespace("kinship2", quietly=TRUE)}

Iñaki

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


[Rd] Fast way to call an R function from C++?

2019-06-18 Thread King Jiefei
Hi,

I'm looking for a most efficient way to call an R function from C++ in a
package. I know there are two functions (`R_forceAndCall` and `Rf_eval`)
that can do the "call" part, but both are slow compared to calling the same
function in R. I also try to use Rcpp and it is the worse one. Here is my
test code:

C++ code:
```
// [[Rcpp::export]]
SEXP C_test1(SEXP f, SEXP x) {
SEXP call =PROTECT(Rf_lang2(f, x));
SEXP val = R_forceAndCall(call, 1, R_GlobalEnv);
UNPROTECT(1);
return val;
}

// [[Rcpp::export]]
SEXP C_test2(SEXP expr, SEXP env) {
SEXP val = Rf_eval(expr, env);
return val;
}

// [[Rcpp::export]]
SEXP C_test3(SEXP f,SEXP x) {
Function fun(f);
return fun(x);
}
```

R code:
```
testFunc<-function(x){
  x=x^2
  return(x)
}
evn=new.env()
evn$x=x
expr=quote(testFunc(evn$x))

testFunc(evn$x)
C_test1(testFunc, evn$x)
C_test2(expr,evn)
C_test3(testFunc,evn$x)
```

For the results, I run each function 1,000,000 times:

   - testFunc : 0.47 sec
   - C_test1 : 2.46 sec
   - C_test2 : 2.74 sec
   - C_test3 : 18.86 sec

It is clear to see that calling an R function in R is the fast one, it is
about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
functions have a similar performance and using Rcpp is the worst one. Is it
expected? Why is calling an R function from C++ much slower than calling
the function from R? Is there any faster way to do the function call in C++?

Best,
Jiefei

[[alternative HTML version deleted]]

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


Re: [Rd] Fast way to call an R function from C++?

2019-06-18 Thread Kevin Ushey
Hi Jiefei,

Calling into R from C++ code is more complicated than one might think.
Please see Tomas Kalibera's post here:
https://developer.r-project.org/Blog/public/2019/03/28/use-of-c---in-packages/index.html

The Rcpp Function class is more expensive than a regular Rf_eval()
because it tries to prevent errors (longjmps) from jumping to the top
level, so that the C++ stack can be properly unwound. In addition,
your example also pays a cost for the indirect function calls used
with Rcpp attributes, since the generated R wrapper functions will
then use `.Call()` under the hood. You could also try to directly use
.Call() to avoid that extra R function call overhead.

Even then, R's dispatch system for primitive functions (like `^`) is
likely still going to be faster than what you get from the .Call()
interface, especially since some extra validation does occur when
using .Call(). In practice, that difference is usually negligible
outside of synthetic benchmarks.

Best,
Kevin

On Tue, Jun 18, 2019 at 10:41 AM King Jiefei  wrote:
>
> Hi,
>
> I'm looking for a most efficient way to call an R function from C++ in a
> package. I know there are two functions (`R_forceAndCall` and `Rf_eval`)
> that can do the "call" part, but both are slow compared to calling the same
> function in R. I also try to use Rcpp and it is the worse one. Here is my
> test code:
>
> C++ code:
> ```
> // [[Rcpp::export]]
> SEXP C_test1(SEXP f, SEXP x) {
> SEXP call =PROTECT(Rf_lang2(f, x));
> SEXP val = R_forceAndCall(call, 1, R_GlobalEnv);
> UNPROTECT(1);
> return val;
> }
>
> // [[Rcpp::export]]
> SEXP C_test2(SEXP expr, SEXP env) {
> SEXP val = Rf_eval(expr, env);
> return val;
> }
>
> // [[Rcpp::export]]
> SEXP C_test3(SEXP f,SEXP x) {
> Function fun(f);
> return fun(x);
> }
> ```
>
> R code:
> ```
> testFunc<-function(x){
>   x=x^2
>   return(x)
> }
> evn=new.env()
> evn$x=x
> expr=quote(testFunc(evn$x))
>
> testFunc(evn$x)
> C_test1(testFunc, evn$x)
> C_test2(expr,evn)
> C_test3(testFunc,evn$x)
> ```
>
> For the results, I run each function 1,000,000 times:
>
>- testFunc : 0.47 sec
>- C_test1 : 2.46 sec
>- C_test2 : 2.74 sec
>- C_test3 : 18.86 sec
>
> It is clear to see that calling an R function in R is the fast one, it is
> about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
> functions have a similar performance and using Rcpp is the worst one. Is it
> expected? Why is calling an R function from C++ much slower than calling
> the function from R? Is there any faster way to do the function call in C++?
>
> Best,
> Jiefei
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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


Re: [Rd] Fast way to call an R function from C++?

2019-06-18 Thread Iñaki Ucar
For reference, your benchmark using UNWIND_PROTECT:

> system.time(test(testFunc, evn$x))
   user  system elapsed
  0.331   0.000   0.331
> system.time(test(C_test1, testFunc, evn$x))
   user  system elapsed
  2.029   0.000   2.036
> system.time(test(C_test2, expr, evn))
   user  system elapsed
  2.307   0.000   2.313
> system.time(test(C_test3, testFunc, evn$x))
   user  system elapsed
  2.131   0.000   2.138

Iñaki

On Tue, 18 Jun 2019 at 20:35, Iñaki Ucar  wrote:
>
> On Tue, 18 Jun 2019 at 19:41, King Jiefei  wrote:
> >
> > [...]
> >
> > It is clear to see that calling an R function in R is the fast one, it is
> > about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
> > functions have a similar performance and using Rcpp is the worst one. Is it
> > expected? Why is calling an R function from C++ much slower than calling
> > the function from R? Is there any faster way to do the function call in C++?
>
> Yes, there is: enable fast evaluation by setting
> -DRCPP_USE_UNWIND_PROTECT, or alternatively, use
>
> // [[Rcpp::plugins(unwindProtect)]]
>
> Iñaki



-- 
Iñaki Úcar

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


Re: [Rd] Fast way to call an R function from C++?

2019-06-18 Thread Iñaki Ucar
On Tue, 18 Jun 2019 at 19:41, King Jiefei  wrote:
>
> [...]
>
> It is clear to see that calling an R function in R is the fast one, it is
> about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
> functions have a similar performance and using Rcpp is the worst one. Is it
> expected? Why is calling an R function from C++ much slower than calling
> the function from R? Is there any faster way to do the function call in C++?

Yes, there is: enable fast evaluation by setting
-DRCPP_USE_UNWIND_PROTECT, or alternatively, use

// [[Rcpp::plugins(unwindProtect)]]

Iñaki

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


Re: [Rd] Fast way to call an R function from C++?

2019-06-18 Thread King Jiefei
Hello Kevin and Iñaki,

Thanks for your quick responses. I sincerely appreciate them! I can see how
complicated it is to interact with R in C. Iñaki's suggestion is very
helpful, I saw there is a lot of performance gain by turning the flag on,
but sadly the best performance it can offer still cannot beat R itself. It
is interesting to see that C++ is worse than R in this special case despite
there is a common belief that C++ code is the fast one... Anyway, thanks
again for your suggestions and reference!

Best,
Jiefei


On Tue, Jun 18, 2019 at 2:39 PM Iñaki Ucar  wrote:

> For reference, your benchmark using UNWIND_PROTECT:
>
> > system.time(test(testFunc, evn$x))
>user  system elapsed
>   0.331   0.000   0.331
> > system.time(test(C_test1, testFunc, evn$x))
>user  system elapsed
>   2.029   0.000   2.036
> > system.time(test(C_test2, expr, evn))
>user  system elapsed
>   2.307   0.000   2.313
> > system.time(test(C_test3, testFunc, evn$x))
>user  system elapsed
>   2.131   0.000   2.138
>
> Iñaki
>
> On Tue, 18 Jun 2019 at 20:35, Iñaki Ucar  wrote:
> >
> > On Tue, 18 Jun 2019 at 19:41, King Jiefei  wrote:
> > >
> > > [...]
> > >
> > > It is clear to see that calling an R function in R is the fast one, it
> is
> > > about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
> > > functions have a similar performance and using Rcpp is the worst one.
> Is it
> > > expected? Why is calling an R function from C++ much slower than
> calling
> > > the function from R? Is there any faster way to do the function call
> in C++?
> >
> > Yes, there is: enable fast evaluation by setting
> > -DRCPP_USE_UNWIND_PROTECT, or alternatively, use
> >
> > // [[Rcpp::plugins(unwindProtect)]]
> >
> > Iñaki
>
>
>
> --
> Iñaki Úcar
>

[[alternative HTML version deleted]]

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


[Rd] Small bug in fligner.test - constant values can produce significant results (patch attached)

2019-06-18 Thread Karolis Koncevičius

In specific cases fligner.test() can produce a small p-value even when both 
groups have constant variance.

Here is an illustration:

   fligner.test(c(1,1,2,2), c("a","a","b","b"))
   # p-value = NA

But:

   fligner.test(c(1,1,1,2,2,2), c("a","a","a","b","b","b"))
   # p-value < 2.2e-16

This can potentially get dangerous if people perform lots of parallel tests of 
this type (i.e. when doing a test for each gene in genomic studies).

Submitted a proposed patch that should solve the issue by producing an error "data 
is essentially constant"
- which is the same error message found in t-test under similar conditions.

P.S. First time writing to this list. Read all the guides of posting, but sorry 
in advance if I still missed any rules.

---
Karolis Koncevičius
Index: src/library/stats/R/fligner.test.R
===
--- src/library/stats/R/fligner.test.R  (revision 76710)
+++ src/library/stats/R/fligner.test.R  (working copy)
@@ -55,6 +55,8 @@
 
 ## Careful. This assumes that g is a factor:
 x <- x - tapply(x,g,median)[g]
+if (all(x == 0))
+  stop("data are essentially constant")
 
 a <- qnorm((1 + rank(abs(x)) / (n + 1)) / 2)
 STATISTIC <- sum(tapply(a, g, "sum")^2 / tapply(a, g, "length"))
__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel