This is my first post here but I am a regular stackoverflow visitor.
For uploading new datasets, I am processing a dataframe in which one column has some typing mistake. I want users to modify the error from a gcombobox, thus the errors and the correct value will be stored and automatically corrected the next time.
# 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"))
This is the code: ( GUI toolkit :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)
})
}
I don't get my expected outcome. I find very hard to retrieve the svalue from the combobox and impossible to store several results from the variable "aNew" when running the loop. Also happens these other two incidents:
1 - when I run the code including the loop: It does not "use to!" enter the widgets (confirm popup)
2 - The loop exists after disposing the first "Recode Country" window, so to say, processing "canadada"
What I really want is that the user can fix the errors in the data.frame Incorrect_Country. Then the error and the solution are stored (data frame: error) for the program to know how to deal with it for future uploads.
How it should work:
1- confirm window (for stopping the loop till the user has corrected the previous error)
2- pop up shows error "canadada"
3- user selects from combobox "canada"
4- Pressing ok will store an integer, the error, and the corrected name in the table error
5- The loop runs again (press confirm and shows "Peruru")
6- Finally I get the error table such as
x, y, z
1, canadada, Canada
2, Perurur, Peru
3, chinanan, China
Any advice would be appreciated. Thanks