[R] calling a function with dynamically generated buttons

Anne Skoeries home at anne-skoeries.de
Wed Aug 12 14:47:27 CEST 2009


Hallo,

I'm dynamically generating buttons depending on the number of rows of  
my dataframe. Every button is supposed to call a function which  
generates a plot with the values of one of my dataframe rows.

My code looks like this:

base <- tktoplevel()

plotten <- function(mat, namen, titel) {
         midpts <- barplot(height=mat, names.arg = namen, main =  
titel, las=2)
         text(midpts, 1, mat)
         mtext(text=paste("Treshold:", thresh), side=3, col="blue")
       }

  lb <- tklabel(base, text="Barplot:")
  tkgrid(lb, row=4, column=0)

  for(i in 1:(reihen-1)) {
         anzeige <- data.matrix(dataframe[i,-c(spalten)])
         namen <- names(anzeige)
         tit <- paste(classi[i], "\nTotal Threshold for", classi[i],  
":", dataframe[i, spalten])

         but <- tkbutton(base, text = classi[i], command = function()  
{plotten(mat = anzeige, namen = namen, titel = tit)})
         tkgrid(but, row=4, column=i, sticky="e")
}

The buttons are all displayed correctly in my window, but after  
pressing one, it always generates a plot with the last parameters. So,  
if I've generated 3 buttons and choose button1, it generates a plot  
with the parameters of button3. The same plot after pressing button2  
or button3.

How can I make sure, that each button calls the function with it's  
"own" parameters? So that button1 calls the function with the first  
row of my dataframe as the function parameter, button2 with the second  
row and so on?

Thanks in advance,

--
Anne Skoeries




More information about the R-help mailing list