从tkentry()小部件获取R中的值

时间:2015-05-13 10:30:01

标签: r user-controls widget tcl tk

我正在尝试创建一个小型交互式窗口,用户(现在我)可以输入有关功率计算的特定信息(测试名称,组数,数据类型),以便执行正确的功率计算。我知道如何直接在R中执行此操作,但我想创建一个模块供其他人使用,他们对代码和统计信息通常不太感兴趣或不称职,因此他们可以进行一些简单的预测试功率计算以获得更好的测试设计。

我被卡住了,我一直收到类似的错误消息,我并不理解。这是我的代码:

>require(tcltk)
>t3<- tktoplevel()
>tktitle(t3)<-"Statistical power"           
##create buildingblocks
#define appearance of heading, sub, questions
>fontHead<-tkfont.create(family="times",size=16,weight="bold")       
>fontSub<-tkfont.create(family="times",size=14,weight="bold")
>fontQ<-tkfont.create(family="times",size=14)
>fontQ.s<-tkfont.create(family="times",size=12)
>heading.m   <-tklabel(t3,text="This module will give you insight in the statistical power requirements of your test",font=fontHead)
>heading.s   <-tklabel(t3,text="Please answer the following questions:",font=fontSub)
>Q1          <-tklabel(t3,text="What is the name of your test?",font=fontQ)
>filename    <-tclVar("Your test")             # this I have seen at**
>e.Q1        <-tkentry(t3,width=30,textvariable=filename)
>submit.name <-function(){                      # this I have seen at**
>  filename<<- as.character(tclvalue(e.Q1)) # this I have seen at**
>  e <- parent.env(environment())           # this I have seen at**
>  e$filename <- filename  }                # this I have seen at**
>b.Q1.sub    <-tkbutton(t3,text="Submit",command=submit.name) # this I have seen at**      
# creating the widget    
>tkgrid(heading.m,columnspan=6)                 
>tkgrid(tklabel(t3,text="     "))
>tkgrid(heading.s, columnspan=6, sticky="w")
>tkgrid(Q1,row=4,column=0,rowspan=1,columnspan=2,sticky="w")                       
<tkgrid(e.Q1,row=4,column=2,rowspan=1,columnspan=3,sticky="ew")
<tkgrid(b.Q1.sub,row=4,column=5,rowspan=1,columnspan=1,sticky="ew")

**我从Greg Snow对以下问题(Get data out of a tcltk function)的回答中复制了这个解决方案,但不幸的是,这对我不起作用。运行我的代码我收到此错误消息: “结构错误(.External(.C_dotTclObjv,objv),class =”tclObj“):   [tcl]无法读取“.32.4”:没有这样的变量。“

上面是一个例子,我还想要实现的是用户输入组的数量以及数据是按比例还是按比例。这是代码:

   >Q2          <-tklabel(t3,text="How many test-groups do you have?",font=fontQ)
   >groups      <-tclVar("")
   >e.Q2        <-tkentry(t3,textvariable=groups)  # I want to link this answer to buttonclick at Q3 for determining type of test 
   > #groups      <-as.numeric(tclvalue(e.Q2))  # running this produced this message *** 
  >#groups      <-function(){
               as.integer(tclvalue(e.Q2))
              }   ## running this produced this message****
> Q3          <-tklabel(t3,text="What type of differences do you want to test for?",font=fontQ)
 l.Q3.prop   <-tklabel(t3,text="Proportional differences (e.g. click through rate, bounce rate, gender rate, etc)",font=fontQ.s)
    l.Q3.scal   <-tklabel(t3,text="Scaled differences (e.g. converted value, age, numer of sessions before conversion, etc)",font=fontQ.s)
    onok.prop   <-function() {        
                groups<-as.numeric(tclvalue(e.Q2))
                test<-ifelse(unlist(groups)>2,"Chisq","prop")               
                return(test)
                tkdestroy(t3) }                                     
    onok.scal        <-function(){ 
                groups<-as.numeric(tclvalue(e.Q2))
                test<-ifelse(unlist(groups)>2,"Anova","t-test")              return(test)
                tkdestroy(t3) }                                      
    b.Q3.prop   <-tkbutton(t3,text="Proportional",command=onok.prop) 
    b.Q3.scal   <-tkbutton(t3,text="Scale",command=onok.scal)        
