Navigation Menu

Skip to content

Commit

Permalink
Moved execution of R commands to R business manager
Browse files Browse the repository at this point in the history
  • Loading branch information
cande committed Apr 19, 2011
1 parent d1b41b7 commit 8409308
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 78 deletions.
5 changes: 4 additions & 1 deletion plugins/net.bioclipse.r.business/META-INF/MANIFEST.MF
Expand Up @@ -14,5 +14,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.springframework.osgi.aopalliance.osgi,
net.bioclipse.r
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.apache.log4j
Import-Package: de.walware.rj.data,
de.walware.rj.servi,
de.walware.rj.services;version="0.5.0",
org.apache.log4j
Bundle-ActivationPolicy: lazy
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2011 Egon Willighagen <egon.willighagen@gmail.com>
* Christian Ander <christian.ander@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -26,5 +27,6 @@ public interface IRBusinessManager extends IBioclipseManager {
params = "String command"
)
public String eval(String command);

public String getStatus();
public Boolean isWorking();
}
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2011 Egon Willighagen <egon.willighagen@gmail.com>
* Christian Ander <christian.ander@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -10,17 +11,52 @@
******************************************************************************/
package net.bioclipse.r.business;

import net.bioclipse.managers.business.IBioclipseManager;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import javax.security.auth.login.LoginException;

import net.bioclipse.managers.business.IBioclipseManager;
import org.apache.log4j.Logger;

import de.walware.rj.servi.RServi;
import de.walware.rj.data.RObject;
import de.walware.rj.data.RStore;
import net.bioclipse.r.RServiManager;
import org.eclipse.core.runtime.CoreException;

