2016-08-27 5 views
0

1)私はいくつかの問題があったので、gWidgetというパッケージを使ってGUIを作った。ウィンドウに「保存」ボタンを追加したいのですが、既に描画された画像を保存する方法がわかりませんggraphicsGUIで "pdf as pdf"ボタンを設定する方法

library("memoise") 
library("gWidgets2RGtk2") 
library("RGtk2") 
library("digest") 
library("gWidgets2") 
library("stats") 
options(guiToolkit="RGtk2") 
d<-0 

#the main window to make and some parts of it to make 
win <- gwindow("Load curve analysis", visible=TRUE,expand = TRUE) 
biggroup <- ggroup(horizontal = FALSE, container=win, expand = TRUE) 
topgroup<-ggroup(horizontal = TRUE, container=biggroup,expand = TRUE) 
bottomgroup<-ggroup(horizontal = TRUE, container=biggroup, expand = TRUE) 
leftgroup<-ggroup(horizontal = FALSE, container=bottomgroup,expand= TRUE) 
rightgroup<-ggroup(horizontal = FALSE, container=bottomgroup,expand=TRUE) 
add(rightgroup, ggraphics(), expand=TRUE) 

#draw a pic 
updatePlot <- function(h,...) { 
    if(d==1){ 
     if(svalue(Analyse1)=="Month duration curve") 
      plot(1:100,1:100,main="1") 

     if(svalue(Analyse1)=="Month load curve") 
      plot(1:100,1:100,main="2") 
    } 
    if(d==2){ 
     if(svalue(Analyse2)=="Jahresdauerlinie"){ 
      plot(1:100,1:100,main="3") 
     } 
    } 

} 
#the "save" button to make, this button will bring another window, 
#but after setting up the road of the saving place, this smaller window will be closed 
Store<-gbutton("Save as pdf",container=topgroup, handler = function(h,...){ 
    win1 <- gwindow("set up road", visible=TRUE,expand = TRUE) 
    group <- ggroup(horizontal = FALSE, container=win1, expand = TRUE) 
    tmp <- gframe("Pls type the place you want to save in", container=group) 
    obj0<-gedit("",cont=tmp,expand = TRUE) 
    tmp <- gframe("Pls name the new diagram, and end it with .pdf", container=group) 
    obj1<-gedit("Lastganganalyse.pdf",cont=tmp,expand = TRUE) 
    #here the function recordPlot will be used,but it doesnt work,the document cant be opened 
    ok<-gbutton("Ok",container=group, handler = function(h,...){ 
     p<-recordPlot() 
     # I dont know why this record Plot doesnt work 
     setwd(svalue(obj0)) 
     pdf(svalue(obj1)) 
     p 
     dev.off() 
     dispose(win1) 
    }) 

}) 

#the other parts of the main window 
tmp <- gframe("Year(after input a year pls press Enter)", container=leftgroup) 
#Jahren <- gradio(c(2012,2013,2014), horizontal=FALSE, cont=tmp, handler=updatePlot) 
Jahren<-gedit("2012",cont=tmp, handler=updatePlot) 
tmp <- gframe("Month", container=leftgroup) 
Monat <- gslider(from=1,to=12,by=1, value=1, cont=tmp, handler=updatePlot) 
tmp <- gframe("Analysis' way of a month", container=leftgroup) 
Analyse1 <- gcombobox(c(" ","Month duration curve","Month load curve"), cont=tmp, handler=function(h,...){ 
    d<<-1 
    updatePlot(h,...) 
},expand = TRUE) 
tmp <- gframe("Analysis' way of a year", container=leftgroup) 
Analyse2 <- gcombobox(c(" ","Jahresdauerlinie"),cont=tmp,handler=function(h,...){ 
    d<<-2 
    updatePlot(h,...)},expand = TRUE) 

The GUI I made

2)に加えて、私はggroupのサイズを設定する方法がわかりません。または、ウィンドウのサイズのすべての部分をよりよく見えるように制御するにはどうすればよいですか。私はその種の機能を知らない。

3)ggraphicsに描かれている線は見えません。そして、どうすればこの状況を変えることができますか?

+0

は、スタックオーバーフローへようこそ!あなたの状況を再現するために必要なコード/データを提供してください。 http://stackoverflow.com/help/mcve –

答えて

0

我々はグラフィックスで、次のプロット持っていたと仮定します。ボタンのハンドラ内で

ggplot(dat = data.frame("x" = 1:100, "y" = rnorm(100)), aes(x = x, y = y)) + geom_point() 

を、あなたは次のことを試すことができます。

setwd(svalue(obj0)) 
dev.copy2pdf(file = svalue(obj1)) 
+0

ありがとう!私はrecordPlot関数を使用しようとしましたが、私のプログラムではうまくいきませんでした。理由はわかりません。今私はすべてのプログラム(具体的なfuncitonプロットを除く)をここに貼り付けています。私は時間がある、助けることができますか?誠実にFangjing –

+0

いくつかの機能が不足しているため、コードを完全に実行できません。しかし、私はggraphicsウィジェット内にプロットして、次のようにしてプロットを保存することができました: 'setwd(svalue(obj0)); dev.copy2pdf(ファイル= svalue(obj1)) '。 これを試して、動作するかどうか教えてください。 – jav

+0

ありがとう! dev.copy2pdf(file = svalue(obj1))は私のコンピュータで動作しませんでしたが、プログラムはそこで停止しました。私はこの質問の説明でプロット関数を変更したので、あなたのコンピュータ上でそれを実行することができます。ご協力いただきありがとうございます! –

関連する問題