Coder Social home page Coder Social logo

rsession's People

Contributors

dependabot[bot] avatar golgauth avatar kktuax avatar nchabalier avatar yannrichet avatar yannrichet-irsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rsession's Issues

session.loadPackage(); resulted in java.net.SocketException: Broken pipe

What steps will reproduce the problem?
1. On MacOSX, with R 2.15.2 and JDK6
2. Rsession session = Rsession.newInstanceTry(System.out, null);
    session.loadPackage("robust");

What is the expected output? What do you see instead?
Evaluation of expressions work, but load libraries fail
run:
! No Rserve conf given. Trying to use R://localhost
[eval] library(robust)
!! [exception] eval failed
java.net.SocketException: Broken pipe
  library(robust)
  request package robust loading...
    at java.net.SocketOutputStream.socketWrite0(Native Method)
!! [exception] eval failed
  loadedpacks<-.packages()
!! [exception] eval failed
  is.element(set=loadedpacks,el='robust')
  package robust is not loaded.
!! [exception] eval failed
  rm(loadedpacks)
!   package robust loading failed.
[eval] rnorm(1000)
!! [exception] eval failed
  rnorm(1000)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:124)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:205)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:180)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:250)
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:230)
    at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:451)
    at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
    at org.math.R.Rsession.silentlyEval(Rsession.java:965)
    at org.math.R.Rsession.silentlyEval(Rsession.java:941)
    at org.math.R.Rsession.isPackageLoaded(Rsession.java:601)
    at org.math.R.Rsession.loadPackage(Rsession.java:839)
    at rsessiontest.RSessionTest.main(RSessionTest.java:63)
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:124)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:205)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:180)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:250)
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:230)
    at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:451)
    at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
    at org.math.R.Rsession.silentlyEval(Rsession.java:965)
    at org.math.R.Rsession.silentlyEval(Rsession.java:941)
    at org.math.R.Rsession.isPackageLoaded(Rsession.java:614)
    at org.math.R.Rsession.loadPackage(Rsession.java:839)
    at rsessiontest.RSessionTest.main(RSessionTest.java:63)
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:124)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:205)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:180)
    at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:250)
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:230)
    at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:451)
    at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
    at org.math.R.Rsession.silentlyEval(Rsession.java:965)
    at org.math.R.Rsession.eval(Rsession.java:997)
    at org.math.R.Rsession.eval(Rsession.java:1016)
    at rsessiontest.RSessionTest.main(RSessionTest.java:65)
BUILD SUCCESSFUL (total time: 2 seconds)

What version of the product are you using? On what operating system?
Mac OSX 10.7, JDK 6, R 2.15.2

Please provide any additional information below.
Interestingly, I have a Macbook Pro and iMac, with the same configs. The same 
programs works on the Macbook Pro but fails on iMac. Still haven't figured out 
what caused the issue.

Original issue reported on code.google.com by [email protected] on 20 Nov 2012 at 8:27

allow js script engine factory loading, whatever classloader (ex. osgi)

ScriptEngineManager manager = new ScriptEngineManager();

should add null argument to ScriptEngineManager: ScriptEngineManager manager = new ScriptEngineManager(null);

Javadoc says:
This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.
If loader is null, the script engine factories that are bundled with the platform and that are in the usual extension directories (installed extensions) are loaded.

[exception] R environment not initialized.

Hello, I am a novice. My PC is window10, Please help me that how I can run successfully, thank you very much:

public class RC {
public static void main(String args[]) throws RException {
Rsession rc = RserveSession.newInstanceTry(System.out,null);
rc.eval("print(1)");
rc.end();
}}

(!) No Rserve conf given. Trying to use R://localhost:6311
failed: timed out
Connection R://localhost:6311 failed.
(!!) Rserve R://localhost:6311 is not accessible.
Trying to spawn R://localhost:6311
(!!) Failed to find R_HOME (with default null) as null
(!!) Failed to find R_HOME (with default null) as null
Failed to find R_HOME (with default null) as null
(!!) Failed to find R_HOME (with default null) as null
(!!) [exception] R environment not initialized.
Exception in thread "main" java.lang.IllegalArgumentException: [asString] Not an REXP object: org.math.R.Rsession$RException: [exception] R environment not initialized.
R: [exception] R environment not initialized.

Get images directly from R

