Re: [Rd] creating a '[' method for an S4 class

2012-07-07 Thread Martin Maechler
> Benilton Carvalho 
> on Sat, 7 Jul 2012 03:49:34 +0100 writes:

> Hi,

> I'm working on an S4 class that is expected to behave like an array.

> I have some difficulties when defining '[' and I wonder if someone
> could point me to the right direction:

> 1) Call the S4 object "obj"
> 2) Assume dim(obj) = c(10, 4, 2)
> 3) Suppose someone calls: obj[1:3,] , which is a mistake, given
> dim(obj); how do I detect such situations?

yes, a good question with a tricky answer.
Actually not only relevant for S4,  and hence one of the basic
tricks is already visible in   [.data.frame :
--- you need to use nargs() in addition to others more well
known tools ---

*and* it is trickier than you think:

Not only want you differentiate between  
  obj[ i , ]
and
  obj[ i , , ]

but more importantly,
between
  obj[ i ]
and
  obj[ i , ]
and that's  where nargs() comes into play
(as 'j', the 2nd argument is missing in both cases, you cannot
 use that check alone).

In my CRAN package  Rmpfr  {Interface R <--> MPFR library for
  arbitrary precision arithmetic}
I use S4 classes and methods extensively, as two argument
dispatch is the most natural paradigm for arithmetic.
Here is the part (from Rmpfr/R/array.R )
which implements "[" for 'mpfrArray':
{but you probably want the full package in src to understand all
 the details}:

.mpfrA.subset <- function(x,i,j, ..., drop) {
nA <- nargs()
if(getOption("verbose"))
message(sprintf("nargs() == %d  mpfrArray indexing ... ", nA))

r <- getD(x)# data part of x, a list()
if(nA == 2) ## A[i]
return(new("mpfr", r[i]))
## else: nA != 2 : nA > 2 -
dim(r) <- (dx <- dim(x))
dimnames(r) <- dimnames(x)
r <- r[i,j, ..., drop=drop]
if(drop && is.null(dim(r)))
new("mpfr", r)
else {
D <- if(is.null(dr <- dim(r))) # ==> drop is FALSE; can this happen?
rep.int(1L, length(r)) else dr
x@Dim <- D
x@Dimnames <- if(is.null(dn <- dimnames(r)))
vector("list", length(D)) else dn
if(length(D) == 2 && class(x) != "mpfrMatrix")
## low-level "coercion" from mpfrArray to *Matrix :
attr(x,"class") <- getClass("mpfrMatrix")@className
attributes(r) <- NULL
setDataPart(x, r, check=FALSE)
}
}

## "["
setMethod("[", signature(x = "mpfrArray", i = "ANY", j = "ANY", drop = "ANY"),
  .mpfrA.subset)

## this signature needs a method here, or it triggers the one for "mpfr"
setMethod("[", signature(x = "mpfrArray", i = "ANY", j = "missing",
 drop = "missing"),
  .mpfrA.subset)




> Thank you very much for your attention and time,
> benilton

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


Re: [Rd] Problem in vignette packaging of Sweave in utils package

2012-07-07 Thread Duncan Murdoch

On 12-07-03 1:21 PM, Paul Johnson wrote:

In ?Sweave, it refers to Sweave User Manual. In the doc folder of
utils package, I see "Sweave.pdf".

However, I can't find it from within R



vignette("Sweave User Manual")

Warning message:
vignette ‘Sweave User Manual’ not found


Turns out there was a bug in the code to install from tarballs, so base 
package vignettes weren't fully installed.  I've fixed it; installs from 
R-patched or R-devel revisions after 59750 should be okay.


Duncan Murdoch





browseVignettes("utils")

No vignettes found by browseVignettes("utils")



library(help=utils)


does not refer to any vignettes.

The vignette does not appear in the main page for utils in help.start().

I checked the source code for the Sweave vignette, but I don't see
anything wrong. It has all of the required elements:

%\VignetteIndexEntry{Sweave User Manual}
%\VignettePackage{utils}
%\VignetteDepends{tools}
%\VignetteDepends{datasets}
%\VignetteDepends{stats}

Am I accessing it incorrectly, or is there something wrong in my
install of R-2.15.1?


sessionInfo()

R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=C LC_NAME=C
  [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.15.1


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