tkrplot
tkrplotとは、Tk ウィジェットの中にRグラフィックを配置するライブラリである。
library(tcltk)
library(tkrplot)
tt <- tktoplevel()
bb <- 1
img <- tkrplot(tt, function() plot(1:20,(1:20)^bb))
f <- function(...) {
b <- as.numeric(tclvalue("bb"))
if (b != bb) {
bb <<- b
tkrreplot(img)
}
}
a <- tkscale(tt, command=f, from=0.05, to=2.00, variable="bb",
showvalue=FALSE, resolution=0.05, orient="horiz")
tkpack(img,a)
結果
例えば、このようにマウスでスクロールして対話的にグラフを表示するようなものを作成できる。
library(tkrplot)
tt <- tktoplevel()
df <- 1
td <- function() {
curve(dt(x, df), from = -4, to = 4, xlab = "x",
ylab = "density", main = "t distribution",
ylim = c(0, 0.4))
}
img <- tkrplot(tt, td)
f <- function(...){
d <- as.numeric(tclvalue("df"))
if(d != df){
df <<- d
tkrreplot(img)
}
}
s <- tkscale(tt, command = f, from = 1, to = 20, label="自由度",
variable = "df",
showvalue = TRUE, resolution = 1, orient = "horiz")
tkpack(img, s)
こちらの環境ではtkscale()は文字化けしないが、tkrplot()のxlabやylabは文字化けする。fontの指定などはできない模様。
結果