Evaluating expressions called from Java in R. Out

2019-08-10 19:41发布

I have an issue loading a large dataset into R from Java. The problem is actually with the function I am using: re.eval(). I want to load a file into R so that I can analyse/manipulate it in R, however I want to do this from Java (this is in order to build a GUI).

What I want the function to do is parse and evaluate the string I provide, however, the eval function parses, evaluates and returns the result. I get an out of memory error from java regarding the heap size. This is the code I have at the moment:

JButton getFile = new JButton("Load");
        getFile.setBounds(316, 10, 151, 23);
        getFile.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                    REXP x;
                    getFileName();
                if (dataFilePath != null){
                String file = dataFilePath.replace("\\", "\\\\\\\\");
                re.eval("data<-read.csv(file='"+file+"', head=TRUE, sep='|')");


                x = re.eval("names(data)");

                String[] column_names = x.asStringArray();
                originalDataLength = column_names.length;

                for (int i = 0; i < column_names.length; i++) {
                        comboBox.insertItemAt(column_names[i], 0);  
                    }

                textField.setText("Data Loaded");
                }

        }
    }
);

This code worked when I was working with a dataframe that was 13500x220. The new dataframe is 50000x700. I was also wondering what exactly is happening in the code above? Is it created in R and sent back to java (i.e duplicated?).

Any help/comments would be greatly appreciated. Also I have looked at the Rosuda Rengine class description and the functions available; I was thinking that maybe rniEval() might solve the problem but I don't know how to implement it.

标签: java r heap jri
1条回答
成全新的幸福
2楼-- · 2019-08-10 20:39

actionPerformed is called on the event stack, and you should keep the event handling short. It just might help too. Call:

SwingUtilities.invokeLater(new Runnable() {
    public void run() { ... your code ... }
}

Furthermore one should combine both eval expressions in R, if only the column names are wanted. Maybe just read the first line (by java?).

查看更多
登录 后发表回答