Skip to content

Commit 6953116

Browse files
committed
TryCatch disable by default, 4.0.0 release
1 parent 6b884a1 commit 6953116

7 files changed

Lines changed: 34 additions & 10 deletions

File tree

RCaller/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repositories {
88
}
99

1010
group = 'com.github.jbytecode'
11-
version = '4.0.0-SNAPSHOT'
11+
version = '4.0.0'
1212
description = 'RCaller is a software library which simplifies performing data analysis and statistical calculations in Java using R. The details are hidden from users including transferring data between platforms, function calls, and retrieving results.'
1313

1414
dependencies {

RCaller/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.github.jbytecode</groupId>
55
<artifactId>RCaller</artifactId>
6-
<version>4.0.0-SNAPSHOT</version>
6+
<version>4.0.0</version>
77
<packaging>jar</packaging>
88

99
<name>RCaller</name>

RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private final static double[][] generateRandomMatrix(int n, int m) {
5353

5454
public static void main(String[] args) {
5555

56-
performSimulation(/* size of vector */ 10, 10 /* times */ , SimType.Vector);
56+
performSimulation(/* size of vector */ 2, 100 /* times */ , SimType.Vector);
5757

5858
}
5959

@@ -77,7 +77,7 @@ public static void performSimulation(int VectorSize, int SimCount, SimType type)
7777
code.addRCode("result <- t(randommatrix)");
7878
}
7979
caller.setRCode(code);
80-
caller.runAndReturnResultOnline("result");
80+
caller.runAndReturnResultOnline("result", false);
8181
/* return variable is not handled */ caller.getParser().getAsDoubleArray("result");
8282
elapsed[simulations] = (int) System.currentTimeMillis() - timeStart;
8383

RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,30 @@ private void runRCode() throws ExecutionException {
278278
* re-used by invoking this method again. When you are done with this
279279
* process, you must explicitly stop it.
280280
*
281+
* If R raises an error, it is ignored. For throwing them in Java, use
282+
* {@link #runAndReturnResultOnline(String, boolean)} with addTryCatch=true
283+
*
281284
* @see #stopStreamConsumers()
282285
* @param var The R variable to return
283286
* @throws ExecutionException if R cannot be started
284287
*/
285288
public void runAndReturnResultOnline(String var) throws ExecutionException {
289+
runAndReturnResultOnline(var, false);
290+
}
291+
292+
/**
293+
* Runs the current code in the existing R instance (or in a new one) and
294+
* returns the R variable "var". The R process is kept alive and can be
295+
* re-used by invoking this method again. When you are done with this
296+
* process, you must explicitly stop it.
297+
*
298+
* @since 4.0.0
299+
* @see #stopStreamConsumers()
300+
* @param var The R variable to return
301+
* @param addTryCatch wrap original R code to tryCatch function (can impact performance)
302+
* @throws ExecutionException if R cannot be started or raise error inside while addTryCatch parameter is true
303+
*/
304+
public void runAndReturnResultOnline(String var, boolean addTryCatch) throws ExecutionException {
286305
rCallerOptions.resetRetries();
287306
boolean done = false;
288307
do {
@@ -323,7 +342,13 @@ public void runAndReturnResultOnline(String var) throws ExecutionException {
323342
}
324343

325344
try {
326-
var script = rCode.toTryCatchScript(errorFile) + rCode.createEndSignalCode(resultReadyControlFile);
345+
String script;
346+
if (addTryCatch) {
347+
script = rCode.toTryCatchScript(errorFile);
348+
} else {
349+
script = rCode.toString();
350+
}
351+
script += rCode.createEndSignalCode(resultReadyControlFile);
327352
rInput.write(script.getBytes(Globals.standardCharset));
328353
rInput.flush();
329354
} catch (IOException e) {

RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public String toString() {
267267
/**
268268
* Wrap current code to standard tryCatch function.
269269
* Error handler saves details to errorOutputFile if the error occurs.
270-
* @param errorOutputFile
271-
* @return
270+
* @param errorOutputFile file to save error if it occurs
271+
* @return generated script to be evaluated
272272
*/
273273
String toTryCatchScript(File errorOutputFile) {
274274
//Using code snippet "An improved “error handler”" with withCallingHandlers nested in tryCatch

RCaller/src/main/resources/arrow_bridge.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ send_element_by_arrow <- function(obj, name, stream) {
2828
if (length(dim(obj)) > 2) {
2929
#3- and more-D arrays are not supported
3030
stop(paste(length(dim(obj)), "-D arrays are not supported"))
31-
#TODO add try-catch support on toplevel
3231
}
3332
#1-D array and empty matrix can be converted to Vector
3433
dim(obj) <- c()

RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void exceptionCatchTest() {
216216
RCaller rcaller = RCaller.create();
217217
RCode code = rcaller.getRCode();
218218
code.addRCode("a <- log(\"not a number\")");
219-
rcaller.runAndReturnResultOnline("a");
219+
rcaller.runAndReturnResultOnline("a", true);
220220
}
221221

222222
@Test
@@ -234,7 +234,7 @@ public void rHaltedTest() {
234234
rcaller.runAndReturnResultOnline("a");
235235
} catch (ExecutionException ex) {
236236
Logger.getLogger(RunOnlineTest.class.getName()).log(Level.SEVERE, ex.getMessage());
237-
if (ex.getMessage().contains("R code throw an error:")) {
237+
if (ex.getMessage().contains("R process died, stderr:")) {
238238
exceptionThrown = true;
239239
}
240240
rcaller.stopRCallerOnline();

0 commit comments

Comments
 (0)