| 1 |
### demo(tkdensity) ## is at |
| 2 |
### /u/maechler/R/D/r-devel/Linux-inst/library/tcltk/demo/tkdensity.R |
| 3 |
|
| 4 |
tkdensity <- function(y, n = 1024, log.bw = TRUE, showvalue = TRUE, |
| 5 |
xlim = NULL, do.rug = size < 1000, kernels = NULL, |
| 6 |
from.f = if(log.bw) -2 else 1/1000, |
| 7 |
to.f = if(log.bw) +2.2 else 2, col = 2) |
| 8 |
{ |
| 9 |
## Purpose: as density() but with scrollbar - bandwidth selection |
| 10 |
## ----------------------------------------------------------------------- |
| 11 |
## Author: Martin Maechler, Date: 8 Nov 2000, 19:00 |
| 12 |
|
| 13 |
requireNamespace("tcltk") || stop("tcltk support is absent") |
| 14 |
tclVar <- tcltk::tclVar |
| 15 |
tclvalue <- tcltk::tclvalue |
| 16 |
tkframe <- tcltk::tkframe |
| 17 |
tkpack <- tcltk::tkpack |
| 18 |
tklabel <- tcltk::tklabel |
| 19 |
tkscale <- tcltk::tkscale |
| 20 |
nbw <- xZ <- xM <- NA_real_ # so '<<-' keeps them here |
| 21 |
|
| 22 |
dFun <- density.default |
| 23 |
all.kerns <- eval(formals(dFun)$kernel) |
| 24 |
kernels <- |
| 25 |
if(is.null(kernels)) all.kerns |
| 26 |
else match.arg(kernels, all.kerns, several.ok = TRUE) |
| 27 |
ynam <- deparse(substitute(y)) |
| 28 |
size <- length(y) |
| 29 |
sd.y <- sqrt(var(y)) |
| 30 |
|
| 31 |
## Use Silverman's Rule of Thumb initially : |
| 32 |
hi <- sd.y |
| 33 |
if ((lo <- min(hi, IQR(y)/1.34)) == 0) |
| 34 |
(lo <- hi) || (lo <- abs(y[1])) || (lo <- 1) |
| 35 |
bw <- bw0 <- 0.9 * lo * size^(-0.2) |
| 36 |
if(log.bw) lbw <- lbw0 <- log10(bw0) |
| 37 |
|
| 38 |
ry <- range(y) |
| 39 |
xlim <- if(is.null(xlim)) ry + c(-2,2)* bw0 else as.numeric(xlim) |
| 40 |
xlmid <- xm0 <- mean(xlim) |
| 41 |
xr0 <- diff(xlim) |
| 42 |
|
| 43 |
## Initialize Tcl variables: |
| 44 |
|
| 45 |
xZoom <- tclVar(100)# % |
| 46 |
xlmid <- tclVar(xlmid) |
| 47 |
|
| 48 |
if(log.bw) |
| 49 |
Lbw <- tclVar(log10(bw)) |
| 50 |
else |
| 51 |
bw <- tclVar(bw) |
| 52 |
|
| 53 |
kernel <- tclVar("gaussian") |
| 54 |
|
| 55 |
## Tvar <- function(v) as.numeric(tclvalue(substitute(v))) |
| 56 |
|
| 57 |
replot <- function(...) { |
| 58 |
if (is.null(y)) return() # too early... |
| 59 |
|
| 60 |
b <- if(log.bw) 10 ^ (lbw <<- as.numeric(tclvalue(Lbw))) else |
| 61 |
nbw <<- as.numeric(tclvalue(bw)) |
| 62 |
##Dbg cat("b = ", formatC(b),"\n") |
| 63 |
k <- tclvalue(kernel) # *is* char |
| 64 |
##Dbg cat("tclvalue(kernel)"); str(k) |
| 65 |
|
| 66 |
xZ <<- as.numeric(tclvalue(xZoom)) |
| 67 |
xM <<- as.numeric(tclvalue(xlmid)) |
| 68 |
|
| 69 |
## "codetools, please do believe that we do use 'b', 'k', 'xlim' !": |
| 70 |
if(0 > 1) |
| 71 |
b <- xlim + b + k |
| 72 |
xr.half <- (xr0 / 2) * 100 / xZ |
| 73 |
xlim <- xM + c(-xr.half, xr.half) |
| 74 |
eval(substitute(plot(density(y, bw = b, kernel = k, n = n), |
| 75 |
main = paste("density(",ynam, |
| 76 |
", bw = ",format(b, dig = 3), |
| 77 |
", kernel = \"", k, "\")", sep=""), |
| 78 |
xlim = xlim, col = col))) |
| 79 |
if(do.rug) rug(y) ## points(y,rep(0,size), col = 3) |
| 80 |
} |
| 81 |
|
| 82 |
replot.maybe <- function(...) |
| 83 |
if ((log.bw && !identical(lbw, as.numeric(tclvalue(Lbw)))) || |
| 84 |
(!log.bw && !identical(nbw, as.numeric(tclvalue(bw)))) || |
| 85 |
!identical(xZ, as.numeric(tclvalue(xZoom))) || |
| 86 |
!identical(xM, as.numeric(tclvalue(xlmid))) |
| 87 |
) |
| 88 |
replot() |
| 89 |
|
| 90 |
base <- tcltk::tktoplevel() |
| 91 |
tcltk::tkwm.title(base, paste("Tk Density(",ynam,")")) |
| 92 |
|
| 93 |
base.frame <- tkframe(base, borderwidth = 2) |
| 94 |
bw.frame <- tkframe(base.frame, relief = "groove", borderwidth = 3) |
| 95 |
kern.frame <- tkframe(base.frame, relief = "groove", borderwidth = 2) |
| 96 |
|
| 97 |
x.frame <- tkframe(base.frame) |
| 98 |
xr.frame <- tkframe(x.frame) |
| 99 |
xmid.frame <- tkframe(x.frame) |
| 100 |
tkpack(xr.frame, xmid.frame, side = "left", anchor = "s") |
| 101 |
|
| 102 |
q.but <- tcltk::tkbutton(base, text = "Quit", command = function() { |
| 103 |
par(op) ## see par() below ! |
| 104 |
tcltk::tkdestroy(base) }) |
| 105 |
tkpack(base.frame, |
| 106 |
bw.frame, kern.frame, |
| 107 |
x.frame, |
| 108 |
q.but) |
| 109 |
|
| 110 |
## Bandwith Frame : |
| 111 |
tkpack(tklabel (bw.frame, |
| 112 |
text = if(log.bw)"log10(Bandwidth)" else "Bandwidth")) |
| 113 |
tkpack(tkscale (bw.frame, command = replot.maybe, |
| 114 |
from = if(log.bw) lbw0 + (from.f) else bw0 * from.f, |
| 115 |
to = if(log.bw) lbw0 + (to.f) else bw0 * to.f, |
| 116 |
showvalue = showvalue, |
| 117 |
variable = if(log.bw) Lbw else bw, |
| 118 |
resolution = if(log.bw) lbw0/20 else bw0/4 * from.f, |
| 119 |
length = 200, |
| 120 |
orient = "horiz")) |
| 121 |
|
| 122 |
## Kernel Frame : |
| 123 |
tkpack(tklabel(kern.frame, text = "Kernel")) |
| 124 |
for (k.name in kernels) |
| 125 |
tkpack(tcltk::tkradiobutton(kern.frame, command = replot, |
| 126 |
text = k.name, value = k.name, variable=kernel), |
| 127 |
anchor = "w") |
| 128 |
|
| 129 |
## [x Zoom] Frame : |
| 130 |
tkpack(tklabel (xr.frame, text = "x zoom [%]")) |
| 131 |
tkpack(tkscale (xr.frame, command = replot.maybe, |
| 132 |
from = 5,# = 1/20 |
| 133 |
to = 500,# = * 5 |
| 134 |
showvalue = TRUE, variable = xZoom, |
| 135 |
length = 80, orient = "horiz")) |
| 136 |
|
| 137 |
## [x Pan] Frame : |
| 138 |
tkpack(tklabel (xmid.frame, text = "x pan")) |
| 139 |
tkpack(tkscale (xmid.frame, command = replot.maybe, |
| 140 |
from = xm0 - xr0, |
| 141 |
to = xm0 + xr0, |
| 142 |
showvalue = FALSE, variable = xlmid, |
| 143 |
resolution = xr0/2000, |
| 144 |
length = 80, orient = "horiz")) |
| 145 |
|
| 146 |
|
| 147 |
if((op <- par("ask")) || prod(par("mfrow")) > 1) |
| 148 |
op <- par(ask = FALSE, mfrow = c(1,1)) |
| 149 |
## on.exit(par(op)) is *NOT* sufficient; do it only when quitting tk !! |
| 150 |
|
| 151 |
##Dbg cat("Before calling `replot()' : \n") |
| 152 |
replot() |
| 153 |
|
| 154 |
## Returning doesn't work!! |
| 155 |
##return(tclvar[c("bw", "kernel")]) |
| 156 |
} |
| 157 |
|
| 158 |
###--- |
| 159 |
|
| 160 |
## tkpack() : |
| 161 |
##- .Tcl(.Tcl.args(...)) : |
| 162 |
##- [tcl] unknown or ambiguous option "": must be \ |
| 163 |
## -after, -anchor, -before, -expand, -fill, -in, |
| 164 |
## -ipadx, -ipady, -padx, -pady, or -side. |