I'm just curious. Why making "if" generic is even more dangerous? Best, Da On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:> `!` is a generic, `if` is not. You can define an `if` that is generic, > but this might be even more dangerous.... > > ? `if` <- function(a, b, c) UseMethod("if") > ? `if.default` <- function(a,b,c) base::`if`(a, b, c) > ? `if.foo` <- function(a, b, c) FALSE > ? a <- structure(42, class = "foo") > > ? if (a) TRUE else FALSE > [1] FALSE > > ? if (1) TRUE else FALSE > [1] TRUE > > Gabor > > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >> Thanks. >> Can I override it for a specific class? >> I can do that for operators such as "!". For example, "!.fm" works for >> objects of the class "fm". >> It seems I can't do the same for "if". >> >> Best, >> Da >> >> On Sat, Mar 4, 2017 at 12:41 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote: >>> You can. Perhaps needless to say, be careful with this. >>> >>> ? `if` <- function(...) FALSE >>> ? if (TRUE) TRUE else FALSE >>> [1] FALSE >>> >>> G. >>> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >>>> Hello, >>>> >>>> I heard we can override almost everything in R. Is it possible to >>>> override "if" keyword in R to evaluate my own object instead of a >>>> logical value? >>>> >>>> Thanks, >>>> Da >>>> >>>> ______________________________________________ >>>> R-devel at r-project.org mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
I'm curious as to precisely why someone would want to do this. On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng <zhengda1936 at gmail.com> wrote:> I'm just curious. Why making "if" generic is even more dangerous? > > Best, > Da > > On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> > wrote: > > `!` is a generic, `if` is not. You can define an `if` that is generic, > > but this might be even more dangerous.... > > > > ? `if` <- function(a, b, c) UseMethod("if") > > ? `if.default` <- function(a,b,c) base::`if`(a, b, c) > > ? `if.foo` <- function(a, b, c) FALSE > > ? a <- structure(42, class = "foo") > > > > ? if (a) TRUE else FALSE > > [1] FALSE > > > > ? if (1) TRUE else FALSE > > [1] TRUE > > > > Gabor > > > > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote: > >> Thanks. > >> Can I override it for a specific class? > >> I can do that for operators such as "!". For example, "!.fm" works for > >> objects of the class "fm". > >> It seems I can't do the same for "if". > >> > >> Best, > >> Da > >> > >> On Sat, Mar 4, 2017 at 12:41 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> > wrote: > >>> You can. Perhaps needless to say, be careful with this. > >>> > >>> ? `if` <- function(...) FALSE > >>> ? if (TRUE) TRUE else FALSE > >>> [1] FALSE > >>> > >>> G. > >>> > >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> > wrote: > >>>> Hello, > >>>> > >>>> I heard we can override almost everything in R. Is it possible to > >>>> override "if" keyword in R to evaluate my own object instead of a > >>>> logical value? > >>>> > >>>> Thanks, > >>>> Da > >>>> > >>>> ______________________________________________ > >>>> R-devel at r-project.org mailing list > >>>> https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
In my case, I create a new type of matrices and override matrix operations in R for these matrices. My goal is to make the system as transparent as possible, which means my system should execute the existing R code without modification. The problem is that when data is in my own vectors or matrices, "if" or "while" can't access their values unless we explicitly convert them into R objects. But this means users need to modify the existing code. So I hope I can override "if", "while", etc to access data in my own vectors and matrices directly. Does this sound reasonable? Best, Da On Sat, Mar 4, 2017 at 3:22 PM, Michael Lawrence <lawrence.michael at gene.com> wrote:> I'm curious as to precisely why someone would want to do this. > > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng <zhengda1936 at gmail.com> wrote: >> >> I'm just curious. Why making "if" generic is even more dangerous? >> >> Best, >> Da >> >> On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> >> wrote: >> > `!` is a generic, `if` is not. You can define an `if` that is generic, >> > but this might be even more dangerous.... >> > >> > ? `if` <- function(a, b, c) UseMethod("if") >> > ? `if.default` <- function(a,b,c) base::`if`(a, b, c) >> > ? `if.foo` <- function(a, b, c) FALSE >> > ? a <- structure(42, class = "foo") >> > >> > ? if (a) TRUE else FALSE >> > [1] FALSE >> > >> > ? if (1) TRUE else FALSE >> > [1] TRUE >> > >> > Gabor >> > >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >> >> Thanks. >> >> Can I override it for a specific class? >> >> I can do that for operators such as "!". For example, "!.fm" works for >> >> objects of the class "fm". >> >> It seems I can't do the same for "if". >> >> >> >> Best, >> >> Da >> >> >> >> On Sat, Mar 4, 2017 at 12:41 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> >> >> wrote: >> >>> You can. Perhaps needless to say, be careful with this. >> >>> >> >>> ? `if` <- function(...) FALSE >> >>> ? if (TRUE) TRUE else FALSE >> >>> [1] FALSE >> >>> >> >>> G. >> >>> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> >> >>> wrote: >> >>>> Hello, >> >>>> >> >>>> I heard we can override almost everything in R. Is it possible to >> >>>> override "if" keyword in R to evaluate my own object instead of a >> >>>> logical value? >> >>>> >> >>>> Thanks, >> >>>> Da >> >>>> >> >>>> ______________________________________________ >> >>>> R-devel at r-project.org mailing list >> >>>> https://stat.ethz.ch/mailman/listinfo/r-devel >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel > >
dplyr::translate_sql() redefines lots of functions, include "if", to translate from R syntax to SQL syntax.> dplyr::translate_sql(if ("mpg">25) "better" else "worse")<SQL> CASE WHEN ('mpg' > 25.0) THEN ('better') ELSE ('worse') END Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Mar 4, 2017 at 12:22 PM, Michael Lawrence <lawrence.michael at gene.com> wrote:> I'm curious as to precisely why someone would want to do this. > > On Sat, Mar 4, 2017 at 11:49 AM, Da Zheng <zhengda1936 at gmail.com> wrote: > >> I'm just curious. Why making "if" generic is even more dangerous? >> >> Best, >> Da >> >> On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> >> wrote: >> > `!` is a generic, `if` is not. You can define an `if` that is generic, >> > but this might be even more dangerous.... >> > >> > ? `if` <- function(a, b, c) UseMethod("if") >> > ? `if.default` <- function(a,b,c) base::`if`(a, b, c) >> > ? `if.foo` <- function(a, b, c) FALSE >> > ? a <- structure(42, class = "foo") >> > >> > ? if (a) TRUE else FALSE >> > [1] FALSE >> > >> > ? if (1) TRUE else FALSE >> > [1] TRUE >> > >> > Gabor >> > >> > On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >> >> Thanks. >> >> Can I override it for a specific class? >> >> I can do that for operators such as "!". For example, "!.fm" works for >> >> objects of the class "fm". >> >> It seems I can't do the same for "if". >> >> >> >> Best, >> >> Da >> >> >> >> On Sat, Mar 4, 2017 at 12:41 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> >> wrote: >> >>> You can. Perhaps needless to say, be careful with this. >> >>> >> >>> ? `if` <- function(...) FALSE >> >>> ? if (TRUE) TRUE else FALSE >> >>> [1] FALSE >> >>> >> >>> G. >> >>> >> >>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> >> wrote: >> >>>> Hello, >> >>>> >> >>>> I heard we can override almost everything in R. Is it possible to >> >>>> override "if" keyword in R to evaluate my own object instead of a >> >>>> logical value? >> >>>> >> >>>> Thanks, >> >>>> Da >> >>>> >> >>>> ______________________________________________ >> >>>> R-devel at r-project.org mailing list >> >>>> https://stat.ethz.ch/mailman/listinfo/r-devel >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Because the S3 class system is very informal. E.g. if you happen to have an `if.whatever` function, that will be automatically a method of your generic. Gabor On Sat, Mar 4, 2017 at 7:49 PM, Da Zheng <zhengda1936 at gmail.com> wrote:> I'm just curious. Why making "if" generic is even more dangerous? > > Best, > Da > > On Sat, Mar 4, 2017 at 1:22 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote: >> `!` is a generic, `if` is not. You can define an `if` that is generic, >> but this might be even more dangerous.... >> >> ? `if` <- function(a, b, c) UseMethod("if") >> ? `if.default` <- function(a,b,c) base::`if`(a, b, c) >> ? `if.foo` <- function(a, b, c) FALSE >> ? a <- structure(42, class = "foo") >> >> ? if (a) TRUE else FALSE >> [1] FALSE >> >> ? if (1) TRUE else FALSE >> [1] TRUE >> >> Gabor >> >> On Sat, Mar 4, 2017 at 5:47 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >>> Thanks. >>> Can I override it for a specific class? >>> I can do that for operators such as "!". For example, "!.fm" works for >>> objects of the class "fm". >>> It seems I can't do the same for "if". >>> >>> Best, >>> Da >>> >>> On Sat, Mar 4, 2017 at 12:41 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote: >>>> You can. Perhaps needless to say, be careful with this. >>>> >>>> ? `if` <- function(...) FALSE >>>> ? if (TRUE) TRUE else FALSE >>>> [1] FALSE >>>> >>>> G. >>>> >>>> On Sat, Mar 4, 2017 at 5:36 PM, Da Zheng <zhengda1936 at gmail.com> wrote: >>>>> Hello, >>>>> >>>>> I heard we can override almost everything in R. Is it possible to >>>>> override "if" keyword in R to evaluate my own object instead of a >>>>> logical value? >>>>> >>>>> Thanks, >>>>> Da >>>>> >>>>> ______________________________________________ >>>>> R-devel at r-project.org mailing list >>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
On Sun, Mar 5, 2017 at 8:13 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:> Because the S3 class system is very informal. E.g. if you happen to > have an `if.whatever` function, that will be automatically a method of > your generic.For example: x <- structure(1:10, class = "test") t(x) #> #> One Sample t-test #> #> data: x #> t = 5.7446, df = 9, p-value = 0.0002782 #> alternative hypothesis: true mean is not equal to 0 #> 95 percent confidence interval: #> 3.334149 7.665851 #> sample estimates: #> mean of x #> 5.5 -- http://hadley.nz