Displaying data in a pop up window (table widget) using tcltk in R - why is it dropping the last row of data?

亡梦爱人 提交于 2019-12-21 20:38:51

问题


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!


回答1:


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:



来源:https://stackoverflow.com/questions/32724775/displaying-data-in-a-pop-up-window-table-widget-using-tcltk-in-r-why-is-it-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!