运行 来自 Java Servlet 的 R 代码

run R code from Java Servlet

我正在尝试 运行 来自 Java Servlet 运行 的一些 R 代码在 Eclipse 中作为 IDE TomCat7.0。

END 目标 是在 运行TOMCAT 上创建一个网站]JETTY 显示 Graphs + Data 使用现有 R 代码制作,R 函数 returns 作为 Base 64 代码结合一些现有 Java 和 Python 函数处理和创建高级 Excel 文件。

(出于很多原因我不打算使用 R Shiny Server,所以我想 运行 R / Python 来自 Java)

为了获得基本设置,我创建了一个动态 Web 项目,其中包含一个简单的 servlet 和一个可以 运行 R 代码的 test.java 文件。

test.java:

public class test {
      public static void main(String[] args) throws Exception {
          System.out.println(DoR.collectR());
      }
}

DoR.java:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class DoR {

  public static Object collectR() {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("Renjin");
    if(engine == null) {
        throw new RuntimeException("Renjin Script Engine not found on the classpath.");
    }
    Object catchR = "XX";
    try {
        catchR = engine.eval("df <- data.frame(x=1:10, y=(1:10)+rnorm(n=10));"
                + "print(df);"
                + "print(lm(y ~ x, df))");
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    return catchR;
   }
}

StartTestServlet.java:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/StartTestServlet")
public class StartTestServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public StartTestServlet() {
    super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //response.getWriter().append("Served at: ").append(request.getContextPath()).append((CharSequence) DoR.collectR());
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
}

我安装了库:

renjin-script-engine-0.7.0-RC2.jar
renjin-studio-0.8.1915-jar-with-dependencies.jar
slf4j-api-1.7.19.jar
slf4j-simple-1.7.19.jar

If i run the test.java as a Java Application it works fine.

If i run the servlet on the TomCat 7.0 server it works fine (without the .append((CharSequence) DoR.collectR()) )

If i run the code with .append((CharSequence) DoR.collectR()) I get the exception:

java.lang.RuntimeException: Renjin Script Engine not found on the classpath.
    ......DoR.collectR(DoR.java:16)
    ......StartTestServlet.doGet(StartTestServlet.java:19)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

DoR.java:16 是行:

try {

StartTestServlet.doGet(StartTestServlet.java:19) 是行:

response.getWriter().append("Served at: ").append(request.getContextPath()).append((CharSequence) DoR.collectR());

I have been trying all kinds of things to get this working in Eclipse Version: Mars.1 Release (4.5.1) And downloaded the Example files from Renjin / searching internet and so on but with no luck uptill now to get the R code running from within the Servlet.

Am i overlooking something simple or do i need to do it completely different ?

查看(新增)eclipse-dynamic-web-project in Renjin Examples。总结自述文件:

  1. Renjin Downloads Page 下载独立的 renjin-script-engine-0.8.1931-jar-with-dependencies.jar。您似乎将旧版本的 renjin-script-engine 与 GUI jar 结合在一起。
  2. 将此 JAR 复制到 WebContent/WEB-INF/lib,以便它与您的应用程序一起部署。

另请查看您的 "DoR" 方法:您正在调用 print() 将其输出发送到标准输出和 returns NULL。在网络服务器上,这可能会发送到日志中,但肯定不会在对客户端的响应中结束。

如果要将lm()函数的结果发送给客户端, 例如,您可以使用 rjson::toJSON() 对其进行序列化。