Re: [Rd] how to manipulate dput output format

2012-06-20 Thread Simon Urbanek

On Jun 19, 2012, at 11:04 AM, andre zege wrote:

> I am reading into Java dput output for a matrix, more specifically for a
> file backed big-matrix. I basically need to lift dimnames for a matrix from
> dput output. It's no big deal, but the code is very 'hackish' due to the
> need to get rid of quotes, endlines, parenthesis, etc. I was wondering if i
> could manipulate to an extent dput output with some options that define it,
> for example, get rid of quoting each element  in matirx dimnames somehow.
> Another great thing wiould be to make dput dump rownames and colnames on
> two separate lines, but i don't think it's possible. To give a specific
> example, instead of dput output like
> 
> 
> **new("big.matrix.descriptor"
>, description = structure(list(sharedType = "FileBacked", filename =
> "res", totalRows = 1528,
>totalCols = 53040, rowOffset = c(0, 1528), colOffset = c(0,
>53040), nrow = 1528, ncol = 53040, rowNames = c("A", "AA",
>"RNT.A", "ADVA", "AAPL", "AAS", "ABFS", "ABM", "ABT", "ACI",
>...
> 
> I'd prefer ideally to have it in the form where rownames and colnames don't
> have quotes and newlines and if possible are on separate lines
> 
> new("big.matrix.descriptor"
>, description = structure(list(sharedType = "FileBacked", filename =
> "res", totalRows = 1528,
>totalCols = 53040, rowOffset = c(0, 1528), colOffset = c(0,
>53040), nrow = 1528, ncol = 53040,
> rowNames = c(A, AA, RNT.A, ADVA, AAPL, AAS, ABFS, ABM, ABT, ... )
> colNames = c(...)
> 

dput() is intended to be parsed by R so the above is not possible without 
massaging the output. But why in the would would you use dput() for something 
that you want to read in Java? Why don't you use a format that Java can read 
easily - such as JSON?

Cheers,
Simon


>   [[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] how to manipulate dput output format

2012-06-20 Thread Dirk Eddelbuettel

On 20 June 2012 at 10:33, Simon Urbanek wrote:
| 
| On Jun 19, 2012, at 11:04 AM, andre zege wrote:
| 
| > I am reading into Java dput output for a matrix, more specifically for a
| > file backed big-matrix. I basically need to lift dimnames for a matrix from
| > dput output. It's no big deal, but the code is very 'hackish' due to the
| > need to get rid of quotes, endlines, parenthesis, etc. I was wondering if i
| > could manipulate to an extent dput output with some options that define it,
| > for example, get rid of quoting each element  in matirx dimnames somehow.
| > Another great thing wiould be to make dput dump rownames and colnames on
| > two separate lines, but i don't think it's possible. To give a specific
| > example, instead of dput output like
| > 
| > 
| > **new("big.matrix.descriptor"
| >, description = structure(list(sharedType = "FileBacked", filename =
| > "res", totalRows = 1528,
| >totalCols = 53040, rowOffset = c(0, 1528), colOffset = c(0,
| >53040), nrow = 1528, ncol = 53040, rowNames = c("A", "AA",
| >"RNT.A", "ADVA", "AAPL", "AAS", "ABFS", "ABM", "ABT", "ACI",
| >...
| > 
| > I'd prefer ideally to have it in the form where rownames and colnames don't
| > have quotes and newlines and if possible are on separate lines
| > 
| > new("big.matrix.descriptor"
| >, description = structure(list(sharedType = "FileBacked", filename =
| > "res", totalRows = 1528,
| >totalCols = 53040, rowOffset = c(0, 1528), colOffset = c(0,
| >53040), nrow = 1528, ncol = 53040,
| > rowNames = c(A, AA, RNT.A, ADVA, AAPL, AAS, ABFS, ABM, ABT, ... )
| > colNames = c(...)
| > 
| 
| dput() is intended to be parsed by R so the above is not possible without 
massaging the output. But why in the would would you use dput() for something 
that you want to read in Java? Why don't you use a format that Java can read 
easily - such as JSON?

Or even use something designed for fast, large scale data serialization such
as Google Protocol Buffers. 

You get code generated for Java from using the Google library / binaries for
it, and the R package RProtoBuf will provide the other side.  

Dirk

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

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


Re: [Rd] "Incompatible methods" for overloaded operator

2012-06-20 Thread Hadley Wickham
Hi all,

Any ideas about this?  As far as I can tell it should work - and I
don't understand why it's ok when run outside of a package.

Hadley

On Wed, Jun 13, 2012 at 7:41 PM, Winston Chang  wrote:
> I'm trying to overload an operator, and I'm running into a strange problem.
> It happens when I install and load the package, but not when I simply
> source() the code.
>
> I'm defining + for two classes. The R code looks like this:
> #' @export
> #' @method "+" a
> `+.a` <- function (x1, x2) {
>    message("Running custom + function")
> }
>
> #' @export
> #' @method "+" b
> `+.b` <- `+.a`
>
> In some cases I do a+b, and in other cases, I do b+b. I'm told that the +.a
> and +.b functions must be identical to avoid the error about "Incompatible
> methods". (In the actual code, the overloaded + function checks the classes
> of x1 and x2, and then sends them off to other functions.)
>
> This is the NAMESPACE file:
> S3method("+",a)
> S3method("+",b)
>
> I've put the code up at https://github.com/wch/badadd.
>
>
>
> If I just cut and paste the function definitions to my R session, it works
> fine:
> x + y
> # Running + function
> # NULL
>
>
> However, if I install and load the package, it gives a warning about
> incompatible methods, and then seems to fall back to the arithmetic +
> operator:
> library(badadd)
> x + y
> # [1] 3
> # attr(,"class")
> # [1] "a"
> # Warning message:
> # Incompatible methods ("+.a", "+.b") for "+"
>
>
> Is this expected behavior? And if so, is there a workaround?
> -Winston
>
>        [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] "Incompatible methods" for overloaded operator

2012-06-20 Thread Martin Morgan

On 06/20/2012 07:25 PM, Hadley Wickham wrote:

Hi all,

Any ideas about this?  As far as I can tell it should work - and I
don't understand why it's ok when run outside of a package.


from ?groupGeneric under 'Ops' (of which "+" is one)

  used.  If different methods are found, there is a warning
  about 'incompatible methods': in that case or if no method is
  found for either argument the internal method is used.

which doesn't really explain why it works at the command line but not in 
a package. S4 methods can be defined on both arguments, so


  setClass("A", contains="numeric")
  setClass("B", contains="numeric")

  setMethod("+", c("A", "A"), function(e1, e2) message("A + A"))
  setMethod("+", c("A", "B"), function(e1, e2) message("A + B"))
  setMethod("+", c("B", "A"), function(e1, e2) message("B + A"))
  setMethod("+", c("B", "B"), function(e1, e2) message("B + B"))

with

  exportClasses("A", "B")
  exportMethods("+")

and then

> new("A") + new("B")
A + B
NULL


Martin



Hadley

On Wed, Jun 13, 2012 at 7:41 PM, Winston Chang  wrote:

I'm trying to overload an operator, and I'm running into a strange problem.
It happens when I install and load the package, but not when I simply
source() the code.

I'm defining + for two classes. The R code looks like this:
#' @export
#' @method "+" a
`+.a`<- function (x1, x2) {
message("Running custom + function")
}

#' @export
#' @method "+" b
`+.b`<- `+.a`

In some cases I do a+b, and in other cases, I do b+b. I'm told that the +.a
and +.b functions must be identical to avoid the error about "Incompatible
methods". (In the actual code, the overloaded + function checks the classes
of x1 and x2, and then sends them off to other functions.)

This is the NAMESPACE file:
S3method("+",a)
S3method("+",b)

I've put the code up at https://github.com/wch/badadd.



If I just cut and paste the function definitions to my R session, it works
fine:
x + y
# Running + function
# NULL


However, if I install and load the package, it gives a warning about
incompatible methods, and then seems to fall back to the arithmetic +
operator:
library(badadd)
x + y
# [1] 3
# attr(,"class")
# [1] "a"
# Warning message:
# Incompatible methods ("+.a", "+.b") for "+"


Is this expected behavior? And if so, is there a workaround?
-Winston

[[alternative HTML version deleted]]

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







--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

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


Re: [Rd] "Incompatible methods" for overloaded operator

2012-06-20 Thread Hadley Wickham
> from ?groupGeneric under 'Ops' (of which "+" is one)
>
>          used.  If different methods are found, there is a warning
>          about 'incompatible methods': in that case or if no method is
>          found for either argument the internal method is used.
>
> which doesn't really explain why it works at the command line but not in a
> package. S4 methods can be defined on both arguments, so

But aren't the methods compatible?  If equality doesn't make a method
compatible what does?

>  setClass("A", contains="numeric")
>  setClass("B", contains="numeric")
>
>  setMethod("+", c("A", "A"), function(e1, e2) message("A + A"))
>  setMethod("+", c("A", "B"), function(e1, e2) message("A + B"))
>  setMethod("+", c("B", "A"), function(e1, e2) message("B + A"))
>  setMethod("+", c("B", "B"), function(e1, e2) message("B + B"))
>
> with
>
>  exportClasses("A", "B")
>  exportMethods("+")
>
> and then
>
>> new("A") + new("B")
> A + B
> NULL

But unfortunately that doesn't work for S3 classes (even with
setOldClass) so it doesn't help much unless we want to rewrite
everything in S4 :/

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] S4 Reference Classes: declaring public and private methods

2012-06-20 Thread Martin Morgan

On 06/18/2012 01:01 AM, Janko Thyson wrote:

Dear list,

is there a way to declare public and private methods in S4 Reference
Classes? If not, are there plans to add such a feature?


Hi Janko -- your question is a little ambiguous; if it's about a 
built-in facility to create public vs. private methods, I think the 
answer is no, there is no built-in facility.


For the more general question of how one might go about enforcing 
private methods, I can think of several convoluted starts, but these 
seem like terrible hacks and I wouldn't want to commit them to paper; 
perhaps someone else will be more bold.


Martin



You'll find a small code example of what I mean at Stackoverflow:
http://stackoverflow.com/questions/11073253/oop-with-r-is-there-a-way-to-declare-public-and-private-methods-s4-reference-c


Thanks a lot,
Janko Thyson

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



--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

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


Re: [Rd] "Incompatible methods" for overloaded operator

2012-06-20 Thread Martin Morgan

On 06/20/2012 08:06 PM, Hadley Wickham wrote:

from ?groupGeneric under 'Ops' (of which "+" is one)

  used.  If different methods are found, there is a warning
  about 'incompatible methods': in that case or if no method is
  found for either argument the internal method is used.

which doesn't really explain why it works at the command line but not in a
package. S4 methods can be defined on both arguments, so


But aren't the methods compatible?  If equality doesn't make a method
compatible what does?


Actually I guess that turns out to be the key (to why they work at the 
command line but not in a package).  At the command line they really 
_are_ the same, e.g., .Internal(inspect("+.a")) has the same  address as 
"+.b". In the package they (e.g., badadd:::"+.a") have different 
addresses, I suppose because S3method() acts on them independently.





  setClass("A", contains="numeric")
  setClass("B", contains="numeric")

  setMethod("+", c("A", "A"), function(e1, e2) message("A + A"))
  setMethod("+", c("A", "B"), function(e1, e2) message("A + B"))
  setMethod("+", c("B", "A"), function(e1, e2) message("B + A"))
  setMethod("+", c("B", "B"), function(e1, e2) message("B + B"))

with

  exportClasses("A", "B")
  exportMethods("+")

and then


new("A") + new("B")

A + B
NULL


But unfortunately that doesn't work for S3 classes (even with
setOldClass) so it doesn't help much unless we want to rewrite
everything in S4 :/


rewrite?



Hadley




--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

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


Re: [Rd] "Incompatible methods" for overloaded operator

2012-06-20 Thread Winston Chang
On Wed, Jun 20, 2012 at 10:49 PM, Martin Morgan  wrote:

> On 06/20/2012 08:06 PM, Hadley Wickham wrote:
>
>> But aren't the methods compatible?  If equality doesn't make a method
>> compatible what does?
>>
>
> Actually I guess that turns out to be the key (to why they work at the
> command line but not in a package).  At the command line they really _are_
> the same, e.g., .Internal(inspect("+.a")) has the same  address as "+.b".
> In the package they (e.g., badadd:::"+.a") have different addresses, I
> suppose because S3method() acts on them independently.
>
>
Thanks for the investigation. I wonder if there's some sort of workaround.



>
>> But unfortunately that doesn't work for S3 classes (even with
>> setOldClass) so it doesn't help much unless we want to rewrite
>> everything in S4 :/
>>
>
> rewrite?
>

Yes, the original question is the result of some issues that came up in
ggplot2 development...

-Winston

[[alternative HTML version deleted]]

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