创建包含GUI的R包

时间:2016-10-24 09:43:02

标签: r r-package

我有一个工作的R代码,主要用于构建,训练和预测一些IC50值。 我有工作的GUI。

# Load the packages
library(gWidgets)
library(gWidgetsRGtk2)
library(tcltk)
library(data.table)
library(cluster)
library(e1071)
library(caret)
options(guiToolkit="RGtk2")

#main GUI code
win=gwindow("Naive Bayes Tool for IC50 Prediction")
size(win)<-c(1000,600)
group<-ggroup(cont=win)
left_group<-ggroup(cont=group,horizontal=FALSE)
right_group<-ggroup(cont=group,horizontal=FALSE)
txtOutput = gtext("", cont = group, expand = TRUE)
frame<-gframe("Bankruptcy Tool",cont=left_group,horizontal=FALSE,)
# size(frame)<-c(300,350)
tbl=glayout(cont=frame)
# To Browse the file
tbl[2,1] =  gfilebrowse (text = "Select Training File...", type = "open", quote = FALSE,
                         filter = list("Text File" = list(patterns = c("*.csv"))), container = tbl)
tbl[3,1] = gbutton("Upload File",cont=tbl,handler=uploadfile)
enabled(tbl[3,1]) <- FALSE

#Function to upload file
uploadfile<-function(h,...){
  bipsdata <- read.csv(svalue(tbl[2,1]))
  inputdata<-data.frame(bipsdata)  
  normo2<-capture.output(head(inputdata))
  str.split <- strsplit(normo2, "\\s+")
  str.joined <- sapply(str.split,function(bits) paste(bits,collapse='\t'))
  insert(txtOutput,normo2,font.attr = c(family="monospace"))
  gmessage("Data Uploaded!!!", color="blue")
}
addHandlerChanged(tbl[2,1], handler=function(h,...) {
  val <-svalue(tbl[2,1])
  #   data1<-data.frame(val)
  if(!is.na(val)) {
    filename <<- val
    #     df1 = read.csv(filename, header = TRUE)
    enabled(tbl[3,1]) <- TRUE
  }
})
addHandlerChanged(tbl[3,1], handler=uploadfile)
# Creating a frame for prediction of clustering results
frame3<-gframe("Prediction",cont=left_group,horizontal=FALSE, font.attr=list(style="bold"))

ARatings<- gbutton("Submit and Build Model",cont=frame3, handler=Arat)
Enter<-glabel("Enter the Input Data:-",cont=frame3)

IR1= gedit("AATS3e", cont=frame3,coerce.with=as.numeric)
MR2= gedit("MATS3c", cont=frame3,coerce.with=as.numeric)
FF3= gedit("MATS4c", cont=frame3,coerce.with=as.numeric)
CR4= gedit("MATS3e", cont=frame3,coerce.with=as.numeric)
CO5= gedit("MATS3s", cont=frame3,coerce.with=as.numeric)
OP6= gedit("GATS7c", cont=frame3,coerce.with=as.numeric)
Class1= gedit("Class", cont=frame3)
Predict<-gbutton("Predict Model", cont=frame3, handler=AratSave)


# Function to Train Data
Arat<-function(ARatings,h,...) 
{
  library(data.table)
  library(e1071)
  bipsdata<-read.csv(filename)
  inputdata<-data.frame(bipsdata)
  naive.model<-naiveBayes(Class~.,inputdata,probability=TRUE)
  result1<-print(naive.model)
  normo<-data.frame(result1)
  normo1<-capture.output(naive.model)
  insert(txtOutput,normo1, font.attr = c(family="monospace"))
}

#Function to Predict Bankruptcy
AratSave<-function(ARatings, h,...)
{
  library(data.table)
  library(e1071)
  bipsdata<-read.csv(filename)
  inputdata<-data.frame(bipsdata)
  naive.model<-naiveBayes(Class~AATS3e+MATS3c+MATS4c+MATS3e+MATS3s+GATS7c,inputdata,probability=TRUE)

  AATS3e<-as.numeric(svalue(IR1))
  MATS3c<-as.numeric(svalue(MR2))
  MATS4c<-as.numeric(svalue(FF3))
  MATS3e<-as.numeric(svalue(CR4))
  MATS3s<-as.numeric(svalue(CO5))
  GATS7c<-as.numeric(svalue(OP6))
  Class<-svalue(Class1)

  Newinput<-data.frame(cbind(AATS3e,MATS3c,MATS4c,MATS3e,MATS3s,GATS7c))
  print(Newinput)
  results<-predict(naive.model,Newinput)
  result12<-print(results)
  normo11<-data.frame(result12)
  normo1<-capture.output(result12)
  insert(txtOutput,normo1, font.attr = c(family="monospace"))

  #Comparing Accuracy
  #code is having error because the predicted result is only one column but the model has many columns.
  xtab<-table(pred=results,true=svm.model)
  cofusionMatrix(xtab)
  y<-print(confusionMatrix(xtab))
  normo12<-capture.output(y)
  insert(txtOutput,normo12, font.attr = c(family="monospace"))
}

