これは私の最初の投稿ですが、私は通常のスタックオーバーフローの訪問者です。
新しいデータセットをアップロードするために、1 つの列に入力ミスがあるデータフレームを処理しています。ユーザーに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" から複数の結果を保存することは不可能です。また、次の 2 つのインシデントも発生します。
1 - ループを含むコードを実行すると、「使用できません!」ウィジェットを入力します (確認ポップアップ)
2 - 最初の「Recode Country」ウィンドウを破棄した後、ループが存在します。つまり、「canadada」を処理しています。
私が本当に望んでいるのは、ユーザーが data.frame Incorrect_Country のエラーを修正できることです。次に、プログラムが将来のアップロードのためにそれを処理する方法を知るために、エラーと解決策が保存されます (データ フレーム: エラー)。
どのように動作するか:
1- 確認ウィンドウ (ユーザーが前のエラーを修正するまでループを停止するため)
2- ポップアップにエラー「カナダ」が表示さ
れる 3- ユーザーがコンボボックス「カナダ」から選択する
4- [OK] を押すと、整数、エラー、および表エラーの修正された名前
5- ループが再度実行されます (確認を押すと「Peruru」が表示されます)
6- 最後に、次のようなエラー表が表示されます
x, y, z
1, canadada, Canada
2, Perurur, Peru
3, chinanan, China
アドバイスをいただければ幸いです。ありがとう