For me it is often helpful to show an R-plot within a JPanel. Here's what
the core piece of code which is necessary to do so:

        String device = "jpeg"; // device we'll call (this would work with
pretty much any bitmap device)

        String tempFileName = "rmPlotFile." + device;
        REXP xp = connection.parseAndEval("try(" + device + "('" +
tempFileName + "',quality=95, width = " + width + ", height = " + height +
"))");

        if (xp.inherits("try-error")) { // if the result is of the class
try-error then there was a problem
            System.err.println("Can't open " + device + " graphics
device:\n" + xp.asString());
            // this is analogous to 'warnings', but for us it's sufficient
to get just the 1st warning
            REXP w = connection.eval("if (exists('last.warning') &&
length(last.warning)>0) names(last.warning)[1] else 0");
            if (w.isString()) System.err.println(w.asString());
            return null;
        }

        // ok, so the device should be fine - let's plot - replace this by
any plotting code you desire ...
        String preparedScript = RUtils.prepare4RExecution(script);

        String[] splitScript = preparedScript.split(";");
        for (String s : splitScript) {
            try {
                connection.parseAndEval(RUtils.prepare4RExecution(s + ";"));
            } catch (REngineException e) {
                throw new RuntimeException("Error while executing line: " +
s, e);
            }
        }
//        connection.parseAndEval(preparedScript);

        // close the image
        connection.parseAndEval("dev.off();");

        // There is no I/O API in REngine because it's actually more
efficient to use R for this
        // we limit the file size to 1MB which should be sufficient and we
delete the file as well
        xp = connection.parseAndEval("r=readBin('" + tempFileName +
"','raw',2024*2024); unlink('" + tempFileName + "'); r");

        // now this is pretty boring AWT stuff - create an image from the
data and display it ...
        return Toolkit.getDefaultToolkit().createImage(xp.asBytes());

Maybe it would be nice to have this available directly in Rsession. If not 
just close the issue...

Original issue reported on code.google.com by [email protected] on 9 Apr 2010 at 7:55

Add SSL support by modifying org.math.R.RserverConf.java

In summary first:

  1. Add a new constructor to org.math.R.RserverConf.java.
  2. Add a branch dealing with SSL connection to org.math.R.RserverConf.connect()
  3. Configure Rsreve with SSL support.
  4. Connect to Rserve with SSL enabled.

Hope that this feature may be merged into the coming versions.

1.Add a new constructor to org.math.R.RserverConf.java.

	// Added by Jean 2023/05/23
	public Properties properties = null;

	// Added by Jean 2023/05/23
	public RserverConf(String RserverHostName, int RserverPort, String login, String password, Properties props) {

		this.host = RserverHostName;
		this.port = RserverPort;
		this.login = login;
		this.password = password;
		this.properties = props;
	}
  1. Add a branch dealing with SSL connection to org.math.R.RserverConf.connect().
	public synchronized RConnection connect() {
		// Logger.err.print("Connecting " + toString()+" ... ");

		TimeOut t = new TimeOut() {

			protected Object defaultResult() {
				return -2;
			}

			protected Object command() {
				// Added by Jean 2023/05/23
				String tls = "false";
				if (properties != null) {
					try {
						tls = properties.getProperty("tls");
						if (tls == null || tls != "true")
							tls = "false";
					} catch (Exception ex) {
					}
				}

				int n = 10;
				while ((n--) > 0) {

					// Modified by Jean 2023/05/23
					if (tls == "false") {

						try {
							if (host == null) {
								if (port > 0) {
									connection = new RConnection(DEFAULT_RSERVE_HOST, port);
								} else {
									connection = new RConnection(DEFAULT_RSERVE_HOST, DEFAULT_RSERVE_PORT);
								}
								if (connection.needLogin()) {
									connection.login(login, password);
								}
							} else {
								if (port > 0) {
									connection = new RConnection(host, port);
								} else {
									connection = new RConnection(host);
								}
								if (connection.needLogin()) {
									connection.login(login, password);
								}
							}
							return 0;
						} catch (RserveException ex) {
							Log.Err.println("Failed to connect on host:" + host + " port:" + port + " login:" + login
									+ "\n  " + ex.getMessage());
						}
						// Added by Jean for tls connection 2023/05/23
					} else {
						SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
						SSLSocket sslsocket;
						try {
							if (host == null) {
								host = DEFAULT_RSERVE_HOST;
							}
							if (port > 0) {
							} else {
								port = 6311;
							}
							// connection = new RConnection();
							sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
							// Connect to the remote server.
							connection = new RConnection(sslsocket);

							if (connection.needLogin()) {
								connection.login(login, password);
							}
							return 0;
						} catch (Exception ex) {
							Log.Err.println("Failed to connect: " + ex.getMessage());
						}

					}
				}
				return -1;
			}
		};

		try {
			t.execute(CONNECT_TIMEOUT);
		} catch (Exception e) {
			Log.Err.println("Connection " + toString() + " failed: " + e.getMessage());
		}

		if (((Integer) t.getResult()) != 0) {
			Log.Err.println("Connection " + toString() + " failed.");
			return null;
		} else {
			return connection;
		}
	}
  1. Configure Rsreve with SSL support.
root@VM-0-14-ubuntu:/home/ubuntu# vi /etc/Rserv.conf
// Enable remote login
remote enable
// Authentication required
auth required
// Disable plaintext
plaintext disable
// Use utf-8
encoding utf8
//Enable remote control or R process
control enable
//Use qap+tls protocol
qap.tls.port 6311
//Rserve server key
tls.key /root/cert/server.key
//Selfsigned Rserve server cert
tls.cert /root/cert/server.crt
//Selfsigned CA cert
tls.ca /root/cert/demoCA/cacert.pem
//Disable unencypted qap
qap disable
  1. Connect to Rserve with SSL enabled.
	public static Rsession initRserve() throws IOException {
		Properties prop = new Properties();
		prop.setProperty("tls", "true");
		RserverConf rconf = new RserverConf(host, 6311, "user", "password", prop);
		rsession = (RserveSession)new RserveSession(System.out, null, rconf);
		return rsession;
	}

Error when running Rsession eval on multiple lines string

What steps will reproduce the problem?

public static void main(String[] args) throws REXPMismatchException, 
RserveException {


        Rsession s = Rsession.newInstanceTry(System.out, null);
    //    RConnection s = new RConnection();

       String string;

       string = "x = 2 + 4" + "\n"
               + "x" + "\n"
               + "x + 2";

       String y;

       y = s.eval(string).asString();

       System.out.println(y);

        s.end();


}

What is the expected output? What do you see instead?


What version of the product are you using? On what operating system?
Any operating system

Please provide any additional information below.

If you use Rconnection instead, it works just fine

Original issue reported on code.google.com by [email protected] on 13 Nov 2012 at 3:30

2.0.4: [exception] eval failed, request status: error code: 127

1.8.3 works fine.

I tried playing around with sinkOutput(true/false) and sinkMessage(true/false) and this resulted in either one of these error messages inside the R instance:

Error in close(.f) : Object '.f' not found
Error in close(.fm) : Object '.fm' not found

sink stack is full exception when run RSession

What steps will reproduce the problem?
public class TestRsession {

public void testTsession(){
    RserverConf rconf = new RserverConf("127.0.0.1", 6311, "", "", new Properties());
    Rsession s = null;
    s = Rsession.newInstanceTry(System.out, rconf);
    s.eval("warning2error <- function(code=\"\") { tryCatch(code, warning = function(e) stop(e)) }");
    s.eval("warning2error(warning(\'hahaha\'))", true);
    s.end();
}

@Test
public void testAll(){
    for(int i=0; i<25; i++) {
        testTsession();
    }
}

}

Help using Rsession

Hi,

I need to execute a R command which needs a file taken from user through Java GUI. So, I have File f.
How do I send this file to process R command through Rsession?

File file; // Taken from user
rsession.sendFile(file, "demo");

Thanks!

Add a method public synchronized void getFile(OutputStream os, String remoteFile) to org.math.R.RserveSession.java

When calling Rserve backend in a servlet and so on, it's better to write the file got from Rserve remote to HttpOutputStream.
The function below works for me to get a PDF/Excel/Word file back and push the binary stream to the browser.

    /**
     * Get file from R environment to user OutputStream
     *
     * @param os local OutputStream file
     * @param remoteFile R environment file name
     */
    public synchronized void getFile(OutputStream os, String remoteFile) {
        try {
            if (((REXP) silentlyRawEval("file.exists('" + remoteFile.replace("\\", "/") + "')", TRY_MODE)).asInteger() != 1) {
                log(HEAD_ERROR + IO_HEAD + "file " + remoteFile + " not found.", Level.ERROR);
            }
        } catch (Exception ex) {
             log(HEAD_ERROR + ex.getMessage() + "\n  getFile(File remoteFile=" + remoteFile + ")", Level.ERROR);
            return;
        }

        InputStream is = null;
        //synchronized (R) {
        try {
            is = R.openFile(remoteFile.replace("\\", "/"));
            //os = new BufferedOutputStream(new FileOutputStream(localfile));
            IOUtils.copy(is, os);
            log(IO_HEAD + "File " + remoteFile + " received.", Level.INFO);
            is.close();
        } catch (IOException e) {
            log(HEAD_ERROR + IO_HEAD + R.getLastError() + ": file " + remoteFile + " not transmitted.\n" + e.getMessage(), Level.ERROR);
        } finally {
            IOUtils.closeQuietly(is);
         }
        //}
    }

every time we use java rsession invoke remote rserve,one more rserve process will start

wo use rsession(version is 1.8.3) connect to Rserve(version is 1.7.3) do some research,but every time we use java rsession invoke remote rserve,one more rserve process will start.

my code is :

RserverConf rconf = new RserverConf(rserveHost,rservePort, "","", new Properties());
Rsession rsess = Rsession.newInstanceTry(rlogger, rconf);

Thank you for your contribution;we are glad to get response from you。

thanks

fix bug for quote expressions in R2js

see https://travis-ci.org/github/yannrichet/rsession/jobs/725915574 for https://github.com/yannrichet/rsession/tree/bug-quote_expr :

        assert engine.eval("paste(sep='<br/>',\n"
                + "        paste('<HTML name=\"minimum\">minimum is ',0.1),\n"
                + "        paste(sep='',\n"
                + "            'found at ',\n"
                + "            paste(collapse='; ',paste(c('x1','x2'),'=',c(.5,.6))),\n"
                + "            '<br/><img src=\"',\n"
                + "            'files',\n"
                + "            '\" width=\"',600,'\" height=\"',600,\n"
                + "            '\"/></HTML>'))").toString().contains("\"minimum\"");

is ok, while

        assert engine.eval("paste0(\"<HTML name='minimum'>minimum is \",0.1,\n"
                + "                \" found at \",\n"
                + "                paste0(paste(c('x1','x2'),'=',c(.5,.6), collapse=';')),\n"
                + "                \"<br/><img src='\",\n"
                + "                \"files\",\n"
                + "                \"' width='600' height='600'/></HTML>\")").toString().contains("'minimum'");

fails and returns QUOTE_EXPRESSION_....

After i start rserver on my windows10, I run the test method error:

After i start rserver In the RGui on my windows10, I run the test method

@Test
    public void testHardConcurrency() throws REXPMismatchException, InterruptedException {

and got the err in the console window as below:

"C:\Program Files\Java\jdk1.8.0_05\bin\java" -DR_HOME=d:\R\R-3.2.5\bin\x64 ... ...org.math.R.RsessionTest
JUnit version 4.0
R version 3.2.5 (2016-04-14)
tmpdir=D:\lianxi\lianxi-rstat\rsession\tmp
Rserve version 1.8-0

Time: 0
There was 1 failure:

  1. testHardConcurrency(org.math.R.RsessionTest)
    java.lang.NullPointerException
    at org.math.R.Rsession.(Rsession.java:439)
    at org.math.R.Rsession.newInstanceTry(Rsession.java:382)
    at org.math.R.RsessionTest.testHardConcurrency(RsessionTest.java:658)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.junit.internal.runners.CompositeRunner.run(CompositeRunner.java:29)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:121)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:91)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:75)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:42)
    at org.math.R.RsessionTest.main(RsessionTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

FAILURES!!!
Tests run: 1, Failures: 1

Process finished with exit code 1

Please help:

code snippet does not compile

I think some casts are needed in order to get the example java code to compile 
and run, see below.

import java.io.File;

import static org.math.R.Rsession.*;
import org.math.R.Rsession;
import org.rosuda.REngine.REXPMismatchException;

public class RsessionHelper {
    public static void main(String args[]) throws REXPMismatchException {
        Rsession s = Rsession.newInstanceTry(System.out, null);

        double[] rand = (double[]) (cast(s.eval("rnorm(10)"))); // create java variable from R command

        s.set("c", Math.random()); // create R variable from java one

        s.save(new File("save.Rdata"), "c"); // save variables in save.Rdata
        s.rm("c"); // delete variable in R environment
        s.load(new File("save.Rdata")); // load R variable from save.Rdata

        s.set("df", new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }, "x1", "x2", "x3"); // create data frame from
                                                                                                                 // given vectors
        double value = (Double) (cast(s.eval("df$x1[3]"))); // access one value in data frame

        s.toJPEG(new File("plot.jpg"), 400, 400, "plot(rnorm(10))"); // create jpeg file from R graphical command (like plot)

        String html = s.asHTML("summary(rnorm(100))"); // format in html using R2HTML
        System.out.println(html);

        String txt = s.asString("summary(rnorm(100))"); // format in text
        System.out.println(txt);

        System.out.println(s.installPackage("sensitivity", true)); // install and load R package
        System.out.println(s.installPackage("wavelets", true));

        s.end();
    }
}

