File: type.R

package info (click to toggle)
r-bioc-s4arrays 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: ansic: 730; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,863 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
### =========================================================================
### type()
### -------------------------------------------------------------------------


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### Default type() method
###

### Perform 'extract_array(x, list(integer(0), ..., integer(0)))'.
### 'x' is **trusted** to be an array-like object.
extract_empty_array <- function(x)
{
    index <- rep.int(list(integer(0)), length(dim(x)))
    extract_array(x, index)
}

### The default type() method (the generic is defined in BiocGenerics)
### implements the 'typeof(as.array(x))' semantic. It will work out-of-the-box
### on any array-like object that supports extract_array().
setMethod("type", "ANY",
    function(x)
    {
        x_dim <- dim(x)
        if (is.null(x_dim))
            stop(wmsg("the default type() method only supports ",
                      "array-like objects"))
        type(extract_empty_array(x))
    }
)


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### type() method for DataFrame objects
###

.get_DataFrame_type <- function(x)
{
    ## Make sure that this remains consistent with .extract_DataFrame_slice0()
    ## defined in R/extract_array.R
    x0 <- x[0L, , drop=FALSE]
    df0 <- as.data.frame(x0)
    if (ncol(df0) != ncol(x))
        stop(wmsg("the type() method for DataFrame objects only suports ",
                  "objects for which 'as.data.frame(x)' preserves the ",
                  "number of columns"))
    type(df0)
}

### Equivalent to 'typeof(as.matrix(as.data.frame(x)))' (granted
### that 'as.data.frame(x)' preserves the number of columns) but with an
### almost-zero memory footprint (it avoids the cost of turning 'x' first
### into a data frame then into a matrix).
setMethod("type", "DataFrame", .get_DataFrame_type)