这是我在这里的第一篇文章,但我是 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 - 处理第一个“重新编码国家”窗口后存在循环,也就是说,处理“加拿大”
我真正想要的是用户可以修复 data.frame Incorrect_Country 中的错误。然后存储错误和解决方案(数据框:错误),以便程序知道如何处理它以供将来上传。
它应该如何工作:
1- 确认窗口(用于停止循环,直到用户纠正了前一个错误)
2- 弹出显示错误“canadada”
3- 用户从组合框“canada”中选择
4- 按确定将存储一个整数、错误和更正表中的名称错误
5- 循环再次运行(按确认并显示“Peruru”)
6- 最后我得到错误表,例如
x, y, z
1, canadada, Canada
2, Perurur, Peru
3, chinanan, China
任何意见,将不胜感激。谢谢