Original issue reported on code.google.com by [email protected] on 17 Jan 2013 at 9:42

Found a bug in isPackageInstalled

I think I found a bug in isPackageInstalled.
silentlyVoidEval(packs + "<-installed.packages(noCache=TRUE)", false);
boolean isinstalled = false;
REXP r = silentlyEval("is.element(set=" + packs + ",el='" + pack + "')");

If some time series packages except "zoo" have been installed, is.element(set=packs, el='zoo') always returns TRUE as "zoo" is in Depends or Suggests etc.

is.element(set=row.names(packs), el='zoo') or is.element(set=packs[,1], el='zoo') could fix the problem.

Cannot load math.js in R2jsSession

javax.script.ScriptException: :50921:3 Property "???" already defined
"???": "\---",
^ in at line number 50921 at column number 3
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:537)
at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at org.math.R.R2jsSession.loadJSLibraries(R2jsSession.java:263)
at org.math.R.R2jsSession.(R2jsSession.java:158)
at org.math.R.R2jsSession.(R2jsSession.java:215)
at org.math.R.R2jsSession.(R2jsSession.java:211)
at org.math.R.R2jsSession.main(R2jsSession.java:3132)
Caused by: jdk.nashorn.internal.runtime.ParserException: :50921:3 Property "???" already defined

