[R] calling a function with dynamically generated buttons

jim holtman jholtman at gmail.com
Wed Aug 12 16:04:47 CEST 2009


Untested, but try something like this:

 for(i in 1:(reihen-1)) {

       but <- tkbutton(base, text = classi[i], command = local({
                       anzeige <- data.matrix(dataframe[i,-c(spalten)])
                        namen <- names(anzeige)
                        tit <- paste(classi[i], "\nTotal Threshold
for", classi[i], ":", dataframe[i, spalten])
                       function() {plotten(mat = anzeige, namen =
namen, titel = tit)}
                   }))
       tkgrid(but, row=4, column=i, sticky="e")
}

On Wed, Aug 12, 2009 at 8:47 AM, Anne Skoeries<home at anne-skoeries.de> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list