Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

r - Get data out of a tcltk function

This is probably so simple I will cringe when the answer comes back but I am totally stumped. I have tried the manuals, tried searching the web, assorted examples and anything else I can think of. I am still stuck.

I am trying to create a simple input for the user to add two values I can then use in the rest of the R script. I need the script to pause and wait for the input from the user and then continue along once it gets the input (like how the choose file function works). AFter reading a bunch of stuff I decided to use library(tcltk). I have a nice little box within a function.

inputs <- function(){

   xvar <- tclVar("")
   yvar <- tclVar("")

   tt <- tktoplevel()
   tkwm.title(tt,"Input Numbers")
   x.entry <- tkentry(tt, textvariable=xvar)
   y.entry <- tkentry(tt, textvariable=yvar)

   reset <- function()
    {
     tclvalue(xvar)<-""
     tclvalue(yvar)<-""
    }

   reset.but <- tkbutton(tt, text="Reset", command=reset)

   submit <- function() {
     x <- as.numeric(tclvalue(xvar))
     y <- as.numeric(tclvalue(yvar))
     print(x)
     print(y)
     tkdestroy(tt)
   }
   submit.but <- tkbutton(tt, text="submit", command=submit)

   tkgrid(tklabel(tt,text="Enter Two Inputs"),columnspan=2)
   tkgrid(tklabel(tt,text="Input1"), x.entry, pady = 10, padx =10)
   tkgrid(tklabel(tt,text="Input2"), y.entry, pady = 10, padx =10)
   tkgrid(submit.but, reset.but)

  }

When I type in:

 inputs()

The nice little box pops up and I can input my values, say 3 and 4 for this example.

I get back

<Tcl>  
[1] 3
[1] 4

I want to use those number in a subsequent part of the R code. How do I get them so I can get the equivalent of this?

input1 <- 3
input2 <- 4

Thanks in advance for helping.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a modification of your function:

inputs <- function(){

   xvar <- tclVar("")
   yvar <- tclVar("")

   tt <- tktoplevel()
   tkwm.title(tt,"Input Numbers")
   x.entry <- tkentry(tt, textvariable=xvar)
   y.entry <- tkentry(tt, textvariable=yvar)

   reset <- function()
    {
     tclvalue(xvar)<-""
     tclvalue(yvar)<-""
    }

   reset.but <- tkbutton(tt, text="Reset", command=reset)

   submit <- function() {
     x <- as.numeric(tclvalue(xvar))
     y <- as.numeric(tclvalue(yvar))
     e <- parent.env(environment())
     e$x <- x
     e$y <- y
     tkdestroy(tt)
   }
   submit.but <- tkbutton(tt, text="submit", command=submit)

   tkgrid(tklabel(tt,text="Enter Two Inputs"),columnspan=2)
   tkgrid(tklabel(tt,text="Input1"), x.entry, pady = 10, padx =10)
   tkgrid(tklabel(tt,text="Input2"), y.entry, pady = 10, padx =10)
   tkgrid(submit.but, reset.but)

  tkwait.window(tt)
  return(c(x,y))
}

Now run the function like:

myvals <- inputs()

Now enter your 2 values and click "Submit", then look at the myvals variable, it contains your 2 values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...