簡體   English   中英

無法使用R和gWidget管理多個窗口並將數據保存在調查的GUI中

[英]Failing to manage multiple windows and save data in a survey's GUI with R and gWidgets

我最近開始使用R,發現自己在嘗試構建圖形用戶界面時被阻止。

我的目的是編寫用於進行調查的GUI 基本上,我想打開一個由問題,答案和“ 下一步”按鈕組成的窗口。 在單擊Next后,我希望保存答案並跳至下一個窗口/問題


由於我在打開幾個窗口時遇到問題,我在這里看了一下 ,發現可以在處理程序的幫助下一個接一個地打開窗口:

addHandlerChanged() #upon the Next button

因此,我嘗試輸入兩個問題/窗口的代碼(見下文)。 我想將結果保存到to_return中 :一個矩陣2行(1、2)和3列(問題編號,問題,答案)。 [我現在正嘗試在Excel文件中逐個問題地回答問題]

我的問題如下:

  1. 打開第二個窗口后, 我無法關閉第一個窗口 (我嘗試將dispose(h $ obj)visible(win1)= FALSE添加到第一個Windows按鈕處理程序中,但失敗)

  2. 我無法將數據“追加”到我的矩陣中(每個窗口都會更新to_return矩陣)

# calling GUI library
library(gWidgets)
options(guiToolkit="tcltk")

Q1 <- function(){
  # creating first window
  win1 <- gwindow("I) Q1.", visible=TRUE)
  group <- ggroup(horizontal = FALSE, container=win1)
  # creating question
  question <- glabel("Do you have a driving license?", container = group)
  # creating answer
  answer <- gradio(c("Yes","No"), container=group)
  # creating next button
  nextQuestion <- gbutton("Next",container=group)
  # handler
  addHandlerChanged(nextQuestion, handler = function(h, ...) {
    # answer to save in matrix
    to_return <- rbind(to_return,c(svalue(win1),svalue(question),svalue(answer)))
    #opening next question
    Q2()
    print(to_return)
  } )
}


Q2<- function(){
  # creating second window
  win2 <- gwindow("I) Q2.", visible=TRUE)
  group <- ggroup(horizontal = FALSE, container=win2)
  # creating question
  question <- glabel("What is your gender?", container = group)
  # creating answer
  answer <- gradio(c("Female","Male"), container=group)
  # finish button
  nextQuestion <- gbutton("Finish",container=group, handler = function(h,...) {
    # answer to save in matrix
    to_return <- rbind(to_return,c(svalue(win2),svalue(question),svalue(answer)))
    print(to_return)
    # finish and close
    dispose(h$obj)
  })
}

如果我運行Q1()則會得到以下結果,其中to_return不保留數據...

     [,1]     [,2]                             [,3] 
[1,] "I) Q1." "Do you have a driving license?" "Yes"
     [,1]     [,2]                   [,3]    
[1,] "I) Q2." "What is your gender?" "Female"

任何幫助或見識將不勝感激!

謝謝!

八度


編輯:按照jverzani的答案,這是兩個問題的可能代碼,數據保存在csv文件中。

## calling GUI library
library(gWidgets)
options(guiToolkit="tcltk")

setwd("Your\\Path\\Here")

w <- gwindow(title="Survey")
g <- ggroup(cont=w)

state = new.env()
pages <- list()
pages[[1]] = function(cont, state) {
  group <- ggroup(horizontal = FALSE, container=cont)
  ## creating question
  question <- glabel("Do you have a driving license?", container = group)
  ## creating answer
  answer <- gradio(c("Yes","No"), container=group)
  ## creating next button
  nextQuestion <- gbutton("Next",container=group)
  ## handler
  addHandlerChanged(nextQuestion, handler = function(h, ...) {
    ## answer to save in matrix
    assign("A", c(svalue(question),svalue(answer)), state)
    delete(cont, group)
    pages[[2]](cont, state)
  })
}

pages[[2]] = function(cont, state) {
  group <- ggroup(horizontal = FALSE, container=cont)
  ## creating question
  question <- glabel("What is your gender?", container = group)
  ## creating answer
  answer <- gradio(c("Male","Female", "Other"), container=group)
  ## creating next button
  nextQuestion <- gbutton("Next",container=group)
  ## handler
  addHandlerChanged(nextQuestion, handler = function(h, ...) {
    ## answer to save in matrix
    assign("B", c(svalue(question),svalue(answer)), state)
    delete(cont, group)
    pages[[3]](cont, state)
  })
}

pages[[3]] = function(cont, state) {
  group <- ggroup(horizontal=FALSE, container=cont)
  ## result matrix for csv
  to_return=matrix(nrow=0,ncol=2)
  colnames(to_return) <- c("Question", "Answer")
  for (k in 1:length(names(state))) {
    a = get(names(state)[k], state)
    to_return <- rbind(to_return,a)
    rownames(to_return)[k] <- paste("Q",k,sep = "")
    g = ggroup(cont=group, horizontal=TRUE)
    glabel(a[1], cont=g)
    glabel(" ", cont=g)
    glabel(a[2], cont=g)
  }
  btn <- gbutton("Finish", container=group, handler = function(h,...) {
    write.csv(to_return, "survey.csv", row.names=TRUE, col.names=TRUE)
    dispose(h$obj)})
}

## start it off
pages[[1]](g, state)

編輯2:行Assign(“ one”,c(svalue(question),svalue(answer)),state)應該使用字母(“ A”,“ B” ...)或數字以正確的順序排列,否則答案在名稱(狀態)中用於k時會混淆。

這不是最抽象的方法,但是對於以后的修改,應該很容易理解這種在新窗口上使用delete模式。 使用環境可以幫助您保持對處理程序的調用之間的狀態,沒有這種技巧就不會出現這種情況,因為默認情況下將在處理程序的環境中進行分配。

# calling GUI library
library(gWidgets2)
options(guiToolkit="tcltk")


w <- gwindow()
g <- ggroup(cont=w)

state = new.env()
pages <- list()
pages[[1]] = function(cont, state) {
    group <- ggroup(horizontal = FALSE, container=cont)
    ## creating question
    question <- glabel("Do you have a driving license?", container = group)
    ## creating answer
    answer <- gradio(c("Yes","No"), container=group)
    ## creating next button
    nextQuestion <- gbutton("Next",container=group)
    ## handler
    addHandlerChanged(nextQuestion, handler = function(h, ...) {
        ## answer to save in matrix
        assign("one", c(svalue(question),svalue(answer)), state)
        delete(cont, group)
       pages[[2]](cont, state)
    })
}

pages[[2]] = function(cont, state) {
    group <- ggroup(horizontal = FALSE, container=cont)
    ## creating question
    question <- glabel("What is your gender?", container = group)
    ## creating answer
    answer <- gradio(c("Male","Female", "Other"), container=group)
    ## creating next button
    nextQuestion <- gbutton("Next",container=group)
    ## handler
    addHandlerChanged(nextQuestion, handler = function(h, ...) {
        ## answer to save in matrix
        assign("two", c(svalue(question),svalue(answer)), state)
        delete(cont, group)
        pages[[3]](cont, state)
    })
}

pages[[3]] = function(cont, state) {
    group <- ggroup(horizontal=FALSE, container=cont)
    for (k in names(state)) {
        g = ggroup(cont=group, horizontal=TRUE)
        a = get(k, state)
        glabel(a[1], cont=g)
        glabel(" ", cont=g)
        glabel(a[2], cont=g)
    }
}

## start it off
pages[[1]](g, state)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM