在 R 中使用 tcltk 在弹出窗口(表格小部件)中显示数据 - 为什么它会删除最后一行数据? [英] Displaying data in a pop up window (table widget) using tcltk in R - why is it dropping the last row of data?

查看:29
本文介绍了在 R 中使用 tcltk 在弹出窗口(表格小部件)中显示数据 - 为什么它会删除最后一行数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建一个小部件以使用 tcl/tk 在弹出窗口中查看表格.我正在关注这个例子:

I'm struggling with creating a widget to view a table in a pop up window using tcl/tk. I'm following this example: http://r.789695.n4.nabble.com/Tck-tk-help-td1837711.html

But when I run the script with my data the last row of data is not included in the array. Here is an example with the cars data (it doesn't matter that the table does not show the row names):

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)

From what I understand, in the original dataset the first row of data is row 1 and the first column of data is column 1. In the array, the header is row zero, the first row of data is row 1, and the first column of data is column zero. In the for loop it goes from row (i) 0 to 32 and column (j) 0 to 10. This makes sense since the columns are being shifted from 1-11 to 0-10, but the rows shouldn't need to change since there will still be 32 rows of data. So I can't figure out what in the code needs to be edited in order to add the last row of data to the table.

(I'm sure there's a great package to run this task, but I'm creating an interface for a project at work and I've been directed not to use any packages that don't come with base R)

Any help is much appreciated. Thank you!

解决方案

This is a pretty simple fix. Your function looks good, but your widget setup is forgetting to account for the header row when allocating rows to the tk table. Just add a +1 and you should be good:

tt<-tktoplevel()
table1 <- tkwidget(tt,"table",variable=temptable,rows=dim(mtcars)[1]+1,
                  cols=dim(mtcars)[2],titlerows=1,selectmode="extended",colwidth=10)
tkgrid(table1, pady = 20, padx = 30)

Result:

这篇关于在 R 中使用 tcltk 在弹出窗口(表格小部件)中显示数据 - 为什么它会删除最后一行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