我尝试创建R包。 按照步骤导入DESCRIPTION文件中的库。 但找不到解决方案。请帮我。 错误就像这样

Updating IC50 documentation
Loading IC50
Error in eval(expr, envir, enclos) (from IC50.r#7) : could not find function "gwindow"
Calls: suppressPackageStartupMessages ... withr_with_dir -> force -> source_many -> source_one -> eval -> eval
Execution halted

Exited with status 1.

正如您所看到的,gwindow是一个应该从我在DESCRIPTION文件中导入的库中调用的函数。但它没有用。

描述文件如下所示。

Package: IC50
Type: Package
Title: Predicts IC50
Version: 0.1.0
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
LazyData: TRUE
RoxygenNote: 5.0.1
Imports: gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret
Depends: R (>= 3.0),gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret, cluster
       , e1071,
         ggplot2, gplots, grid, gtools,
        gWidgetsRGtk2, nnet,
Suggests: pmml (>= 1.2.13), bitops, colorspace, ada, amap, arules,
        arulesViz, biclust, cairoDevice, cba, cluster, corrplot, descr,
        doBy, dplyr, e1071, ellipse, fBasics, foreign, fpc, gdata,
        ggdendro, ggplot2, gplots, graph, grid, gridExtra, gtools,
        gWidgetsRGtk2, hmeasure, Hmisc, kernlab, Matrix, mice, nnet,
        odfWeave, party, playwith, plyr, psych, randomForest, RBGL,
        RColorBrewer, readxl, reshape, rggobi, RGtk2Extras, ROCR,
        RODBC, rpart, rpart.plot, SnowballC, stringr, survival,
        timeDate, tm, verification, wskm, XML, pkgDepTools, Rgraphviz
NeedsCompilation: Yes
LazyData: yes

这是错误

==> devtools::document(roclets=c('rd', 'collate', 'namespace'))

Updating IC50 documentation
Loading IC50

(Rterm.exe:732): Gtk-CRITICAL **: gtk_table_attach: assertion `child->parent == NULL' failed
First time using roxygen2. Upgrading automatically...
Error: Missing name at IC50.r:6
Execution halted

Exited with status 1.

添加了带有更新导入的DESCRIPTION文件。 现在工作正常描述文件如下。并在Main R文件中添加了Funtion以作为库调用。

Description: Utilizes the Naive Bayes Classifier for Drug Prediction.
License: Public
LazyData: TRUE
RoxygenNote: 5.0.1
Imports: gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret,gWidgetstcltk
Depends: R (>= 3.2),gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret, cluster
       ,e1071,gWidgetstcltk, ggplot2, gplots, grid, gtools,
        gWidgetsRGtk2, nnet,
Suggests: pmml (>= 1.2.13), bitops, colorspace, ada, amap, arules,
        arulesViz, biclust, cairoDevice, cba, cluster, corrplot, descr,
        doBy, dplyr, e1071, ellipse, fBasics, foreign, fpc, gdata,
        ggdendro, ggplot2, gplots, graph, grid, gridExtra, gtools,
        gWidgetsRGtk2, hmeasure, Hmisc, kernlab, Matrix, mice, nnet,
        odfWeave, party, playwith, plyr, psych, randomForest, RBGL,
        RColorBrewer, readxl, reshape, rggobi, RGtk2Extras, ROCR,
        RODBC, rpart, rpart.plot, SnowballC, stringr, survival,
        timeDate, tm, verification, wskm, XML, pkgDepTools, Rgraphviz
NeedsCompilation: Yes
LazyData: yes

1 个答案:

答案 0 :(得分:1)

添加了带有更新导入的DESCRIPTION文件。现在工作正常描述文件如下。并在Main R文件中添加了Funtion以作为库调用。

Description: Utilizes the Naive Bayes Classifier for Drug Prediction.
License: Public
LazyData: TRUE
RoxygenNote: 5.0.1
Imports: gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret,gWidgetstcltk
Depends: R (>= 3.2),gWidgets,gWidgetsRGtk2,tcltk,data.table,cluster,e1071,caret, cluster
       ,e1071,gWidgetstcltk, ggplot2, gplots, grid, gtools,
        gWidgetsRGtk2, nnet,
Suggests: pmml (>= 1.2.13), bitops, colorspace, ada, amap, arules,
        arulesViz, biclust, cairoDevice, cba, cluster, corrplot, descr,
        doBy, dplyr, e1071, ellipse, fBasics, foreign, fpc, gdata,
        ggdendro, ggplot2, gplots, graph, grid, gridExtra, gtools,
        gWidgetsRGtk2, hmeasure, Hmisc, kernlab, Matrix, mice, nnet,
        odfWeave, party, playwith, plyr, psych, randomForest, RBGL,
        RColorBrewer, readxl, reshape, rggobi, RGtk2Extras, ROCR,
        RODBC, rpart, rpart.plot, SnowballC, stringr, survival,
        timeDate, tm, verification, wskm, XML, pkgDepTools, Rgraphviz
NeedsCompilation: Yes
LazyData: yes