从组合框循环返回值

时间:2013-09-06 09:55:58

标签: r gwidgets

这是我在这里发表的第一篇文章,但我是一名常规的stackoverflow访客。

为了上传新的数据集,我正在处理一个数据框,其中一列有一些输入错误。我希望用户从gcombobox修改错误,因此错误和正确的值将被存储并在下次自动更正。

# Sample data which includes a wrong countryid
Incorrect_Country = data.frame(id=c(1,2,3), countryid=c("Canadada", "Peruru", "Chinanan"), othercolumn=c("777", "111", "333"))

# Dataframe where some previous pitfalls have been stored 
#(it´s useful because the model can learn from previous pitfalls)
Country_Recode = data.frame(id=c(1,2,3), Remote.Name=c("Frankekz", "Potuugal", "Mexxico"), Name=c("France", "Portugal", "Mexico"))

# This table presents values for the combobox
Master_Country = data.frame(name=c("Canada", "Peru", "China", "France", "Portugal", "Mexico"))

这是代码:(GUI工具包:gWidgetstcltk)

# Define errors in country
Rewrite_Country = unique(sqldf("SELECT * FROM Incorrect_Country WHERE countryid NOT IN  (SELECT 'Remote.Name' FROM Country_Recode)"))

B <- 0

# Dataframe where to store the wrong names which the respective correction
error <- data.frame(x=integer(0), y= character(0), z = character(0))

# Loop for each row with typing errors
for (i in Rewrite_Country["countryid"])  {
    B <- B + 1

    # I create a dialog for preventing several windows to pop up 
    # as this produced that the returned value from a combobox was assigned to the wrong recode name 
    gconfirm("New Value not specified. Do u want to change it?", handler=function(h,...){

        # I create the window which will include a combobox of correct values    
        w <- gwindow("Recode Country for:")
        gp <- ggroup(container=w)

        ## A group for the message and buttons
        i.gp <- ggroup(horizontal=FALSE, container = gp)
        glabel(i, container=i.gp)

        ## Combobox including the correct names
        cb <- gcombobox(Master_Country[["name"]], selected=0, container=i.gp)
        addHandlerChanged(cb,handler=function(h,...)  {

            # I assign the combobox's svalue to a new global variable
            aNew <- as.character()
            assign("aNew", svalue(cb), envir = as.environment(1)) 
            print(svalue(cb))

            })

        ## A group to organize the buttons
        button.group <- ggroup(container = i.gp)
        ## Push buttons to right
        addSpring(button.group) 

        # Ok Button for storing the resuts: (index, wrong value, correct value)     
        button <- gbutton("ok", handler = function(h,...) {
            error <- rbind(error, c(B,i, aNew))

            # In one of the last tries I set the new environment for the table 
            assign("error", error, envir = as.environment(1)) 
            print(error)
            dispose(w) 
        }, container = button.group)

        gbutton("cancel", handler = function(h,...) dispose(w), container=button.group)            
    }) 

}

我没有得到预期的结果。我发现很难从组合框中检索svalue,并且在运行循环时无法从变量“aNew”存储多个结果。还发生了其他两个事件:

1 - 当我运行包含循环的代码时:它不“用来!”进入小部件(确认弹出窗口)

2 - 在处理完第一个“重新编码国家/地区”窗口后存在循环,所以说,处理“canadada”

我真正想要的是用户可以修复data.frame Incorrect_Country中的错误。然后存储错误和解决方案(数据框:错误),以便程序知道如何处理以备将来上传。
它应该如何运作:

1-确认窗口(用于停止循环直到用户纠正上一个错误)
2-弹出显示错误“canadada”
3-用户从组合框中选择“加拿大”
4-按ok将在表错误中存储整数,错误和更正的名称 5-循环再次运行(按确认并显示“Peruru”)
6-最后我得到了错误表,例如

    x, y, z  
    1, canadada, Canada    
    2, Perurur, Peru  
    3, chinanan, China  

任何建议都将不胜感激。感谢

0 个答案:

没有答案