从Rserve调用ggplot。 1KB的空白png图像[重复]

问题描述 投票:5回答:1

这个问题在这里已有答案:

我最近一直在使用r,rserve和ggplot。

在R控制台中执行此代码时,会直接呈现带有两个条形的图表。 png(file ='Yash_GenderVsTotalAccountBalance.png',width = 400,height = 350,res = 72)ggplot(data = YashCustomersAccounts,aes(x = GENDER_DESC,y = ACCOUNT_BALANCE))+ geom_bar(stat ='identity')dev。关闭()

但是当我使用Rserve从JAVA调用相同的代码(涉及ggplot调用)时,它会创建一个空白的png。代码如下。

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

}

注意: - 如果我像下面一行做一个简单的情节调用,它可以正常工作,而不是ggplot调用。 png图像正确创建。 c.parseAndEval( “图(YashCustomers [ 'CUSTOMER_ID']); dev.off()”); ...而不是... c.parseAndEval(“ggplot(data = YashCustomersAccounts,aes(x = GENDER_DESC,y = ACCOUNT_BALANCE))+ geom_bar(stat ='identity'); dev.off()”);

请帮助我找到问题。非常感谢, - Yash

java r ggplot2 png rserve
1个回答
3
投票

在控制台ggplot中自动绘制图像。但是在从Rserver调用ggplot绘图时,我们需要明确地打印图像。

c.parseAndEval("print(ggplot(data=YashCustomersAccounts, aes(x=GENDER_DESC,y=ACCOUNT_BALANCE)) + geom_bar(stat='identity'));dev.off()");
© www.soinside.com 2019 - 2024. All rights reserved.