~ubuntu-branches/ubuntu/oneiric/latticeextra/oneiric

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
## update elements of a list recursively. 

updateList <-
    function (x, val)
{
    if (is.null(x))
        x <- list()
    modifyList(x, val)
}


## common operations that only make sense in certain contexts


useOuterStrips <-
    function(x,
             strip = strip.default,
             strip.left = strip.custom(horizontal = FALSE),
             strip.lines = 1,
             strip.left.lines = strip.lines)
{
    dimx <- dim(x)
    stopifnot(inherits(x, "trellis"))
    stopifnot(length(dimx) == 2)
    opar <- if (is.null(x$par.settings)) list() else x$par.settings
    par.settings <-
        modifyList(opar,
                   list(layout.heights =
                        if (x$as.table) list(strip = c(strip.lines, rep(0, dimx[2]-1)))
                        else list(strip = c(rep(0, dimx[2]-1), strip.lines)),
                        layout.widths =
                        list(strip.left = c(strip.left.lines, rep(0, dimx[1]-1)))))
    if (is.character(strip))
        strip <- get(strip)
    if (is.logical(strip) && strip)
        strip <- strip.default
    new.strip <-
        if (is.function(strip))
        {
            function(which.given, which.panel, var.name, ...) {
                if (which.given == 1)
                    strip(which.given = 1,
                          which.panel = which.panel[1],
                          var.name = var.name[1],
                          ...)
            }
        }
        else strip
    if (is.character(strip.left))
        strip.left <- get(strip.left)
    if (is.logical(strip.left) && strip.left)
        strip.left <- strip.custom(horizontal = FALSE)
    new.strip.left <-
        if (is.function(strip.left))
        {
            function(which.given, which.panel, var.name, ...) {
                if (which.given == 2)
                    strip.left(which.given = 1,
                               which.panel = which.panel[2],
                               var.name = var.name[2],
                               ...)
            }
        }
        else strip.left
    update(x,
           par.settings = par.settings,
           strip = new.strip,
           strip.left = new.strip.left,
           par.strip.text = list(lines = 0.5),
           layout = dimx)
}




resizePanels <-
    function(x, h = 1, w = 1)
{
    if (!missing(x))
        return(update(x,
                      par.settings =
                      list(layout.heights = list(panel = h),
                           layout.widths = list(panel = w))))

    cl <- trellis.currentLayout()
    if (all(dim(cl) > 1))
        stop("layout must have single column or single row.")
    if (all(dim(cl) == 1))
    {
        message("Nothing to be done.")
        return()
    }
    if (any(cl == 0)) stop("missing panels not allowed")
    if (dim(cl)[2] == 1) ## single column
    {
        pos <- seq(length = dim(cl)[1])
        heights <- 
            sapply(pos,
                   function(i) {
                       trellis.focus("panel", 1, i, highlight = FALSE)
                       ylim <- current.panel.limits()$ylim
                       trellis.unfocus()
                       diff(range(ylim))
                   })
        return(trellis.last.object(par.settings =
                                   list(layout.heights = list(panel = heights))))
    }
    else if (dim(cl)[1] == 1) ## single row
    {
        pos <- seq(length = dim(cl)[2])
        widths <- 
            sapply(pos,
                   function(i) {
                       trellis.focus("panel", i, 1, highlight = FALSE)
                       xlim <- current.panel.limits()$xlim
                       trellis.unfocus()
                       diff(range(xlim))
                   })
        return(trellis.last.object(par.settings =
                                   list(layout.widths = list(panel = widths))))
    }
    print(dim(cl))
    stop("shouldn't come here")
}




## utility functions to extract components of a formula.  Don't work
## reliably with unusual symbols

.responseName <- function(formula)
{
    if (length(formula) == 3) as.character(formula[2])
    else stop("invalid formula")
}

.covariateName <- function(formula)
{
    RHS <- 
        if (length(formula) == 3) as.character(formula[3])
        else if (length(formula) == 2) as.character(formula[2])
        else stop("invalid formula")
    RHS <- strsplit(RHS, " | ", fixed = TRUE)[[1]]
    RHS[1]
}

.groupsName <- function(formula)
{
    RHS <- 
        if (length(formula) == 3) as.character(formula[3])
        else if (length(formula) == 2) as.character(formula[2])
        else stop("invalid formula")
    RHS <- strsplit(RHS, " | ", fixed = TRUE)[[1]]
    RHS[2]
}