Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

tcl

tk-toolkit

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!

like image 249
LM6 Avatar asked Oct 26 '25 22:10

LM6


1 Answers

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:

enter image description here

like image 105
Thomas Avatar answered Oct 28 '25 13:10

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!