Issue to laod R script in to remote R process with r.eval()

Hey, I'm accessing R functions provided by Rserve on a Linux server from Java through Rsession, and I've hack the code of RserverConf.java and RservSession to include a property "tls" to support a SSL connection, it works fine with the old version of Rsession.
But when I try to upgrade to the newest version of Rsession, it doesn't work again with the function rc.eval("source(file_path)") to load R scripts into the remote R process anymore. Here's an example code and it's output below.
I guess there's something wrong with preprocessing of the eval expression string, , any idea? Thanks in advance.

RserveTest.java

package test.jean;

import java.util.Properties;

import org.math.R.RserveSession;
import org.math.R.RserverConf;

public class RserveTest {

	public static void main(String args[]) {
		try {
			Properties prop = new Properties();
			prop.setProperty("tls", "true");
			RserverConf rconf = new RserverConf(host_ip, 6311, "user", "password", prop);
			RserveSession r = (RserveSession)new RserveSession(System.out, null, rconf);
			// create java variable from R command
			double[] rand = (double[]) r.eval("rnorm(10)"); 
			// Try to load source R script into remote R process
			r.eval("source(\"/home/jean/R/testChinese.R\")",false);
                        r.eval("testChinese()");
			System.out.println(r.asString(r.eval("rc$st")));
						
			r.end();
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}

testChinese.R

testChinese<-function(){
  test<-c("Rserve中文测试。")
  rc<<-list(st=test)
  return(rc)  
}

Console outputs, connection is O.K., and r.eval("rnorm(10)") is run, but fail with r.eval("source("/home/jean/R/testChinese.R")",false),
the line is O.K. in Rstudio.

Will start Rserve session using: R://rserve:rserve@[email protected]:6311


[eval] [?] rnorm(10)


[1.1353798479890087, 0.7775232440858648, 1.6027476936447704, 0.9962751826116198, -0.051552560170046106, -1.0327774491148507, 1.267379942526929, -0.7154783097683929, 0.2075579485456512, 1.2805371369512242]
[eval] source("/home/jean/R/testChinese.R")


java.lang.NullPointerException
	at org.math.R.RserveSession.toString(RserveSession.java:1208)
	at org.math.R.RserveSession.toString(RserveSession.java:1187)
	at org.math.R.Rsession.rawEval(Rsession.java:945)
	at org.math.R.Rsession.eval(Rsession.java:1006)
	at test.jean.RserveTest.main(RserveTest.java:19)

R2js: __.Sys__info is not a function

with variables: {}
because: TypeError: this.Sys__info is not a function in at line number 1
R: Failed to evaluate code

math.squeeze(math.subset(__this__.Sys__info(), R._index('release')))

with variables: {}
because: TypeError: this.Sys__info is not a function in at line number 1
R!
at org.math.R.R2jsSession.silentlyRawEval(R2jsSession.java:2522)
at org.math.R.Rsession.rawEval(Rsession.java:924)
at org.math.R.Rsession.rawEval(Rsession.java:944)
at org.math.R.Rsession.eval(Rsession.java:996)
at org.funz.script.RMathExpression.printInformation(RMathExpression.java:387)
at org.funz.script.RMathExpression.initR(RMathExpression.java:221)
at org.funz.script.RMathExpression.(RMathExpression.java:196)
at org.funz.api.AbstractShell.setInputModel(AbstractShell.java:201)
at org.funz.api.Shell_v1.(Shell_v1.java:71)
at org.funz.main.RunDesign.main(RunDesign.java:292)
org.math.R.Rsession$RException: Failed to evaluate code

Error in file(con, “w“) : cannot open the connection

When try to load R source scripts into remote R process through Rserve, sometimes we will get this error message:

package test.jean;

import java.util.Properties;

import org.math.R.RserveSession;
import org.math.R.RserverConf;

public class RserveTest {

	public static void main(String args[]) {
		try {
			Properties prop = new Properties();
			prop.setProperty("tls", "true");
			RserverConf rconf = new RserverConf(host, 6311, "user", "password", prop);
			RserveSession r = (RserveSession)new RserveSession(System.out, null, rconf);
			// create java variable from R command
			double[] rand = (double[]) r.eval("rnorm(10)"); 
			// Use r.eval() to source script will cause java.lang.NullPointerException
			//r.eval("source(\"/home/jean/R/testChinese.R\")",false);
			// So r.voidEval() to load.
			r.voidEval("source(\"/home/jean/R/testChinese.R\")");
			r.eval("testChinese()");
			System.out.println(r.asString(r.eval("rc$st")));
					
			r.end();
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
source("/home/jean/R/testChinese.R")
R! Error in file(con,"w"):无法打开链结
此外: Warning message:
In file(con,"w"):
无法打开文件'/tmp/RtmpGLcOIq/file7ccc5c5ae965.sh': 没有那个文件或目录
at org.math.R.Rsession.voidEval(Rsession.java:864
at org.math.R.Rsession.voidEval(Rsession,java:885)
at MelbourneRserve.calRServe(MelbourneRserve.java:70)
at MelbourneRserve .doGet(MelbourneRserve.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.iava:670)
<22 internal lines>

testChinese.R

testChinese<-function(){
  test<-c("Rserve中文测试。")
  rc<<-list(st=test)
  return(rc)  
}

The reason is that the temporary directory created by Rserve damon is deleted, need to restart Rserve to created a temporary directory again to address it, it hard to find out the real reason, so I paste it here incase of someone gets the issue.

resssion fails to start correctly when using R 2.10 on windows vista

What steps will reproduce the problem?
1. Start Rserve within R with: Rserve()
2. create an Rsession instance in java with:
Rsession.newLocalInstance(System.out, null)
3. The error msg which show is up is:
Connecting R://localhost:6314
Rserve R://localhost:6314 is not accessible.

If I specify the Rserve port in R via: Rserve(port=6315), the error msg
remains and the port is simply incremented compared to the Rserve-port.
This indicates that there is some kind of communication between Rsession
and Rserve.



Original issue reported on code.google.com by [email protected] on 21 Dec 2009 at 3:39

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.