3

I've been trying to get his to work all day, but I can't find a simple explanation on how this works. I have a vector with 3 items, c(file1, file2, file3), I want to be able to toggle the checkbox on and off, when its on I want the first item c(file1) to be in the vector, however when its off I want it to be deleted from the vector. This is my attempt at it, but I don't know why its not working:

library(gWidgetRGtk2)
GraphFiles <- FileNamesOrig

w <- gwindow("Tag Density Checkboxes")
g <- ggroup(container = w)
lyt <- glayout(cont = g, horizontal = T)

gcheckbox(FileNamesOrig[1], container=g, checked=TRUE, handler=function(h,...){  
  if(!svalue(h$obj)){GraphFiles[[1]] <- NA}  else {GraphFiles[[1]] <-FileNamesOrig[1]}
})

While we're all here, I was thinking of doing:

for(i in 1:No.file)
gcheckbox(GraphFiles[i], container=g, handler=function(h,...){
  })

No.file = 3

First, how would I get the check-boxes on a separate line within the GUI window. And second how would I incorporate the functionality of the first piece of code into the loop? (the problem is the number of input will differ each run of the script)

Gago-Silva
  • 1,873
  • 4
  • 22
  • 46
crysis405
  • 1,121
  • 2
  • 13
  • 26
  • @You want all the checkbox in single Line? and once you remove the file from the list how you would include it ? – agstudy Jan 07 '13 at 17:47

1 Answers1

2

You can use [, see ?ggwidget

The "[" method returns the label on the box.

then I change your handler function:

 handler=function(h,...){  
         browser()
         label <- h$obj[]
         if(!svalue(h$obj)) ## I remove the item
            GraphFiles <<- GraphFiles[GraphFiles!=label]   ## note the use of the global 
                                                              operator
         else               ## I add the item
            GraphFiles  <<- c(GraphFiles,FileNamesOrig[FileNamesOrig==label])

 })

finally you call this in loop:

library(gWidgetsRGtk2)
FileNamesOrig <- paste('file',1:3,sep='')
GraphFiles <- FileNamesOrig
w <- gwindow("Tag Density Checkboxes")
g <- ggroup(container = w)
lyt <- glayout(cont = g, horizontal = T)
for(x in seq_along(FileNamesOrig)){
  gcheckbox(FileNamesOrig[x], 
            container=g, 
            checked=TRUE, 
            handler=function(h,...){  
                  label <- h$obj[]
                  if(!svalue(h$obj)) GraphFiles <<- GraphFiles[GraphFiles!=label]
                  else GraphFiles  <<- c(GraphFiles,FileNamesOrig[FileNamesOrig==label])
                 print(GraphFiles) ## edit to show the changes
            })
}
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Are you sure this is working? If yes, I guess I have very little idea as to how this is supposed to work. When you uncheck a checkbox is it going to remove the item from the list and then save the list? Because I don't see the list changing. – crysis405 Jan 08 '13 at 08:59
  • @user1872432 I am pretty sure. you can't check it visually.When you unchek an element `GraphFiles[GraphFiles!=label]` is executed. – agstudy Jan 08 '13 at 09:04
  • @user1872432 Now I put `print(GraphFiles)` see my edit to show the changes. I think there is a small mistake. I will correct it if you explain for me the expected result more precisly. – agstudy Jan 08 '13 at 09:09
  • so how would I verify that it works before moving onto the next part? Edit: ok just read your new post, basically I have a list, using the checkbox I want to remove an item from the list if its corresponding checkbox is unchecked, or add it back in if it is rechecked. – crysis405 Jan 08 '13 at 09:09
  • @user1872432 see my last comment. – agstudy Jan 08 '13 at 09:11
  • im unchecking the checkboxes, and then running `print(GraphFiles)` but it keeps giving me all 3 files. Btw the original list of files is created from tk.choose.file after the user chooses some files. – crysis405 Jan 08 '13 at 09:14
  • @user1872432 I update my answer. it is question of global scope change `<-` to `<<-` – agstudy Jan 08 '13 at 09:16