public class RBusinessManager implements IBioclipseManager {

private static final Logger logger = Logger.getLogger(RBusinessManager.class);

{
private RServi rservi;
public String R_HOME;
private String status = "";
private Boolean working = true;
private RServiManager rsmanager = new RServiManager("app");

public RBusinessManager() throws LoginException, NoSuchElementException {
logger.info("Starting R manager");
logger.debug("R_HOME =" + System.getenv("R_HOME"));
R_HOME = System.getenv("R_HOME");
logger.debug("R_HOME=" + R_HOME);
try {
R_HOME = rsmanager.checkRPath(R_HOME);
rsmanager.setEmbedded(R_HOME);
}
catch (FileNotFoundException e) {
working = false;
status = e.getMessage();
}
catch (CoreException e) { // Catch R startup errors.
working = false;
status = extractRjError(e.getCause().getCause().getMessage());
}
if (working) {
try { rservi = rsmanager.getRServi("task"); }
catch (CoreException e){
working = false;
status = e.getMessage();
}
}
if (!working) logger.error(status);
}

/**
Expand All @@ -30,17 +66,56 @@ public class RBusinessManager implements IBioclipseManager {
public String getManagerName() {
return "r";
}


public String getStatus() {
return status;
}

public Boolean isWorking() {
return working;
}

public String eval(String command) {
logger.debug("R cmd: " + command);
try {
String returnVal = "disabled";
logger.debug(" -> " + returnVal);
return returnVal;
} catch (Throwable error) {
logger.debug(
"Error while evaluating R command: " + error.getMessage(), error );
return "Error: " + error.getMessage();
String returnVal;
try {
RObject data = rservi.evalData("capture.output(print(("+command+")))",null); // capture.output(print( )) gives a string output from R, otherwise R objects. The extra pair of () is needed for the R function print to work properly.
RStore rData = data.getData();
StringBuilder builder = new StringBuilder();
for(int i=0;i<rData.getLength();i++) {
builder.append(rData.getChar(i));
}
returnVal = builder.toString();
}
catch (CoreException rError) { // Catch R errors.
returnVal = "Error: " + extractRError(rError.getMessage());
}
catch (Throwable error) {
error.printStackTrace();
returnVal = "Error: " + error.getMessage();
}
logger.debug(" -> " + returnVal);
return returnVal;
}

private String extractRjError(String error) {
String newline = System.getProperty("line.separator");
error = error.substring(error.indexOf("JR library path:"));
error = error.replaceFirst(newline, "");
error = error.substring(0, error.indexOf(newline)).trim();
error = "Path to rj package not found." + newline + error;
return error;
}
private String extractRError(String error) {
logger.debug("full error: " + error);
String result = error;
if (error.startsWith("Evaluation failed")) {
result = error.substring(error.indexOf(":")+1).trim();
int index;
if ((index=result.indexOf("):")) > 0) {
result = result.substring(index+2, result.lastIndexOf(">.")).trim();
}
}
return result;
}
}
8 changes: 3 additions & 5 deletions plugins/net.bioclipse.r.ui/META-INF/MANIFEST.MF
Expand Up @@ -10,14 +10,12 @@ Require-Bundle: org.eclipse.core.runtime,
net.bioclipse.ui,
net.bioclipse.scripting.ui,
org.eclipse.ui,
net.bioclipse.r
net.bioclipse.r,
net.bioclipse.r.business;bundle-version="2.5.0"
Bundle-ActivationPolicy: lazy
Export-Package: net.bioclipse.r.ui,
net.bioclipse.r.ui.views
Import-Package: de.walware.rj.data,
de.walware.rj.servi,
de.walware.rj.services;version="0.5.0",
org.apache.log4j,
Import-Package: org.apache.log4j,
org.slf4j
Bundle-Vendor: The Bioclipse Team
Bundle-ClassPath: .
Expand Up @@ -5,79 +5,36 @@
*which accompanies this distribution, and is available at
*http://www.eclipse.org/legal/epl-v10.html
*
*Contact: http://www.bioclipse.net/
*******************************************************************************/
package net.bioclipse.r.ui.views;

import java.util.NoSuchElementException;

import javax.security.auth.login.LoginException;

import de.walware.rj.servi.RServi;
import de.walware.rj.data.RObject;
import de.walware.rj.data.RStore;
import net.bioclipse.r.RServiManager;
import org.eclipse.core.runtime.CoreException;

import net.bioclipse.r.business.Activator;
import net.bioclipse.r.business.IRBusinessManager;
import net.bioclipse.scripting.ui.views.ScriptingConsoleView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RConsoleView extends ScriptingConsoleView {

private RServi rs;
public String R_home;
private RServiManager rm = new RServiManager("app");
final Logger logger = LoggerFactory.getLogger(RConsoleView.class);
final Logger logger = LoggerFactory.getLogger(RConsoleView.class);
private IRBusinessManager r;

public RConsoleView() throws LoginException, NoSuchElementException, CoreException {
logger.info("RConsole: Starting R..");
R_home = System.getenv("R_HOME");
logger.debug("RConsole: R_HOME =" + R_home);
// String URL = "rmi://127.0.0.1/rservi-pool";
rm.setEmbedded(R_home);
rs = rm.getRServi("task");
public RConsoleView() {
logger.info("Starting R console UI");
r = Activator.getDefault().getJavaRBusinessManager();
}

@Override
protected String executeCommand( String command ) {
echoCommand(command);
logger.debug("R cmd: " + command);
String returnVal;
try {
RObject data = rs.evalData("capture.output(print(("+command+")))",null); // capture.output(print( )) gives a string output from R, otherwise R objects. The extra pair of () is needed for the R function print to work properly.
RStore rData = data.getData();
StringBuilder builder = new StringBuilder();
for(int i=0;i<rData.getLength();i++) {
builder.append(rData.getChar(i));
}
returnVal = builder.toString();
}
catch (CoreException rError) { // Catch R errors.
returnVal = "Error: " + extractRError(rError.getMessage());
}
catch (Throwable error) {
error.printStackTrace();
returnVal = "Error: " + error.getMessage();
}
logger.debug(" -> " + returnVal);
echoCommand(command);
if (r.isWorking()) {
returnVal = r.eval(command);
} else
returnVal = "R console is inactivated: " + r.getStatus();
printMessage(returnVal);
return returnVal;
}

private String extractRError(String error) {
logger.debug("full error:" + error);
String result = error;
if (error.startsWith("Evaluation failed")) {
result = error.substring(error.indexOf(":")+1).trim();

int index;
if ((index=result.indexOf("):")) > 0) {
result = result.substring(index+2, result.lastIndexOf(">.")).trim();
}
}

return result;
return returnVal;
}

protected void waitUntilCommandFinished() {
Expand All @@ -87,4 +44,4 @@ protected void waitUntilCommandFinished() {
void echoCommand(final String command) {
printMessage(NEWLINE + "> " + command + NEWLINE);
}
}
}

0 comments on commit 8409308

Please sign in to comment.