2013-06-30 13 views
5

Pracowałem z użyciem r, rserve i ggplot późno.Wzywając ggplot z Rserve. Pusty obraz png o wielkości 1KB

Ten fragment kodu, gdy jest wykonywany w konsoli R, bezpośrednio renderuje wykres z dwoma paskami zgodnie z oczekiwaniami. png (file = 'Yash_GenderVsTotalAccountBalance.png', width = 400, height = 350, res = 72) ggplot (dane = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = "tożsamość") dev.off()

Ale kiedy wywołuję ten sam kod (z udziałem wywołań ggplot) z JAVA, używając Rserve'a, tworzy on puste png. Kod jest jak poniżej.

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()); 
     } 
    } 

} 

UWAGA: - Zamiast wywołania ggplot, jeśli wykonam proste wywołanie wykresu, takie jak poniższa linia, działa poprawnie. Obraz png jest prawidłowo tworzony. c.parseAndEval ("fabuła (YashCustomers ['CUSTOMER_ID']); dev.off()"); ... zamiast ... c.parseAndEval ("ggplot (dane = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat =" tożsamość "); dev.off()");

Uprzejmie pomóżcie mi w znalezieniu problemu. Wielkie dzięki, - Yash

+0

IMO powinieneś 'print' wynikowy' ggplot2' obiekt. – daroczig

Odpowiedz

2

W konsoli ggplot rysuje obraz automatycznie. Ale podczas wywoływania plotowania ggplot z Rserver musimy jawnie wydrukować obraz.

c.parseAndEval ("drukowania (ggplot (dane = YashCustomersAccounts, AES (x = GENDER_DESC, Y = ACCOUNT_BALANCE)) + geom_bar ('tożsamość' STAT =)); dev.off()");

Dzięki, --Yash