2013-06-30 36 views
5

Tôi đã làm việc với r, rserve và ggplot trễ.Gọi ggplot từ Rserve. Hình ảnh png trống của 1KB

Đoạn mã này khi được thực hiện trong bảng điều khiển R trực tiếp hiển thị biểu đồ có hai thanh như mong đợi. png (tệp = 'Yash_GenderVsTotalAccountBalance.png', chiều rộng = 400, chiều cao = 350, res = 72) ggplot (dữ liệu = YashCustomersTài khoản, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity') dev.off()

Nhưng khi tôi gọi cùng một mã (liên quan đến cuộc gọi ggplot) từ JAVA bằng cách sử dụng Rserve nó tạo ra một png trống. Mã như sau.

package RRnD; 
import java.awt.*; 
import org.rosuda.REngine.*; 
import org.rosuda.REngine.Rserve.*; 

public class PlottingGenderVsTotalAccountBalance { 

    public static void main(String[] args) throws RserveException { 
     try { 
      RConnection c = new RConnection(); // make a new local connection on default port (6311) 
      System.out.println("1. Connection created ----------------------------------------------------------------------");    
      System.out.println("Working directory = "+c.eval("getwd()").asString()); 
      System.out.println("2. Working dir read ----------------------------------------------------------------------"); 
      c.eval("YashCustomers <- read.csv('YashCustomer.csv', header=TRUE)"); 
      c.eval("YashAccounts <- read.csv('YashAccount.csv', header=TRUE)"); 
      c.eval("YashCustomersAccounts <- merge(YashCustomers,YashAccounts, by='CUSTOMER_ID')"); 
      System.out.println("3. Data.frames read ----------------------------------------------------------------------"); 

      c.eval("library(ggplot2)"); 
      c.eval("require(ggplot2)"); 
      System.out.println("4. ggplot2 loaded ----------------------------------------------------------------------"); 

      c.eval("png(file='Yash_GenderVsTotalAccountBalance.png',width=400,height=350,res=72)"); 
      c.parseAndEval("ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='identity');dev.off()");    
      System.out.println("5. plotting done ----------------------------------------------------------------------"); 

      REXP xp = c.parseAndEval("r=readBin('Yash_GenderVsTotalAccountBalance.png','raw',1024*1024)"); 
      c.parseAndEval("unlink('Yash_GenderVsTotalAccountBalance.jpg'); r"); 
      Image img = Toolkit.getDefaultToolkit().createImage(xp.asBytes()); 
      System.out.println("img = "+img); 
      System.out.println("6. File reading done ----------------------------------------------------------------------"); 

      System.out.println("10. All done ----------------------------------------------------------------------");    
      c.close(); 
     } catch (REngineException ree) { 
      System.out.println("REngineException ..."); 
      System.out.println(ree.getMessage()); 
     } catch (Exception e) { 
      System.out.println("Exception ..."); 
      System.out.println(e.getMessage()); 
     } 
    } 

} 

LƯU Ý: - Thay vì gọi ggplot, nếu tôi thực hiện cuộc gọi âm mưu đơn giản như dòng sau, nó hoạt động tốt. hình ảnh png được tạo đúng cách. c.parseAndEval ("cốt truyện (YashCustomers ['CUSTOMER_ID']); dev.off()"); ... thay vì ... c.parseAndEval ("ggplot (dữ liệu = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity'); dev.off()");

Vui lòng giúp tôi tìm sự cố. Rất cám ơn, --Yash

+0

IMO bạn nên 'in' kết quả' ggplot2'. – daroczig

Trả lời

2

Trong bảng điều khiển, ggplot sẽ tự động vẽ hình ảnh. Nhưng trong khi gọi ggplot âm mưu từ Rserver, chúng tôi cần in rõ ràng hình ảnh.

c.parseAndEval ("in (ggplot (dữ liệu = YashCustomersTài khoản, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity')); dev.off()");

Xin cảm ơn, --Yash

Các vấn đề liên quan