java调用R快速入门:
应项目的需求,要在java项目中调用R。
已经装好了R软件,如何调用?这里给出了一种方式,可供大家参考:
Rserve的配置和启动:
①安装Rserve包。通过R命令行:install.packages("Rserve");
②在R命令行输入:library(Rserve),加载Rserve;
然后输入Rserve(),启动服务。
Java调用R:
1. 下载Rserve提供的jar包(http://www.rforge.net/Rserve/files/)下载REngine.jar和RserveEngine.jar。然后导入java项目。import org.rosuda.REngine.*; import org.rosuda.REngine.Rserve.RConnection; import org.rosuda.REngine.Rserve.RserveException; public class CallR { public static void main(String[] args) throws REXPMismatchException, REngineException { RConnection c; try { c = new RConnection();//连接 double[] w={75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 63.5,66.6, 64.0, 57.0, 69.0, 56.9, 50.0, 72.0}; c.eval("jpeg('D:\\\\test2.jpg')"); //存储路径 c.assign("x",w); //赋值 c.eval("hist(x,freq = FALSE)");//绘图函数 c.eval("lines(density(x), col = 'blue')");//线条颜色 c.eval("dev.off()");//关闭设备 c.close();//记得关闭连接,否则内存的资源不被释放,会引起进程冲突 } catch (RserveException e) { e.printStackTrace(); } } }
<img>在web项目里,访问url
首先,图片和工程不在一个盘符下。图片也不能放到工程下。在JSP文件中,用<img src="C:/……/test.jpg"/>读取图片,是读不到的。
因为,JSP页面在引图片时页面解析路径为<img src="http://localhost:8080/test/images/test.jpg">。也就是说JSP在引图片的时候是先去你的工程目录下找的。
所以如果要在JSP中引入其他盘符的图片,那只能配置虚拟路径。
可参考:http://blog.csdn.net/ryl219362/article/details/9449567#
Ubuntu服务器安装R
在Ubuntu下安装统计语言R还是比较简单的,只需要两行命令即可搞定。
sudo apt-get install r-base
sudo apt-get install r-base-dev
若是安装不成功,试试更新下Ubuntu的软件
sudo apt-get update
安装结束后输入来查看R语言的版本,如果显示正常,表示R语言的环境安装成功
R --version
通常R语言安装在
/usr/lib/R/
设置环境变量R=/usr/lib/R/bin(可直接用$R取值)
下载的Rserve_1.7-3.tar.gz包在:/tmp/RtmpUvYbKu/downloaded_packages
然后可以直接安装:
R CMD INSTALL /tmp/RtmpUvYbKu/downloaded_packages/Rserve_1.7-3.tar.gz
查看是否成功:
R CMD Rserve
然后,
cd $R
R startup
启动成功!
R CMD Rserve --RS-enable-remote
这个可以
打开远程模式重新启动
#查看进程 ~ ps -aux|grep Rserve conan 7142 0.0 1.2 116296 25240 ? Ss 09:13 0:00 /usr/lib/R/bin/Rserve #查看端口 ~ netstat -nltp|grep Rserve tcp 0 0 127.0.0.1:6311 0.0.0.0:* LISTEN 7142/Rserve
#杀掉刚才的Rserve守护进程 ~ kill -9 7142 #打开远程模式重新启动 ~ R CMD Rserve --RS-enable-remote #查看端口 ~ netstat -nltp|grep Rserve tcp 0 0 0.0.0.0:6311 0.0.0.0:* LISTEN 7173/Rserve
http://my.oschina.net/zh119893/blog/224275?fromerr=DKvmauzW
在CenOS上安装,出现如下问题:(20160316)
Error in .External2(C_X11, paste("jpeg::", quality, ":", filename, sep = ""), :
unable to start device JPEG
In addition: Warning message:
In jpeg("////usr////lib////java////apache-tomcat-7.0.65////webapps////response-educube-analysis////img////testR_all_unit.jpg") :
no jpeg support in this version of R
Error in .External2(C_X11, paste("jpeg::", quality, ":", filename, sep = ""), :
unable to start device JPEG
指令:
R
> capabilities()
显示不支持。
在我的Ubuntu服务器上,显示如下:
所以问题根源在于CenOS的R软件,不支持jpeg指令
http://blog.csdn.net/wa2003/article/details/46310811linux下不能画图的问题解决
安装完后,还要重新装Rserve
安装完后画出来的图上的字不能正常显示,服务器提示如下:
Pango-WARNING **: failed to choose a font, expect ugly output. engine-type='PangoRenderFc', script='latin'
yum install liberation-sans-fonts
装完后就可以正常显示啦!!!
组员yc博客记录:http://blog.jibug.com/73.html
/*****************************20160421******************************************/
安装完forecast包之后,library(forecast)提示不能找到对应包。
http://www.dataguru.cn/blog-2472-445.html
ERROR: compilation failed for package ‘RcppArmadillo’
* removing ‘/usr/local/lib64/R/library/RcppArmadillo’
ERROR: dependency ‘RcppArmadillo’ is not available for package ‘forecast’
* removing ‘/usr/local/lib64/R/library/forecast’
The downloaded source packages are in
‘/tmp/RtmpNGvBTk/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning messages:
1: In install.packages("forecast") :
installation of package ‘RcppArmadillo’ had non-zero exit status
2: In install.packages("forecast") :
installation of package ‘forecast’ had non-zero exit status
> library('forecast')
解决办法:
升级系统版本或是安装较低版本的R包
下载低版本R包:
http://cran.r-project.org/src/contrib/Archive/RcppArmadillo/
http://cran.r-project.org/src/contrib/Archive/forecast/
手动安装R包:
# R CMD INSTALL -l /usr/lib64/R/library RcppArmadillo_0.3.930.1.tar.gz
# R CMD INSTALL -l /usr/lib64/R/library forecast_5.0.tar.gz
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> install.packages("forecast")
--- Please select a CRAN mirror for use in this session ---
Error in download.file(url, destfile = f, quiet = TRUE) :
unsupported URL scheme
HTTPS CRAN mirror
1: 0-Cloud [https] 2: Austria [https]
3: Chile [https] 4: China (Beijing 4) [https]
5: Colombia (Cali) [https] 6: France (Lyon 2) [https]
7: France (Paris 2) [https] 8: Germany (Münster) [https]
9: Iceland [https] 10: Mexico (Mexico City) [https]
11: Russia (Moscow) [https] 12: Spain (A Coruña) [https]
13: Switzerland [https] 14: UK (Bristol) [https]
15: UK (Cambridge) [https] 16: USA (CA 1) [https]
17: USA (KS) [https] 18: USA (MI 1) [https]
19: USA (TN) [https] 20: USA (TX) [https]
21: USA (WA) [https] 22: (HTTP mirrors)
Selection: 22
HTTP CRAN mirror
1: 0-Cloud 2: Algeria
3: Argentina (La Plata) 4: Australia (Canberra)
5: Australia (Melbourne) 6: Austria
7: Belgium (Antwerp) 8: Belgium (Ghent)
9: Brazil (BA) 10: Brazil (PR)
11: Brazil (RJ) 12: Brazil (SP 1)
13: Brazil (SP 2) 14: Canada (BC)
15: Canada (NS) 16: Canada (ON)
17: Chile 18: China (Beijing 2)
19: China (Beijing 3) 20: China (Beijing 4)
21: China (Xiamen) 22: Colombia (Cali)
23: Czech Republic 24: Denmark
25: Ecuador 26: El Salvador
27: Estonia 28: France (Lyon 1)
29: France (Lyon 2) 30: France (Marseille)
31: France (Montpellier) 32: France (Paris 1)
33: France (Paris 2) 34: Germany (Berlin)
35: Germany (Göttingen) 36: Germany (Münster)
37: Greece 38: Hungary
39: Iceland 40: India
41: Indonesia (Jakarta) 42: Iran
43: Ireland 44: Italy (Milano)
45: Italy (Padua) [https] 46: Italy (Padua)
47: Italy (Palermo) 48: Japan (Tokyo)
49: Japan (Yamagata) 50: Korea (Seoul 1)
51: Korea (Seoul 2) 52: Korea (Ulsan)
53: Lebanon 54: Mexico (Mexico City)
55: Mexico (Texcoco) 56: Mexico (Queretaro)
57: Netherlands (Amsterdam) 58: Netherlands (Utrecht)
59: New Zealand 60: Norway
61: Philippines 62: Poland
63: Russia (Moscow) 64: Singapore
65: Slovakia 66: South Africa (Cape Town)
67: South Africa (Johannesburg) 68: Spain (A Coruña)
69: Spain (Madrid) 70: Sweden
71: Switzerland 72: * (Chungli)
73: * (Taipei) 74: Thailand
75: Turkey (Denizli) 76: Turkey (Mersin)
77: UK (Bristol) 78: UK (Cambridge)
79: UK (London 1) 80: UK (London 2)
81: UK (St Andrews) 82: USA (CA 1)
83: USA (CA 2) 84: USA (CO)
85: USA (IA) 86: USA (IN)
87: USA (KS) 88: USA (MI 1)
89: USA (MI 2) 90: USA (MO)
91: USA (NC) 92: USA (OH 1)
93: USA (OH 2) 94: USA (OR)
95: USA (PA 2) 96: USA (TN)
97: USA (TX) 98: USA (WA)
99: Venezuela
Selection: 21
trying URL 'http://mirrors.xmu.edu.cn/CRAN/src/contrib/forecast_7.1.tar.gz'
Content type 'application/x-gzip' length 196896 bytes (192 KB)
==================================================
downloaded 192 KB
* installing *source* package ‘forecast’ ...
** package ‘forecast’ successfully unpacked and MD5 sums checked
** libs
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c calcBATS.cpp -o calcBATS.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c calcTBATS.cpp -o calcTBATS.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c etsTargetFunction.cpp -o etsTargetFunction.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c etsTargetFunctionWrapper.cpp -o etsTargetFunctionWrapper.o
gcc -std=gnu99 -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c etscalc.c -o etscalc.o
gcc -std=gnu99 -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c etspolyroot.c -o etspolyroot.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c makeBATSMatrices.cpp -o makeBATSMatrices.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c makeTBATSMatrices.cpp -o makeTBATSMatrices.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c updateMatrices.cpp -o updateMatrices.o
g++ -I/usr/local/lib64/R/include -DNDEBUG -I/usr/local/include -I"/usr/local/lib64/R/library/Rcpp/include" -I"/usr/local/lib64/R/library/RcppArmadillo/include" -fpic -g -O2 -c updateTBATSMatrices.cpp -o updateTBATSMatrices.o
g++ -shared -L/usr/local/lib64/R/lib -L/usr/local/lib64 -o forecast.so calcBATS.o calcTBATS.o etsTargetFunction.o etsTargetFunctionWrapper.o etscalc.o etspolyroot.o makeBATSMatrices.o makeTBATSMatrices.o updateMatrices.o updateTBATSMatrices.o -L/usr/local/lib64/R/lib -lRlapack -L/usr/local/lib64/R/lib -lRblas -lgfortran -lm -L/usr/local/lib64/R/lib -lR
installing to /usr/local/lib64/R/library/forecast/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (forecast)
The downloaded source packages are in
‘/tmp/Rtmp6jy0xb/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
> library(forecast)
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: timeDate
This is forecast 7.1
> Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: timeDate
This is forecast 7.1
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: timeDate
This is forecast 7.1
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: timeDate
This is forecast 7.1