Re: [Rd] Attributes of 1st argument in ...

From: Daniel Murphy <chiefmurphy_at_gmail.com>
Date: Mon, 05 Jul 2010 23:49:03 -0700

I stand corrected. It is possible for the S4 cbind to know the symbol of unnamed arguments, by "cheating": colnames(base::cbind(...,deparse.level = deparse.level)) -- which tells me that there ought to be a non-cheating way.

I note that the Rmpfr class union, Mnumber, means that the simple cbind(1:3) must go through the S4 cbind. Now I know why your method, Martin, tests for all is.atomic at the beginning (which will not work for mondates, which test TRUE). This actually increases the price of an S4 cbind, a price increase that I believe could be avoided if S4 dispatch on dots occurred based on the first argument, which is back to my original post. I'm guessing that's too difficult to implement.

Again, thanks to everyone for your time in schooling me on the issues. :-)

Dan

On Mon, Jul 5, 2010 at 10:35 PM, Daniel Murphy <chiefmurphy_at_gmail.com>wrote:

> I truly am grateful for all the help.
>
> I never would have thought defining a class union for objects that can be
> combined with cbind, as you do in Rmpfr. That does the trick.
>
> Is it true that the only difference in behavior between the S4 cbind and
> base::cbind is that, unlike base::cbind, the S4 cbind does not seem to know
> the symbol of unnamed arguments? E.g.,
> x<-mpfr(0:7,100)
> cbind(x,x)
> If so, that seems a small price to pay for the S4 functionality.
>
> Thanks again.
>
> Cheers,
> Dan
>
> On Mon, Jul 5, 2010 at 2:47 AM, Martin Maechler <
> maechler_at_stat.math.ethz.ch> wrote:
>
>> >>>>> "DM" == Daniel Murphy <chiefmurphy_at_gmail.com>
>> >>>>> on Sun, 4 Jul 2010 11:11:43 -0700 writes:
>>
>> DM> Thank you, Professor, for drawing my attention to the nifty
>> tracemem
>> DM> function. I'm using the ..1 syntax to check the properties of the
>> S4 class
>> DM> at function call.
>>
>> DM> The Description at ?"cBind" tells me that I'm not alone in this
>> predicament.
>>
>>
>> DM> Just as Matrix needs its own cBind function, my package will need
>> its own
>> DM> cbindMondate function. Alas, subclasses of mondate will also need
>> their own
>> DM> binding functions, thus defeating one of the purposes of the class
>> paradigm
>> DM> (for binding, anyway).
>>
>> I don't think you see the full picture:
>>
>> Matrix::cBind() builds on methods::cbind2()
>>
>> and cbind2 / rbind2 are there (in package methods) for you to
>> use setMethod() on them.
>>
>> BTW: When Matrix::cBind() -- and cbind2 / rbind2 in package
>> methods were written,
>>
>> R did not have the possibility yet to write methods for "...",
>> which it now does.
>>
>> In the package Rmpfr (arbitrary-precision number ["mpfr"] computing),
>> where I also define methods for matrices of such "mpfr" numbers
>> (classes "mpfrMatrix" and "mpfrArray"),
>> I use the new feature of defining methods for "..." :
>>
>> setGeneric("cbind", signature = "...")
>>
>> setMethod("cbind", "Mnumber",
>> function(..., deparse.level = 1) {
>> args <- list(...)
>> if(all(sapply(args, is.atomic)))
>> return( base::cbind(..., deparse.level = deparse.level) )
>> ## else: at least one is "mpfr(Matrix/Array)"
>>
>> if(any(sapply(args, is.character))) {
>> ## result will be <character> matrix !
>> isM <- sapply(args, is, class2 = "mpfr")
>> args[isM] <- lapply(args[isM], as, Class = "character")
>> return(do.call(base::cbind,
>> c(args,
>> list(deparse.level=deparse.level))))
>>
>> } else if(any(sapply(args, is.complex))) {
>> ## result will be <complex> matrix;
>> ## in the future <complex_mpfr> ???
>>
>> stop("cbind(...) of 'complex' and 'mpfr' objects is not
>> implemented")
>> ## give at least warning !!
>> }
>> ## else
>>
>> ..........
>> ..........
>> })
>>
>>
>> where I use a useful class union
>>
>> setClassUnion("Mnumber",
>> members = c("array_or_vector", # *but* must be numeric-like
>> "mpfr", "mpfrArray", "mpfrMatrix"))
>>
>> ----
>>
>> As always, I'd recommend to read the R source, rather than just
>> investigate the installed package.
>> You can get the *source* tarbal, i.e., the *.tar.gz file from
>> CRAN, or, as it's all on R-forge,
>> http://rmpfr.r-forge.r-project.org/
>> you can get the source tarball here,
>>
https://r-forge.r-project.org/R/?group_id=386
>> or browse the source at
>> https://r-forge.r-project.org/scm/viewvc.php/pkg/?root=rmpfr
>>
>> But if I were you I'd get it via
>> svn checkout svn://svn.r-forge.r-project.org/svnroot/rmpfr/pkg Rmpfr
>>
>>
>>
>> DM> As an aside, I wonder why, on around line 75, cBind uses 'rep.int
>> ("",
>> DM> ncol(r))' rather than the slightly faster 'character(ncol(r))'.
>>
>> Well, I would not remember, but the first one is a more self-explaining,
>> ... and I would guess strongly that time difference is
>> irrelevant in the context where it's used...
>> ... but then thanks for your hint :-)
>>
>> Martin
>>
>> <snip>
>
>
>

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 06 Jul 2010 - 06:52:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 06 Jul 2010 - 10:40:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive