[R] Open directory within a menu in tcltk

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Aug 19 09:37:58 CEST 2008


Judith Flores wrote:
> Hello,
>
>     I am trying to setup a menu to open files and directories. I have the following code:
>
>
>
>
> opendir<-function() {
>   dirname<<-tclvalue(tkchooseDirectory())
> }
>
> openfile<-function() {
>   filename<<-tclvalue(tkgetOpenFile())
> }
>
> require(tcltk)
> t1<-tktoplevel()
> topMenu<-tkmenu(t1)
> tkconfigure(t1,menu=topMenu)
>
> plotMenu<-tkmenu(topMenu, tearoff=FALSE)
>
> tkadd(plotMenu,"command",label="Open file", command=openfile)
> tkadd(plotMenu, "command",label="Select directory",command=opendir)
> tkadd(plotMenu,"command",label="Quit", command=function() tkdestroy(t1))
> tkadd(topMenu, "cascade", label="Menu",menu=plotMenu) 
>
> tkfocus(t1)
>
>   I can extract a file, but not a directory, I obtain the following error message:
>
> Error in function ():
> cannot change value of locked binding for 'dirname'.
>
>   I would appreciate very much any help or explanation on this.
>   

This is mainly the effect of an unfortunate choice of variable name. 
Beware the semantics of "<<-". You need to ensure that you have control 
of where the assignment goes to.

It is in general unfortunate to make such assignments to the global 
environment, where they can mask and clobber other objects. I'd consider 
a redesign along the lines of

myGui <- function(....) {
    opendir<-function() {
        dirname<<-tclvalue(tkchooseDirectory())
   }
    dirname <- NA
    tl <- tktoplevel()
   ......
}


> As always, thank you,
>
> Judith
>
> ______________________________________________
> 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.
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list