我正在努力创建一个小部件来使用 tcl/tk 在弹出窗口中查看表格。我正在遵循这个例子:http://r.789695.n4.nabble.com/Tck-tk-help-td1837711.html http://r.789695.n4.nabble.com/Tck-tk-help-td1837711.html
但是,当我使用数据运行脚本时,最后一行数据不包含在数组中。这是汽车数据的示例(表不显示行名称并不重要):
require(tcltk)
tclRequire("Tktable")
toTclArray<-function(dsn,dig=2) {
# http://r.789695.n4.nabble.com/Tck-tk-help-td1837711.html
# Converts Data Frame/Matrix to a Tcl Array for Use in Displaying Tables
# dsn is the data set name
# dig is the number of digits to round to
require(tcltk)
tclarray1<-tclArray()
for (i in 0:(dim(dsn)[1])) {
for (j in 0:(dim(dsn)[2]-1)) {
# First Row is Set to Column Names to be Used as Labels
if (i==0) {
tclarray1[[i,j]]<-colnames(dsn)[j+1]
} else {
tem<-dsn[i,j+1]
tclarray1[[i,j]]<-ifelse(is.na(tem),".",
ifelse(is.numeric(tem),round(tem,digits=dig),
as.character(tem)))
}
}
}
return (tclarray1)
}
temptable <- toTclArray(mtcars)
tt<-tktoplevel()
table1 <- tkwidget(tt,"table",variable=temptable,rows=dim(mtcars)[1],
cols=dim(mtcars)[2],titlerows=1,selectmode="extended",colwidth=10)
tkgrid(table1, pady = 20, padx = 30)
据我了解,在原始数据集中,第一行数据是第 1 行,第一列数据是第 1 列。在数组中,标题是第 0 行,第一行数据是第 1 行,第一列是数据为零列。在 for 循环中,它从行 (i) 0 到 32,列 (j) 0 到 10。这是有道理的,因为列从 1-11 移动到 0-10,但行不需要更改因为仍然有 32 行数据。因此,我无法弄清楚需要编辑代码中的哪些内容才能将最后一行数据添加到表中。
(我确信有一个很棒的包来运行此任务,但我正在为工作中的项目创建一个界面,并且我被指示不要使用任何不随基本 R 一起提供的包)
任何帮助深表感谢。谢谢你!