tkgrid(tklabel(t3,text="     "))
tkgrid(Q2,row=6,column=0,rowspan=1,columnspan=4, sticky="w")
tkgrid(e.Q2,row=6,column=4,rowspan=1,columnspan=2,sticky="ew")
tkgrid(tklabel(t3,text="     "))
tkgrid(Q3,sticky="w")
tkgrid(l.Q3.prop,row=9,column=0,rowspan=1,columnspan=4, sticky="w" )
tkgrid(b.Q3.prop,row=9,column=4,rowspan=1,columnspan=2, sticky="ew")                         
tkgrid(l.Q3.scal,row=10,column=0,rowspan=1,columnspan=4, sticky="w" )
tkgrid(b.Q3.scal,row=10,column=4,rowspan=1,columnspan=2, sticky="ew")
*** Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") : 
  [tcl] can't read ".19.6 ": no such variable. (or variations on this with different numbers)
**** Error in as.integer(substitute(groups)) : 
  cannot coerce type 'symbol' to vector of type 'integer' (or variations such as " ... type 'closure' to vector of type 'integer' )

我真的很感激这里的一些实际帮助,因为谷歌搜索错误消息产生的解决方案让我得到了新的错误消息(至少感觉就像进步)但有时也没有结果(让我感到惊讶)。从R中调用与环境,get,mget,deparse,function等相关的帮助文件实际上让我比以前更加困惑。

提前谢谢, 你的

1 个答案:

答案 0 :(得分:0)

我找到了答案(由Greg Snow提供enter link description here

很抱歉回答我自己的Q,但是错误的错误调用“你的文字似乎包含代码blablabla”,而我正确地指定了它(cntrl K)。

这是代码

    define.test<-function(){                
# create a function to call upon
require(tcltk)                    
# make sure tcltk package is active
#  define attributes of widget components   
t3<- tktoplevel()                 
# create main (toplevel) widget
tktitle(t3)<-"Statistical power"      
# widget looks better with title
fontHead    <-tkfont.create(family="times",size=16,weight="bold") 
# define font for heading, etc
fontSub     <-tkfont.create(family="times",size=14,weight="bold")
fontQ       <-tkfont.create(family="times",size=14)
fontQ.s     <-tkfont.create(family="times",size=12)
filename    <-tclVar("Your test")                  
# Default testname, to change in widget
groups      <-tclVar("")                          
# empty variable for number of groups, to fill in in widget
test        <-""                                        
# empty var for test, to be found by <<- in button function
submit.prop <- function(){                 
# function for proportional button
            test<<-"prop"                            
# reassign test in higer level
            tkdestroy(t3)}                            
# destroy widget, or nothing happens
submit.scal <- function(){                       
# function voor scalar button (same as above)
            test<<-"scale"
            tkdestroy(t3)}
###   build widget         
tkgrid(tklabel(t3, text="This module will give you insight in the statistical power requirements of your test", font=fontHead), columnspan=6)     
# header
tkgrid(tklabel(t3, text="     "))                # empty row
tkgrid(tklabel(t3, text="Please answer the following questions:", font=fontSub),  columnspan=6, sticky="w")           # sub header
tkgrid(tklabel(t3,text="     "))
tkgrid(tklabel(t3,text="What is the name of your test?", font=fontQ), row=5, column=0, rowspan=1, columnspan=2,sticky="w")          #Q1
tkgrid(tkentry(t3, textvariable=filename), row=5, column=2, rowspan=1, columnspan=3, sticky="ew") 
# entry for test name(= filename)

tkgrid(tklabel(t3,text="     "))
tkgrid(tklabel(t3,text="How many test-groups do you have?", font=fontQ), row=7, column=0, rowspan=1, columnspan=4, sticky="w")                      
#Q2
tkgrid(tkentry(t3,textvariable=groups),row=7,column=4,rowspan=1,columnspan=1,sticky="ew")                                                      # entry for number of groups to test

tkgrid(tklabel(t3,text="     "))
tkgrid(tklabel(t3,text="What type of differences do you want to test for?",font=fontQ), sticky="w")                                             #Q3
tkgrid(tklabel(t3,text="Proportional differences (e.g. click through rate, bounce rate, gender rate, etc)",  font=fontQ.s),row=10,column=0,rowspan=1,columnspan=4, sticky="w" )
tkgrid(tkbutton(t3,text="Proportional",command=submit.prop),row=10,column=4,rowspan=1,columnspan=2, sticky="ew")            # define test as proportional
tkgrid(tklabel(t3,text="Scaled differences (e.g. converted value, age, numer of sessions before conversion, etc)",  font=fontQ.s),row=11,column=0,rowspan=1,columnspan=4, sticky="w" )
tkgrid(tkbutton(t3,text="Scale",command=submit.scal),row=11,column=4,rowspan=1,columnspan=2, sticky="ew")        # define test as scalar

tkfocus(t3)            # focus on widget
tkwait.window(t3)         # wait for user to give input
return(c(tclvalue(filename),tclvalue(groups),test))        # create character string with user input
}
input       <-define.test()
filename    <-input[1]
groups      <-as.numeric(input[2])
test        <-input[3]