如何在以下示例中禁止rJava
输出到控制台?
library(rJava)
TC <- J("edu.cens.spatial.RTileController")
dummy <- capture.output(suppressWarnings(suppressMessages(
res <- TC$getInstance(type="osm-bw")$getTileValues(4389,2691,13)
)))
尽管capture.output
,我仍然在控制台中获得以下内容:
java.lang.NullPointerException
at edu.cens.spatial.RTileController.getTileValues(RTileController.java:109)
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:498)
at RJavaTools.invokeMethod(RJavaTools.java:386)
编辑:在纯R控制台(没有Rstudio)中,我没有收到任何消息(但我必须先调用library("OpenStreetMap")
)。毕竟这可能是一个Rstudio问题......现在的问题是:如何在Rstudio中将Java输出抑制到R控制台?调用osmtile
时是否可以这样做?如下所述?
PS1:适用于osm
而不是osm-bw
PS2:我偶然发现了这个
tile <- OpenStreetMap::osmtile(x=4389,y=2691,zoom=13,type="osm-bw")
答案 0 :(得分:0)
在我的情况下,类似这样的东西 - NullPointer消息被抑制:
> s <- .jcall(obj, returnSig="V", method="nullcall")
Error in .jcall(obj, returnSig = "V", method = "nullcall") :
java.lang.NullPointerException: Exception
> suppressMessages(s <- .jcall(obj, returnSig="V", method="nullcall"))
要重现此代码,请执行以下操作:
创建文件(在R中)
dir.create("utils")
dir.create("target")
cat('package utils;
public class RUsingStringArray {
public void nullcall() throws NullPointerException {
throw new NullPointerException("Exception");
}
public static void main(String [] arg) {
RUsingStringArray obj = new RUsingStringArray();
obj.nullcall();
}
}', file="utils/RUsingStringArray.java")
编译java代码(在cmd / terminal中,最后一行不适用于Windows)
javac -d target utils/*.java
java -cp target utils/RUsingStringArray
Exception in thread "main" java.lang.NullPointerException: Exception
at utils.RUsingStringArray.nullcall(RUsingStringArray.java:19)
at utils.RUsingStringArray.main(RUsingStringArray.java:24)
export CLASSPATH=`pwd`/target
里面的R
library(rJava)
.jinit("C:/path_to_folder/target") # leave empty if CLASSPATH was set
obj <- .jnew("utils.RUsingStringArray")
s <- .jcall(obj, returnSig="V", method="nullcall")
suppressMessages(s <- .jcall(obj, returnSig="V", method="nullcall"))
Error in .jcall(obj, returnSig = "V", method = "nullcall") :
java.lang.NullPointerException: Exception