Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are (at least) two ways of going about this. </p> <p><hr></p> <h2>Command Line &amp; Scripts</h2> <p>You can execute Rscripts from the command line with <code>Rscript.exe</code>. E.g. in your script you would have:</p> <pre><code># Parse arguments. # ... # ... chisq.test(obs, exp) </code></pre> <p>Rather than creating CSVs in Java and having R read them, you should be able to pass them straight to R. I don't see the need to create CSVs and pass data that way, UNLESS your matrices are quite big. There are limitations on the size of command line arguments you can pass (varies across operating system I think).</p> <p>You can pass arguments into Rscripts and parse them using the <code>commandArgs()</code> functions or with various packages (e.g. <a href="http://cran.r-project.org/web/packages/optparse/index.html" rel="nofollow noreferrer">optparse</a> or <a href="http://cran.r-project.org/web/packages/getopt/index.html" rel="nofollow noreferrer">getopt</a>). See <a href="https://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script">this thread for more information</a>.</p> <p>There are several ways of calling and reading from the command line in Java. I don't know enough about it to give you advice but a bit of googling will give you a result. Calling a script from the command line is done like this:</p> <pre><code>Rscript my_script.R </code></pre> <hr> <h2>JRI</h2> <p>JRI lets you talk to R straight from Java. Here's an example of how you would pass a double array to R and have R sum it (this is Java now):</p> <pre><code>// Start R session. Rengine re = new Rengine (new String [] {"--vanilla"}, false, null); // Check if the session is working. if (!re.waitForR()) { return; } re.assign("x", new double[] {1.5, 2.5, 3.5}); REXP result = re.eval("(sum(x))"); System.out.println(result.asDouble()); re.end(); </code></pre> <p>The function <code>assign()</code> here is the same as doing this in R:</p> <pre><code>x &lt;- c(1.5, 2.5, 3.5) </code></pre> <p>You should be able to work out how to extend this to work with a matrix. </p> <hr> <p>I think JRI is quite difficult at the beginning. So if you want to get this done quickly the command line option is probably best. I would say the JRI approach is less messy once you get it set up though. And if you have situations where you have a lot of back and forth between R and Java it is definitely better than calling multiple scripts. </p> <ol> <li><a href="http://www.rforge.net/JRI/" rel="nofollow noreferrer">Link to JRI</a>. </li> <li><a href="http://www.studytrails.com/RJava-Eclipse-Plugin/" rel="nofollow noreferrer">Recommended Eclipse plugin to set up JRI</a>.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload