I used following code to calculating prediction in R using Java that code work fine it shows me output but, I want draw plot using same code on my predicted output code was given below
import org.rosuda.REngine.Rserve.RConnection;
public class demo{
public static void main(String args[]){
try{
System.out.println("INFO: Trying to connect to R ");
RConnection c = new RConnection();
c.eval("library('RMongo')");
c.eval(" db <- mongoDbConnect('dbname', '127.0.0.1', '27017')");
c.eval("query <- dbGetQuery(db,'collection_name','{\"hostId\" : \"300.3.3.3\"}')");
c.eval("date <- query$Date");
c.eval("cpu <- query$cpuUtilization");
c.eval("memory <- query$memory");
c.eval("df <- data.frame(date=1377843220)");
c.eval("res <- lm(cbind(memory,cpu)~ date -1 )");
int[] d= c.eval("predict(res,df)").asIntegers();
for (Integer td : d) {
System.out.println(td);
}
c.close();
}
catch(Exception e){
System.out.println("ERROR: In Connection to R ");
System.out.println("The Exception is "+ e.getMessage());
e.printStackTrace();
}
}
}
So any one knows how to draw plot using R in java code ?
Use a JFrame with a JGDPanel from JavaGD and plot something via JRI.
Look into JGD: http://cran.r-project.org/web/packages/JavaGD/JavaGD.pdf
Look into JRI: http://rosuda.org/JRI/
Another alternative is using R to generate the data you want and using native Java graphics libraries.
Related
The below code is throwing ‘stale element reference: element is not attached to the page document’
How to handle error message and show the result as a pass
Action- The below code is adding one image(which can be one or more) which inside another image
public void click_on_the_Add_to_collection_button_displaying_down_below_the_assert() {
List<WebElement> all_colection = driver.findElements(By.xpath("//li//button[#class='icon-button ' and #title='Add To Collection']"));
int collection_size = all_colection.size();
System.out.println("the collection size is " + collection_size);
Random ran = new Random();
all_colection.get(collection_size - 1).click();
crate_new_colection.sendKeys("newcollection#1");
}
Wrap with try..catch like shown below:
public void click_on_the_Add_to_collection_button_displaying_down_below_the_assert() {
try{
List<WebElement> all_colection = driver.findElements(By.xpath("//li//button[#class='icon-button ' and #title='Add To Collection']"));
int collection_size = all_colection.size();
System.out.println("the collection size is " + collection_size);
Random ran = new Random();
all_colection.get(collection_size - 1).click();
crate_new_colection.sendKeys("newcollection#1");
}catch (StaleElementReferenceException e){
System.err.println("Skipping the exception. Do not let it get down stack.");
}
}
P.S. - I kept your snake case unchanged but please do not use Python-like code style in Java.
In a Libreoffice Spreadsheet's sheet, I can get access to a Cell, but not to its value. I don't find my error, any feedback is welcome. Here is the code:
import java.io.File;
import java.io.IOException;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
public class MyClass {
protected Sheet dataSheet;
protected File dataCalcFile;
public static void main(String[] args) {
MyClass myClassInstance = new MyClass();
myClassInstance.loadData();
}
public void loadData() {
int numRows=0, numColumnas=0;
MutableCell cell=null;
try {
dataCalcFile = new File(""C:\\temp\\Data.ods"");
dataSheet = SpreadSheet.createFromFile(dataCalcFile).getSheet(0);
numRows = dataSheet.getRowCount();
System.out.println("Number of rows: " + numRows);
System.out.println("Cell at 0,0: " + dataSheet.getCellAt(0, 0));
System.out.println("Nullpointer Exception when getting cell value at 0,0: " + dataSheet.getValueAt(0, 0)); // *** THE INFAMOUS ONE ***
} catch (Exception e) {
System.out.println(e);
}
}
}
And here is the output at console:
Number of rows: 107
Cell at 0,0: <table:table-cell xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" table:style-name="ce1" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:value-type="string" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" calcext:value-type="string"><text:p xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">Carpeta</text:p></table:table-cell>
java.lang.NullPointerException
Just found that:
"Yes, LO 7 switched to OpenDocument 1.3. We're working on supporting it. In the mean time, you can change the format to "1.2 extended". Go to Options, then Load/Save, then General, then ODF format version."
It works for me, but am excited to hear my customers opinion...
I found that it must be some incompatibility between the library jopendocument and the newer versions of Libreoffice (7.x), as the code above:
Works as expected if the spreadsheet is edited with Libreoffice 6.4.3.2
Spits the null pointer exception out if the spreadsheet is edited with Libreoffice 7.0.1.2
In some version in between Calc produces an XML not understood by jopendocument.
Instead of using
getValueAt(0, 0),
you can use
getCellAt(0, 0).getElement().getValue()
It works for me.
I've prepared a class with a static method in Java 6, which I've exported to a JAR file:
package pl.poznan.put.stemutil;
public class Stemmer {
public static String stemText(String text) {
Set<String> c = new HashSet<String>();
...
return StringUtils.join(c, " ");
}
}
I import it to R with following code:
require(rJava)
.jinit("java/stem-util.jar")
stem = J("pl.poznan.put.stemutil.Stemmer")$stemText
Then, when I call it directly it works, e.g:
> stem("płotkami")
[1] "płotek płotka"
But when I'll try to use it with tm_map() function, something goes wrong:
> vc = VCorpus(vs, readerControl = list(language = "pl"))
> vc[[1]]
<<PlainTextDocument (metadata: 7)>>
mirki mirkówny zaczynam wolne jutra ( ͡° ͜ʖ ͡°) #pijzwykopem #piwozlidla
> vc = tm_map(vc, stem)
Komunikat ostrzegawczy:
In mclapply(content(x), FUN, ...) :
all scheduled cores encountered errors in user code
> vc[[1]]
[1] "Error in FUN(X[[1L]], ...) : \n Sorry, parameter type `NA' is ambiguous or not supported.\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in FUN(X[[1L]], ...): Sorry, parameter type `NA' is ambiguous or not supported.>
What am I doing incorrectly?
Finally adding mc.cores parameter has worked for me. However, It's more a workaround, than a proper solution.
vc = tm_map(vc, content_transformer(stem), mc.cores=1)
I am new to JT400. I am trying to invoke a test program in AS400 through JT400. Here is my code
public class TestRpg {
public static void main(String[] args){
try{
AS400 sys=new AS400("mydomain","username","password");
String number="asdf <= Return value from Java Input";
String lnsts="";
String amount="";
String lnofcd="";
AS400Text txt80 = new AS400Text(80);
AS400Text txt50 = new AS400Text(50);
ProgramParameter[] parmList = new ProgramParameter[4];
parmList[0] = new ProgramParameter( txt80.toBytes(number),80);
parmList[1] = new ProgramParameter( txt50.toBytes(lnsts),50);
parmList[2] = new ProgramParameter( txt80.toBytes(amount),80);
parmList[3] = new ProgramParameter( txt50.toBytes(lnofcd),50);
ProgramCall pgm = new ProgramCall(sys,"/QSYS.LIB/mylib.LIB/testrpg.PGM",parmList);
if (pgm.run()!=true) {
System.out.println("executed");
}else{
System.out.println("Output Data 0: " + (String)txt80.toObject( parmList[0].getOutputData() ) );
System.out.println("Output Data 1: " + (String)txt50.toObject( parmList[1].getOutputData() ) );
System.out.println("Output Data 2: " + (String)txt80.toObject( parmList[2].getOutputData() ) );
System.out.println("Output Data 3: " + (String)txt50.toObject( parmList[3].getOutputData() ) );
sys.disconnectService(AS400.COMMAND);
}
AS400Message[] messageList = pgm.getMessageList();
System.out.println(messageList.length);
for (int i=0; i < messageList.length; i++)
{
System.out.print ( messageList[i].getID() );
System.out.print ( ": " );
System.out.println( messageList[i].getText() );
}
sys.disconnectService(AS400.COMMAND);
}catch(Exception e) {
System.out.println(e.toString());
}
}
}
I had debug the code it's not giving any response after executing
pgm.run(). It is not even showing any exception. Programme is just holding at pgm.run() and not returning any thing.
As per the comments I got, I want to include the scenario I am trying to work on. In AS400 when we execute the testrpg.pgm program, it displays a screen with four input fields and some function keys to perform operations. My intention is to invoke f2 function key of that program from JT400. Is the approach I am following is the right way? Please suggest me
All program calls happen in batch so your program is most likely in MSGW on the server. Find it with wrkactjob and investigate the message it is waiting for, and give the appropriate action.
This is typically due to incorrectly formed parameters.
This is a common misunderstanding, so just for clarification for other readers:
Calling a Cobol/RPG program from Java is batch, just the same as calling the Cobol/RPG program from a Cobol/RPG/CL.
How to begin: Create a program which you can call from CL:
... declare and fill MYFIELD1, MYFIELD2 ...
CALL PGM(MYPGM) PARM(&MYFIELD1 &MYFIELD2)
...
If this works, it will also work from Java using jt400, if you:
call the right AS400 using correct credentials
call the right program in the right library
use the right number and lentgh of parameters
In case of crash as described (waiting forever), DSPMSG QSYSOPR will show an open message, like "MCH0801 = wrong number of parameters". D=Dump will create a spoolfile where you see which incoming parameters are filled with which content, or you see "undefined".
When I using Xuggler library on computer with more than one web-camera with Windows I need use "name" of this web-cameras to select device. I means that command "vfwcap 0" select only first web-camera and command "vfwcap 1" or "vfwcap 2" not allowed get access to other web-cameras. If I use:
$ ffmpeg -list_devices true -f dshow -i dummy
I can see the list of "names" this web-cameras. For example: "Logitech HD Webcam C270", "Logitech Webcam C210". If I using this names into the Xuggler library I get en error.
So, I can get access only to the first web-camera on Windows machine using Xuggler.
Can I get list of all devices in Windows from Xuggler and can I use this names into the Xuggler (I just want to use more than one web-camera at the same time)?
Maybe alternative way are exists?
Using this simple java program, you can get list of all webcam's available on your system.
import com.github.sarxos.webcam.Webcam;
public class CameraTest {
public static void main(String[] args) {
List<Webcam> list = Webcam.getWebcams();
for (int i = 0; i < list.size(); i++) {
try {
Webcam cam = list.get(i);
System.out.println("Found this Camera : "+cam.getName());
BufferedImage image = cam.getImage();
} catch (Exception e) {
System.out.println("Exception in cam : " + i);
}
}
}
}
Sample Output :
Found this Camera : TV CARD WDM VIDEO CAPTURE 0
Found this Camera : ManyCam Video Source 1
Found this Camera : DroidCam 2
You will have List of all camera's , So you can use anyone of them as you like.