[R] [tcltk][tktable] How to make an efficient data transfer from R to Tcl?

Cleber N.Borges klebyn at yahoo.com.br
Wed Dec 9 01:37:42 CET 2015


hello all,
I intend transfer a big data.frame, more than 1e4 rows, more than 100 
columns...
I found solutions (in internet and help pages) for small data.frame like 
the showed bellow.
Big data.frames is very expensive in computation time in my approach
I would like to optimize this transfer anyway ...
I saw a function named "as.tclObj" that seems to me a promising form but 
hard to code...
Somebody know a way more clever to do it?

thanks for tips
cleber

##########################################
library( tcltk )
nrow <- 10 ; ncol <- 100
# simulated data: data is originally a data frame
DF <- as.data.frame(matrix(round(runif(nrow*ncol),3),nr=nrow,nc=ncol))
######
# DF_tcl <- as.tclObj( as.matrix( DF ) )

tableDataTclArray <- tclArray()
for( i in 1:nrow( DF ) )
     tableDataTclArray[[ i, 0 ]] <- .Tcl( noquote( paste('format "', 
rownames( DF )[ i ] , '"') ) )

for( i in 1:ncol( DF ) )
     tableDataTclArray[[ 0, i ]] <- .Tcl( noquote( paste('format "', 
colnames( DF )[ i ] , '"') ) )


for( i in 1:nrow( DF ) )
     for( j in 1:ncol( DF ) )
         tableDataTclArray[[ i, j ]] <- DF[i,j]
######





##############################################
##############################################
##############################################
# my full code:
###############
library( tcltk ); tclRequire("Tktable")
wDataOrg <- tktoplevel()
tcl('wm', 'geometry',  wDataOrg, "1024x600+175+80" )
fmTableData <- ttklabelframe( wDataOrg, text='  Data  ', padding=c( 8, 
5, 8, 15 ) )
height <- -1; width <- -1
nrowDefault <- 1e4+1; ncolDefault <- 100+1
tableDataTclArray <- tclArray()
tableData <- tkwidget( fmTableData, "table", rows=nrowDefault, 
cols=ncolDefault,
                      height=height+1,width=width+1,
                      xscrollcommand=function(...) tkset( scrX, ... ),
                      yscrollcommand=function(...) tkset( scrY, ... ),
                      rowstretchmode='unset', colstretchmode='unset', 
multiline=0, cache=1,
                      background="white", selectmode="extended", 
selecttitle=0,
                      font='{Arial} 10', ipadx=5, ipady=5, anchor='e',
                      borderwidth=c( 0, 1, 0, 1 ), drawmode='slow',
                      variable=tableDataTclArray )
tcl( tableData, "tag", "celltag", "ZeroZero", "0,0" )
tcl( tableData, "tag", "rowtag",  "rowtitle", "0" )
tcl( tableData, "tag", "coltag",  "coltitle", "0" )
tcl( tableData, "tag", "configure", "ZeroZero", bg='SystemButtonFace', 
relief="raised", state='disabled' )
tcl( tableData, "tag", "configure", "rowtitle", bg='lightgreen', 
relief='raised', borderwidth=c( 0, 1, 0, 1 ), anchor='center' )
tcl( tableData, "tag", "configure", "coltitle", bg='lightgreen', 
relief='raised', borderwidth=c( 0, 1, 0, 1 ), anchor='w' )
tcl( tableData, "tag", "configure", "active", fg='yellow', bg='red' )
tcl( tableData, "width", "0", "20" )
scrX <- tkscrollbar( fmTableData, orient="horizontal", 
command=function(...) tkxview( tableData, ... ) )
scrY <- tkscrollbar( fmTableData, orient="vertical", 
command=function(...) tkyview( tableData, ... ) )
scrYwidth <- as.integer( tkcget( scrY, width = NULL ) )
tcl( "pack", scrY, side = "right",  fill = "y", expand = FALSE, pady = 
c(0, scrYwidth + 2) )
tcl( "pack", scrX, side = "bottom", fill = "x", expand = FALSE )
tcl( "pack", tableData, anchor='n', side="right", expand=TRUE, 
fill="both"  )
tcl( "pack", fmTableData, fill="both", expand = TRUE, padx=10, pady=10 )
##########################################  expensive time computation 
[[elided Yahoo spam]]
DF <- as.data.frame( matrix( round(runif(1e4 * 100),3), nr=1e4, nc=100 ) 
) #### simulated data
for( i in 1:nrow( DF ) ){
     tableDataTclArray[[ i, 0 ]] <- .Tcl( noquote( paste('format "', 
rownames( DF )[ i ] , '"') ) )
     }
for( i in 1:ncol( DF ) ){
     tableDataTclArray[[ 0, i ]] <- .Tcl( noquote( paste('format "', 
colnames( DF )[ i ] , '"') ) )
     }
for( i in 1:nrow( DF ) ) {
     for( j in 1:ncol( DF ) ){
         tableDataTclArray[[ i, j ]] <- DF[i,j]
     }
}
#


---
Este email foi escaneado pelo Avast antivírus.
https://www.avast.com/antivirus



More information about the R-help mailing list