我目前正在开发一个R包以在R中集成Java代码。但是,在尝试正确调用Java类方法时遇到了问题。到目前为止,我已经独立开发了Java程序并将其编译为类文件,然后将其打包为jar文件。我的代码示例如下:
library(rJava)
.jinit()
.onLoad <- function(lib, pkg) {
pathToSdk <- paste(system.file(package = "mailViz") , "C:\\path\\to\\libs", sep="")
jarPaths <- c(paste(pathToSdk, "mail.jar", sep=""),
paste(pathToSdk, "mailReader.jar", sep="")
)
.jpackage(pkg, morePaths=jarPaths)
attach( javaImport( c("java.lang", "java.io", "java.mail", "java.util", "java.text")))
packageStartupMessage( paste( "mailViz loaded. The classpath is: ", paste(.jclassPath(), collapse=" " ) ) )
}
# get method arguments for the class
#.jmethods("mailReader","readEmailInfo")
z=.jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", "password")
Run Code Online (Sandbox Code Playgroud)
但是,当我执行.jcall函数时,会收到错误消息:
Error in .jcall("mailReader", "Ljava/lang/String;", "readEmailInfo", "username", :
method readEmailInfo with signature (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; not found
Run Code Online (Sandbox Code Playgroud)
我尝试了多种更改参数的方法,但是没有运气。当我在类文件上运行.jmethods(“ mailReader”)时,它将列出所有可用的方法:
[2] …Run Code Online (Sandbox Code Playgroud) 我遇到了许多其他人似乎在某个时刻遇到的同样的rJava问题.R CMD INSTALL测试是否可以编译JNI程序时会发生错误.
输出建议运行R CMD javareconf -ed或sudo R CMD javareconf后者的输出是:
computer: home$ sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.6.0_31
Java home path : /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Java library path:
JNI linker flags : -framework JavaVM
JNI cpp flags : -I$(JAVA_HOME)/include
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
Run Code Online (Sandbox Code Playgroud)
然后在尝试安装rJava时,
computer: home$ R CMD INSTALL ~/Downloads/rJava_0.9-4.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library’
* …Run Code Online (Sandbox Code Playgroud) 我无法在centos R中安装rJava,它是安装rhdfs的基础.请帮忙看一下安装rJava时得到的输出.
> install.packages( c("rJava"))
Installing package into ‘/home/training/R/x86_64-redhat-linux-gnu-library/3.1’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://ftp.iitm.ac.in/cran/src/contrib/rJava_0.9-6.tar.gz'
Content type 'application/x-gzip' length 567515 bytes (554 Kb)
opened URL
==================================================
downloaded 554 Kb
* installing *source* package ‘rJava’ ...
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc... gcc -m64 -std=gnu99
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking …Run Code Online (Sandbox Code Playgroud) 当我从RStudio启动Rserve并尝试使用RSclient连接到Rserve时,如下面的代码所示:
#Loading Libraries
library(Rserve)
library(RSclient)
#Loading Rserve
Rserve(args='--no-save --slave')
#Open Connection to Rserve and Executing openNPL
conn <- RS.connect(port=6311)
RS.eval(conn,library(openNLP))
Run Code Online (Sandbox Code Playgroud)
一切正常.
但是如果我尝试从命令行启动Rserve(模拟Java的功能).作为代码:
#loading Rserve from command line, to simulate what java does.
#Using Mac OS 10.10.2
/Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --no-save --slave --RS-port 6311
#in RStudio
#Loading Rclient
library(RSclient)
#Loading Rserve
Rserve(args='--no-save --slave')
#Open Connection to Rserve and Executing openNPL
conn <- RS.connect(port=6311)
RS.eval(conn,library(openNLP))
Run Code Online (Sandbox Code Playgroud)
我在RStudio中收到此错误:
rsc_abort: connection closed by peer
Error in RS.eval(conn, library(openNLP)) :
read error …Run Code Online (Sandbox Code Playgroud) 我尝试使用xlsx包将命名数据帧列表保存到xlsx文件中.列表中的数据框都已命名,但如果它也可以处理列表中未命名的元素,那将会很好.
这是我写的函数:
toXLS <- function(myList,myFile) {
#Initialize output workbook
require(xlsx)
wb <- createWorkbook()
for (name in names(myList)) {
createSheet(wb, sheetName=name)
}
#Write blank workbook to file
saveWorkbook(wb, myFile)
#Write output data to saved workbook
for (name in names(myList)) {
write.xlsx(myList[[name]], myFile, sheetName=name)
}
}
myList <- list(a=data.frame(1,2,3),b=data.frame('a','b','c'),d=data.frame('01-01-1900'))
toXLS(myList,paste(getwd(),'/output.xlsx',sep=''))
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,但每次调用时都会覆盖输出工作簿write.xlsx.如何在工作簿中将data.frame作为工作表插入?
我没有与xlsx软件包结合,但它似乎为excel文件提供了一个很好的接口,只需要最少的设置.我也对其他好的选择持开放态度.
我正在运行以下脚本
library(xlsx);
wb <- loadWorkbook("/home/.../MyFile.xlsx") #works fine
sh <- getSheets(wb) #works fine
rw <- getRows(sh[[1]]) #-works fine
rc <- getCells(rw) # works fine
v <- lapply(rc, getCellValue) # works fine
v['21.4'] #works fine, returns the correct value
setCellValue(rc['21.4'], 'Hallo') #fails
Error in .jcall(cell, "V", "setCellValue", value) :
java.lang.ClassNotFoundException
Run Code Online (Sandbox Code Playgroud)
重复命令后
Error in .jcall(cell, "V", "setCellValue", value) :
RcallMethod: cannot determine object class
Run Code Online (Sandbox Code Playgroud)
它发生在RStudio和Ubuntu 12.04 32Bit下的经典R终端中。请帮忙!
我遇到了这个问题中讨论的相同错误消息,归结为:
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/home/anh/Rlibs/rJava/libs/rJava.so':
libjvm.so: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
问题是,在答案之后(即设置LD_LIBRARY_PATH指向libjvm.so)在命令行中解决了我的问题R,但Rstudio libjvm.so仍然无法找到.
系统规格:Ubuntu 14.04,R 3.1.1,Rstudio Desktop 0.98,apt-get install r-cran-rjava完成
我正在尝试在时间序列上使用 auto.arima 。现在我需要知道已选择的 arima 的顺序。返回值是 ARIMA 类型,它不保存任何地方的顺序。(或者我错过了这些价值观)。在代码片段和输出属性中给出。(这与 R 文档中的相同)
double[] list1 = {0, 0, 2, 1, 2, 10, 21, 0, 0, 3, 6, 5, 11, 51, 0, 11, 8, 6, 24, 25, 104, 0, 0, 6, 4, 5, 25, 71};
rconnection.assign("myData1", list1);
rconnection.eval("timeSeries1 <- ts(myData1,start=1,frequency="+staticBookingStage+")");
REXP fc = rconnection.eval("fitModel1 <- auto.arima(timeSeries1)");
System.out.println( fc.asList().names);
Run Code Online (Sandbox Code Playgroud)
输出
[coef、sigma2、var.coef、掩码、loglik、aic、arma、残差、调用、系列、代码、n.cond、nobs、模型、bic、aicc、x、拟合]
自从我昨天更新了 RStudio 以来,我无法让 Rstudio 放弃它对我的 excelfiles 的保留。
我尝试了以下方法无济于事:
unlink(fileName, recursive = TRUE)
file.info(fileName)
file.remove(fileName)
Run Code Online (Sandbox Code Playgroud)
file.info 提供以下内容:
size 2383465
isdir FALSE
mode 666
mtime 2020-08-06 11:02:03
ctime 2020-06-16 13:00:22
atime 2020-08-06 10:40:30
exe no
Run Code Online (Sandbox Code Playgroud)
RStudio 由管理员安装,但我个人在我的组织中没有管理员权限。即使取消链接也不允许 file.remove (但它不是目录所以......)。而且我真的不想删除它,只需让 Rstudio 取消保留即可。该文件将在 Excel 中修改(并保存,这是不允许的,因为 RStudio 持有它)。
更新:
我想这是一个错误,所以到目前为止我所有的谷歌搜索都没有找到解决方案。我想可以解决这个问题的一种解决方法是,如果我可以以编程方式重新初始化 JVM 实例。(例如, .rs.restartR()删除文件上的 R:s 保留)
更新 2:
openxlsx-package 发生了完全相同的错误,它不依赖于 Java,因此问题可能出在 R 上。
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=Swedish_Finland.1252 LC_CTYPE=Swedish_Finland.1252 LC_MONETARY=Swedish_Finland.1252 …Run Code Online (Sandbox Code Playgroud) 我有一个与 tabulizer 一起使用的脚本,但必须清理我的硬盘并重新安装 R,现在我什至无法下载和访问 tabulizer 库。我现在使用 R 版本 4.1.2 64 位,我想也许我需要使用早期版本的 R???这是我尝试安装 tabulizer 时收到的错误消息。
\ninstall.packages("tabulizer")\nInstalling package into \xe2\x80\x98C:/Users/cdonner/Documents/R/win-library/4.1\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\nWarning in install.packages :\n package \xe2\x80\x98tabulizer\xe2\x80\x99 is not available for this version of R\n\nA version of this package for your version of R might be available elsewhere,\nsee the ideas at\nhttps://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages\nRun Code Online (Sandbox Code Playgroud)\n我尝试去 Cran ( https://cran.r-project.org/src/contrib/Archive/tabulizer/ ) 并将库下载并解压到我的目录中,但随后我再次收到错误。有任何想法吗?谢谢。
\n