[R] Trying tkscript, more info

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Jul 25 00:11:54 CEST 2002


Kenneth Cabrera <krcabrer at perseus.unalmed.edu.co> writes:

> Kenneth Cabrera wrote:
> 
> > I am trying the function tkscript, that apears in "A primer on the
> > R-Tcl/Tk Package" on
> > The Newsletter of the R Project of September 2001.
> >
> > But I am a newbee and I don´t know how to handle some problems.
> >
> > 1. When I "Load" a file, such a file appears without newline (All
> > the text in a line) and
> >    only the first and the last line.
> > 2. When I "Run" a ? symbol appears and I don't know what to do there.
> > 3. If I type any letter and press return then this message appears:
> > Executing from script window:
> > -----
> > Error in cat(list(...), file, sep, fill, labels, append) :
> >        argument 3 not yet handled by cat
> >

1. might be a Windows/Unix difference, but see below. It doesn't
   happen to me, and I suspect that it might depend on the format of
   the file you're trying to load. Can you read the same file in
   Notepad?

2. is making you a victim of some changes that were made in R 1.5.0:
   All return values are now objects of class "tclObj" and you need to
   take tclvalue() of them to access them, or use as.character(). 
   You get one of these objects returned into "code" and for some odd
   reason, parse() decides to parse off of stdin when it gets a text
   argument that isn't of mode "character". 

3. is a consequence of the same thing: parse() sits waiting for you to
   type an expression on the input line, and only then does cat()
   realize that it doesn't handle the "code" argument that it got.

This doesn't affect the load/save routines as much, because tclObj's
get automatically converted with as.character when passed to tkcmd()
and friends, but there are still some cases where you need to use
tclvalue() explicitly, notably around tkget().

> > The file I try to use with the tkscript function is attached too
> >
> > Thank you for your help
> >
> > Kenneth Cabrera
> >
> >I am using W2K and R 1.5.1 version and ActiveState Active Tcl version 8.3.4.3
> >------------------------------------------------------------------------
> >
> >require(tcltk) || stop("tcltk support is absent")
> >
> >tkscript <- function() {
> > wfile <- ""
> > tt<-tktoplevel()
> > txt<-tktext(tt,height=10)
> > tkpack(txt)
> > save<-function() {
> >   file <- tkcmd("tk_getSaveFile",
> >     initialfile=tkcmd("file","tail",wfile),
> >     initialdir=tkcmd("file","dirname",wfile))
> >   if (!length(file)) return()
> >   chn<-tkcmd("open",file,"w")
> >   tkcmd("puts",chn,tkget(txt,"0.0","end"))
> >   tkcmd("close",chn)
> >   wfile <<-file
> > }
> > load <-function() {
> >   file <-tkcmd("tk_getOpenFile")
> >   if (!length(file)) return()
> >   chn<-tkcmd("open",file,"r")
> >   tkinsert(txt,"0.0",tkcmd("read",chn))
> >   tkcmd("close",chn)
> >   wfile <<-file
> > }
> > run <- function() {
> >   code<-tkget(txt,"0.0","end")
> >   e<-try(parse(text=code))
> >   if (inherits(e,"try-error")) {
> >     tkcmd("tk_messageBox",message="Syntax error",icon="error")
> >     return()
> >   }
> >   cat("Executing from script window:","-----",code,"result:",sep="\n")
> >   print(eval(e))
> > }
> > topMenu <- tkmenu(tt)
> > tkconfigure(tt,menu=topMenu)
> > fileMenu <- tkmenu(topMenu, tearoff=FALSE)
> > tkadd(fileMenu,"command",label="Load",command=load)
> > tkadd(fileMenu,"command",label="Save",command=save)
> > tkadd(topMenu,"cascade",label="File",menu=fileMenu)
> > tkadd(topMenu,"command",label="Run",command=run)
> >}
> >
> >
> >------------------------------------------------------------------------
> >
> >x<-seq(1,10,length=200)
> >y<-x^2
> >plot(x,y)
> >

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list