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

getting values in R from tkentry() widgets

我正在尝试创建一个小型交互式 window,用户(现在是我)可以在其中输入有关功效计算的特定信息(测试名称、组数、数据类型)以执行正确的功率计算类型。我知道如何直接在 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) 的回答中复制了这个解决方案,不幸的是它对我不起作用。 运行 我的代码我收到此错误消息: "Error in structure(.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、函数等相关的帮助文件实际上让我比阅读它们之前更加困惑。

提前致谢, 你的

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

很抱歉回答了我自己的问题,但是由于错误调用 "you're text appears to contain code 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]