2013-06-30 11 views
5

Ho lavorato con r, rserve e ggplot di ritardo.Chiamata ggplot da Rserve. Immagine png vuota di 1KB

Questo pezzo di codice eseguito nella console R rende direttamente un grafico con due barre come previsto. png (file = 'Yash_GenderVsTotalAccountBalance.png', larghezza = 400, altezza = 350, res = 72) ggplot (dati = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity') dev.off()

Ma quando chiamo lo stesso codice (che coinvolge le chiamate ggplot) da JAVA utilizzando Rserve crea un png vuoto. Il codice è il seguente.

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

} 

NOTA: - Invece di chiamata ggplot, se faccio una semplice chiamata trama come la seguente riga, funziona benissimo. l'immagine png è stata creata correttamente. c.parseAndEval ("plot (YashCustomers ['CUSTOMER_ID']); dev.off()"); ... anziché ... c.parseAndEval ("ggplot (data = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity'); dev.off()");

Gentilmente aiutarmi a trovare il problema. Molte grazie, --Yash

+0

IMO si dovrebbe 'print' la risultante' oggetto ggplot2'. – daroczig

risposta

2

Nella console ggplot disegna automaticamente l'immagine. Ma durante il richiamo del disegno ggplot da Rserver dobbiamo stampare esplicitamente l'immagine.

c.parseAndEval ("print (ggplot (data = YashCustomersAccounts, aes (x = GENDER_DESC, y = ACCOUNT_BALANCE)) + geom_bar (stat = 'identity')); dev.off()");

Grazie, --Yash

Problemi correlati