How to create a table structure in Java code?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create a table structure in Java code?

R devel mailing list
Hi all,

Using RConsole, it's easy to get data from the database that you can use in an R Command. Like this:

(Reference case)

irisQuery <- dbGetQuery(conn, "select * from iris")
boxM(irisQuery [,-5], irisQuery[,5])

----

(Actual case this posting is about)

Yet, if I'm getting that same (sample IRIS) data, say, in a web service possibly POSTED from a SQL command, that same data might look like this (portion of the included iris data set below). I'm thus not sure how to package the data so R likes it.

Example R-included IRIS data from SQL output:

5.1,3.5,1.4,0.2,setosa,
4.9,3,1.4,0.2,setosa,
4.7,3.2,1.3,0.2,setosa,
4.6,3.1,1.5,0.2,setosa,
5,3.6,1.4,0.2,setosa,

I've tried various combinations in code to achieve what's simple in RConsole, but I can't get R to accept my structure.

- I've tried just including the data in a string.
- I've tried wrapping the data with "data"
- I've tried wrapping the data with "data.frame" (as below).

Here's my latest attempt:

String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
"4.9,3,1.4,0.2,setosa\n" +
"4.7,3.2,1.3,0.2,setosa\n" +
"4.6,3.1,1.5,0.2,setosa\n" +
"5,3.6,1.4,0.2,setosa)" ;

// using parseAndEval below to give me actual error R is sending...
REXP rResponseObject = rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
if (rResponseObject.inherits("try-error"))
{
     System.out.println("R Serve Eval Exception : "+rResponseObject.asString());
}
REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," + tableRead + "[, 5])");  // FAILS <<

----

Error in the above case is:

Disconnected from the target VM, address: '127.0.0.1:51356', transport: 'socket'
org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
at net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)

Does the POSTed data need to be in a different format or am I just not framing it correctly?

Would appreciate any tips on how to package table data that might come from a SQL Query passed to Java code.

Thanks very much in advance,

- M

Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

Jan van der LAan-2

I suspect that you are looking for something like:

read.csv(textConnection(
"5.1,3.5,1.4,0.2,setosa
4.9,3,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5,3.6,1.4,0.2,setosa"
), header = FALSE)

HTH,
Jan



On 25-10-17 12:50, Morkus via R-devel wrote:

> Hi all,
>
> Using RConsole, it's easy to get data from the database that you can use in an R Command. Like this:
>
> (Reference case)
>
> irisQuery <- dbGetQuery(conn, "select * from iris")
> boxM(irisQuery [,-5], irisQuery[,5])
>
> ----
>
> (Actual case this posting is about)
>
> Yet, if I'm getting that same (sample IRIS) data, say, in a web service possibly POSTED from a SQL command, that same data might look like this (portion of the included iris data set below). I'm thus not sure how to package the data so R likes it.
>
> Example R-included IRIS data from SQL output:
>
> 5.1,3.5,1.4,0.2,setosa,
> 4.9,3,1.4,0.2,setosa,
> 4.7,3.2,1.3,0.2,setosa,
> 4.6,3.1,1.5,0.2,setosa,
> 5,3.6,1.4,0.2,setosa,
>
> I've tried various combinations in code to achieve what's simple in RConsole, but I can't get R to accept my structure.
>
> - I've tried just including the data in a string.
> - I've tried wrapping the data with "data"
> - I've tried wrapping the data with "data.frame" (as below).
>
> Here's my latest attempt:
>
> String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
> "4.9,3,1.4,0.2,setosa\n" +
> "4.7,3.2,1.3,0.2,setosa\n" +
> "4.6,3.1,1.5,0.2,setosa\n" +
> "5,3.6,1.4,0.2,setosa)" ;
>
> // using parseAndEval below to give me actual error R is sending...
> REXP rResponseObject = rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
> if (rResponseObject.inherits("try-error"))
> {
>       System.out.println("R Serve Eval Exception : "+rResponseObject.asString());
> }
> REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," + tableRead + "[, 5])");  // FAILS <<
>
> ----
>
> Error in the above case is:
>
> Disconnected from the target VM, address: '127.0.0.1:51356', transport: 'socket'
> org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
> at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
> at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
> at net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)
>
> Does the POSTed data need to be in a different format or am I just not framing it correctly?
>
> Would appreciate any tips on how to package table data that might come from a SQL Query passed to Java code.
>
> Thanks very much in advance,
>
> - M
>
> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

R devel mailing list
That's amazing! Thank you!!!

One follow up question, if that's OK?

If, instead of using hard-coded CSV, I read the CSV into a variable first, then it fails again with a parse error.

Code below.

So, if I read the CSV into a variable, do I need an additional wrapper method?

Seems like it should still work.

Thanks in advance for your reply.

 -M

String inputIris = "5.1,3.5,1.4,0.2,setosa\n" +
"4.9,3,1.4,0.2,setosa\n" +
"4.7,3.2,1.3,0.2,setosa\n" +
"4.6,3.1,1.5,0.2,setosa\n" +
"5,3.6,1.4,0.2,setosa\n" +
"5.4,3.9,1.7,0.4,setosa\n" +
"4.6,3.4,1.4,0.3,setosa";

REXP irisData =rConnection.eval(
"read.csv(textConnection(" + inputIris + "), header = FALSE)");

Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.

> -------- Original Message --------
> Subject: Re: [Rd] How to create a table structure in Java code?
> Local Time: October 26, 2017 7:45 AM
> UTC Time: October 26, 2017 11:45 AM
> From: [hidden email]
> To: [hidden email]
>
> I suspect that you are looking for something like:
>
> read.csv(textConnection(
> "5.1,3.5,1.4,0.2,setosa
> 4.9,3,1.4,0.2,setosa
> 4.7,3.2,1.3,0.2,setosa
> 4.6,3.1,1.5,0.2,setosa
> 5,3.6,1.4,0.2,setosa"
> ), header = FALSE)
>
> HTH,
> Jan
>
> On 25-10-17 12:50, Morkus via R-devel wrote:
>
>> Hi all,
>> Using RConsole, it's easy to get data from the database that you can use in an R Command. Like this:
>> (Reference case)
>> irisQuery <- dbGetQuery(conn, "select * from iris")
>> boxM(irisQuery [,-5], irisQuery[,5])
>> ---------------------------------------------------------------
>>
>> (Actual case this posting is about)
>> Yet, if I'm getting that same (sample IRIS) data, say, in a web service possibly POSTED from a SQL command, that same data might look like this (portion of the included iris data set below). I'm thus not sure how to package the data so R likes it.
>> Example R-included IRIS data from SQL output:
>> 5.1,3.5,1.4,0.2,setosa,
>> 4.9,3,1.4,0.2,setosa,
>> 4.7,3.2,1.3,0.2,setosa,
>> 4.6,3.1,1.5,0.2,setosa,
>> 5,3.6,1.4,0.2,setosa,
>> I've tried various combinations in code to achieve what's simple in RConsole, but I can't get R to accept my structure.
>>
>> - I've tried just including the data in a string.
>> - I've tried wrapping the data with "data"
>> - I've tried wrapping the data with "data.frame" (as below).
>>
>> Here's my latest attempt:
>> String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
>> "4.9,3,1.4,0.2,setosa\n" +
>> "4.7,3.2,1.3,0.2,setosa\n" +
>> "4.6,3.1,1.5,0.2,setosa\n" +
>> "5,3.6,1.4,0.2,setosa)" ;
>> // using parseAndEval below to give me actual error R is sending...
>> REXP rResponseObject = rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
>> if (rResponseObject.inherits("try-error"))
>> {
>> System.out.println("R Serve Eval Exception : "+rResponseObject.asString());
>> }
>> REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," + tableRead + "[, 5])"); // FAILS <<
>> ---------------------------------------------------------------
>>
>> Error in the above case is:
>> Disconnected from the target VM, address: '127.0.0.1:51356', transport: 'socket'
>> org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
>> at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
>> at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
>> at net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)
>> Does the POSTed data need to be in a different format or am I just not framing it correctly?
>> Would appreciate any tips on how to package table data that might come from a SQL Query passed to Java code.
>> Thanks very much in advance,
>>
>> - M
>>
>> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
>> [[alternative HTML version deleted]]
>> ---------------------------------------------------------------
>>
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ---------------------------------------------------------------
>
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

Simon Urbanek
You are entering the quoting hell - you are missing quotes and escapes for \n. it would be much more reasonable to use the rConnection.assign method instead of pasting any content through the parser.

Cheers,
Simon


> On Oct 26, 2017, at 9:59 AM, Morkus via R-devel <[hidden email]> wrote:
>
> That's amazing! Thank you!!!
>
> One follow up question, if that's OK?
>
> If, instead of using hard-coded CSV, I read the CSV into a variable first, then it fails again with a parse error.
>
> Code below.
>
> So, if I read the CSV into a variable, do I need an additional wrapper method?
>
> Seems like it should still work.
>
> Thanks in advance for your reply.
>
> -M
>
> String inputIris = "5.1,3.5,1.4,0.2,setosa\n" +
> "4.9,3,1.4,0.2,setosa\n" +
> "4.7,3.2,1.3,0.2,setosa\n" +
> "4.6,3.1,1.5,0.2,setosa\n" +
> "5,3.6,1.4,0.2,setosa\n" +
> "5.4,3.9,1.7,0.4,setosa\n" +
> "4.6,3.4,1.4,0.3,setosa";
>
> REXP irisData =rConnection.eval(
> "read.csv(textConnection(" + inputIris + "), header = FALSE)");
>
> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
>
>> -------- Original Message --------
>> Subject: Re: [Rd] How to create a table structure in Java code?
>> Local Time: October 26, 2017 7:45 AM
>> UTC Time: October 26, 2017 11:45 AM
>> From: [hidden email]
>> To: [hidden email]
>>
>> I suspect that you are looking for something like:
>>
>> read.csv(textConnection(
>> "5.1,3.5,1.4,0.2,setosa
>> 4.9,3,1.4,0.2,setosa
>> 4.7,3.2,1.3,0.2,setosa
>> 4.6,3.1,1.5,0.2,setosa
>> 5,3.6,1.4,0.2,setosa"
>> ), header = FALSE)
>>
>> HTH,
>> Jan
>>
>> On 25-10-17 12:50, Morkus via R-devel wrote:
>>
>>> Hi all,
>>> Using RConsole, it's easy to get data from the database that you can use in an R Command. Like this:
>>> (Reference case)
>>> irisQuery <- dbGetQuery(conn, "select * from iris")
>>> boxM(irisQuery [,-5], irisQuery[,5])
>>> ---------------------------------------------------------------
>>>
>>> (Actual case this posting is about)
>>> Yet, if I'm getting that same (sample IRIS) data, say, in a web service possibly POSTED from a SQL command, that same data might look like this (portion of the included iris data set below). I'm thus not sure how to package the data so R likes it.
>>> Example R-included IRIS data from SQL output:
>>> 5.1,3.5,1.4,0.2,setosa,
>>> 4.9,3,1.4,0.2,setosa,
>>> 4.7,3.2,1.3,0.2,setosa,
>>> 4.6,3.1,1.5,0.2,setosa,
>>> 5,3.6,1.4,0.2,setosa,
>>> I've tried various combinations in code to achieve what's simple in RConsole, but I can't get R to accept my structure.
>>>
>>> - I've tried just including the data in a string.
>>> - I've tried wrapping the data with "data"
>>> - I've tried wrapping the data with "data.frame" (as below).
>>>
>>> Here's my latest attempt:
>>> String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
>>> "4.9,3,1.4,0.2,setosa\n" +
>>> "4.7,3.2,1.3,0.2,setosa\n" +
>>> "4.6,3.1,1.5,0.2,setosa\n" +
>>> "5,3.6,1.4,0.2,setosa)" ;
>>> // using parseAndEval below to give me actual error R is sending...
>>> REXP rResponseObject = rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
>>> if (rResponseObject.inherits("try-error"))
>>> {
>>> System.out.println("R Serve Eval Exception : "+rResponseObject.asString());
>>> }
>>> REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," + tableRead + "[, 5])"); // FAILS <<
>>> ---------------------------------------------------------------
>>>
>>> Error in the above case is:
>>> Disconnected from the target VM, address: '127.0.0.1:51356', transport: 'socket'
>>> org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
>>> at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
>>> at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
>>> at net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)
>>> Does the POSTed data need to be in a different format or am I just not framing it correctly?
>>> Would appreciate any tips on how to package table data that might come from a SQL Query passed to Java code.
>>> Thanks very much in advance,
>>>
>>> - M
>>>
>>> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
>>> [[alternative HTML version deleted]]
>>> ---------------------------------------------------------------
>>>
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>> ---------------------------------------------------------------
>>
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

Jan van der LAan-2
In reply to this post by R devel mailing list
As Joris mentioned this question does not belong on R-devel, but on R-help.

When replying to your earlier question I missed that I was replying to
R-devel and not R-help. R-devel: sorry for that.

So, please post any follow up questions to R-help.

--
Jan


On 26-10-17 15:59, Morkus wrote:

> *_That's amazing! Thank you!!!_*
>
> One follow up question, if that's OK?
>
> If, instead of using hard-coded CSV, I read the CSV into a variable
> _first_, then it fails again with a parse error.
>
> Code below.
>
> So, if I read the CSV into a variable, do I need an additional wrapper
> method?
>
> Seems like it should still work.
>
> Thanks in advance for your reply.
>
>   -M
>
> *String inputIris *= *"5.1,3.5,1.4,0.2,setosa**\n**" *+
> *"4.9,3,1.4,0.2,setosa**\n**" *+
> *"4.7,3.2,1.3,0.2,setosa**\n**" *+
> *"4.6,3.1,1.5,0.2,setosa**\n**" *+
> *"5,3.6,1.4,0.2,setosa**\n**" *+
> *"5.4,3.9,1.7,0.4,setosa**\n**" *+
> *"4.6,3.4,1.4,0.3,setosa"*;
>
> *REXP irisData *=*rConnection*.*eval*(
> *"read.csv(textConnection(" *+ *inputIris *+ *"), header = FALSE)"*);
>
> Sent from ProtonMail <https://protonmail.com>, Swiss-based encrypted email.
>
>
>> -------- Original Message --------
>> Subject: Re: [Rd] How to create a table structure in Java code?
>> Local Time: October 26, 2017 7:45 AM
>> UTC Time: October 26, 2017 11:45 AM
>> From: [hidden email]
>> To: [hidden email]
>>
>>
>> I suspect that you are looking for something like:
>>
>> read.csv(textConnection(
>> "5.1,3.5,1.4,0.2,setosa
>> 4.9,3,1.4,0.2,setosa
>> 4.7,3.2,1.3,0.2,setosa
>> 4.6,3.1,1.5,0.2,setosa
>> 5,3.6,1.4,0.2,setosa"
>> ), header = FALSE)
>>
>> HTH,
>> Jan
>>
>>
>>
>> On 25-10-17 12:50, Morkus via R-devel wrote:
>>
>>     Hi all,
>>     Using RConsole, it's easy to get data from the database that you
>>     can use in an R Command. Like this:
>>     (Reference case)
>>     irisQuery <- dbGetQuery(conn, "select * from iris")
>>     boxM(irisQuery [,-5], irisQuery[,5])
>>     ------------------------------------------------------------------------
>>
>>     (Actual case this posting is about)
>>     Yet, if I'm getting that same (sample IRIS) data, say, in a web
>>     service possibly POSTED from a SQL command, that same data might
>>     look like this (portion of the included iris data set below). I'm
>>     thus not sure how to package the data so R likes it.
>>     Example R-included IRIS data from SQL output:
>>     5.1,3.5,1.4,0.2,setosa,
>>     4.9,3,1.4,0.2,setosa,
>>     4.7,3.2,1.3,0.2,setosa,
>>     4.6,3.1,1.5,0.2,setosa,
>>     5,3.6,1.4,0.2,setosa,
>>     I've tried various combinations in code to achieve what's simple
>>     in RConsole, but I can't get R to accept my structure.
>>
>>       * I've tried just including the data in a string.
>>       * I've tried wrapping the data with "data"
>>       * I've tried wrapping the data with "data.frame" (as below).
>>
>>     Here's my latest attempt:
>>     String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
>>     "4.9,3,1.4,0.2,setosa\n" +
>>     "4.7,3.2,1.3,0.2,setosa\n" +
>>     "4.6,3.1,1.5,0.2,setosa\n" +
>>     "5,3.6,1.4,0.2,setosa)" ;
>>     // using parseAndEval below to give me actual error R is sending...
>>     REXP rResponseObject =
>>     rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
>>     if (rResponseObject.inherits("try-error"))
>>     {
>>     System.out.println("R Serve Eval Exception :
>>     "+rResponseObject.asString());
>>     }
>>     REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," +
>>     tableRead + "[, 5])"); // FAILS <<
>>     ------------------------------------------------------------------------
>>
>>     Error in the above case is:
>>     Disconnected from the target VM, address: '127.0.0.1:51356',
>>     transport: 'socket'
>>     org.rosuda.REngine.REngineException: eval failed, request status:
>>     R parser: syntax error
>>     at
>>     org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
>>     at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
>>     at
>>     net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)
>>     Does the POSTed data need to be in a different format or am I just
>>     not framing it correctly?
>>     Would appreciate any tips on how to package table data that might
>>     come from a SQL Query passed to Java code.
>>     Thanks very much in advance,
>>
>>       * M
>>
>>     Sent from ProtonMail <https://protonmail.com>, Swiss-based
>>     encrypted email.
>>     [[alternative HTML version deleted]]
>>     ------------------------------------------------------------------------
>>
>>     [hidden email] <mailto:[hidden email]> mailing list
>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>> ------------------------------------------------------------------------
>>
>> [hidden email] <mailto:[hidden email]> mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

R devel mailing list
In reply to this post by Simon Urbanek
Thanks! I just figured it out (thanks to "Beyond Compare") and was coming here to post back.

The boxM test doesn't work with that (now, finally working) REXP structure, but I probably now need to create a table or something and parse that structure.

So much fun! :)

Thanks again.

- M

Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.

> -------- Original Message --------
> Subject: Re: [Rd] How to create a table structure in Java code?
> Local Time: October 26, 2017 10:13 AM
> UTC Time: October 26, 2017 2:13 PM
> From: [hidden email]
> To: Morkus <[hidden email]>
> Jan van der Laan <[hidden email]>, [hidden email] <[hidden email]>
>
> You are entering the quoting hell - you are missing quotes and escapes for \n. it would be much more reasonable to use the rConnection.assign method instead of pasting any content through the parser.
>
> Cheers,
> Simon
>
>> On Oct 26, 2017, at 9:59 AM, Morkus via R-devel [hidden email] wrote:
>> That's amazing! Thank you!!!
>> One follow up question, if that's OK?
>> If, instead of using hard-coded CSV, I read the CSV into a variable first, then it fails again with a parse error.
>> Code below.
>> So, if I read the CSV into a variable, do I need an additional wrapper method?
>> Seems like it should still work.
>> Thanks in advance for your reply.
>> -M
>> String inputIris = "5.1,3.5,1.4,0.2,setosa\n" +
>> "4.9,3,1.4,0.2,setosa\n" +
>> "4.7,3.2,1.3,0.2,setosa\n" +
>> "4.6,3.1,1.5,0.2,setosa\n" +
>> "5,3.6,1.4,0.2,setosa\n" +
>> "5.4,3.9,1.7,0.4,setosa\n" +
>> "4.6,3.4,1.4,0.3,setosa";
>> REXP irisData =rConnection.eval(
>> "read.csv(textConnection(" + inputIris + "), header = FALSE)");
>> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
>>
>>> -------- Original Message --------
>>> Subject: Re: [Rd] How to create a table structure in Java code?
>>> Local Time: October 26, 2017 7:45 AM
>>> UTC Time: October 26, 2017 11:45 AM
>>> From: [hidden email]
>>> To: [hidden email]
>>> I suspect that you are looking for something like:
>>> read.csv(textConnection(
>>> "5.1,3.5,1.4,0.2,setosa
>>> 4.9,3,1.4,0.2,setosa
>>> 4.7,3.2,1.3,0.2,setosa
>>> 4.6,3.1,1.5,0.2,setosa
>>> 5,3.6,1.4,0.2,setosa"
>>> ), header = FALSE)
>>> HTH,
>>> Jan
>>> On 25-10-17 12:50, Morkus via R-devel wrote:
>>>
>>>> Hi all,
>>>> Using RConsole, it's easy to get data from the database that you can use in an R Command. Like this:
>>>> (Reference case)
>>>> irisQuery <- dbGetQuery(conn, "select * from iris")
>>>> boxM(irisQuery [,-5], irisQuery[,5])
>>>>
>>>> (Actual case this posting is about)
>>>> Yet, if I'm getting that same (sample IRIS) data, say, in a web service possibly POSTED from a SQL command, that same data might look like this (portion of the included iris data set below). I'm thus not sure how to package the data so R likes it.
>>>> Example R-included IRIS data from SQL output:
>>>> 5.1,3.5,1.4,0.2,setosa,
>>>> 4.9,3,1.4,0.2,setosa,
>>>> 4.7,3.2,1.3,0.2,setosa,
>>>> 4.6,3.1,1.5,0.2,setosa,
>>>> 5,3.6,1.4,0.2,setosa,
>>>> I've tried various combinations in code to achieve what's simple in RConsole, but I can't get R to accept my structure.
>>>>
>>>> - I've tried just including the data in a string.
>>>> - I've tried wrapping the data with "data"
>>>> - I've tried wrapping the data with "data.frame" (as below).
>>>>
>>>> Here's my latest attempt:
>>>> String tableRead = "data.frame(5.1,3.5,1.4,0.2,setosa\n" +
>>>> "4.9,3,1.4,0.2,setosa\n" +
>>>> "4.7,3.2,1.3,0.2,setosa\n" +
>>>> "4.6,3.1,1.5,0.2,setosa\n" +
>>>> "5,3.6,1.4,0.2,setosa)" ;
>>>> // using parseAndEval below to give me actual error R is sending...
>>>> REXP rResponseObject = rConnection.parseAndEval("try(eval("+tableRead+"),silent=TRUE)");
>>>> if (rResponseObject.inherits("try-error"))
>>>> {
>>>> System.out.println("R Serve Eval Exception : "+rResponseObject.asString());
>>>> }
>>>> REXP boxMResult = rConnection.eval("boxM("+ tableRead+ "[,-5]," + tableRead + "[, 5])"); // FAILS <<
>>>>
>>>> Error in the above case is:
>>>> Disconnected from the target VM, address: '127.0.0.1:51356', transport: 'socket'
>>>> org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
>>>> at org.rosuda.REngine.Rserve.RConnection.parseAndEval(RConnection.java:454)
>>>> at org.rosuda.REngine.REngine.parseAndEval(REngine.java:108)
>>>> at net.example.start_r_fromjava.RStatisticsExample.main(RStatisticsExample.java:151)
>>>> Does the POSTed data need to be in a different format or am I just not framing it correctly?
>>>> Would appreciate any tips on how to package table data that might come from a SQL Query passed to Java code.
>>>> Thanks very much in advance,
>>>>
>>>> - M
>>>>
>>>> Sent from [ProtonMail](https://protonmail.com), Swiss-based encrypted email.
>>>> [[alternative HTML version deleted]]
>>>>
>>>> [hidden email] mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>> ---------------------------------------------------------------
>>>
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>> [[alternative HTML version deleted]]
>>
>> ---------------------------------------------------------------
>>
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to create a table structure in Java code?

R devel mailing list
ALMOST THERE!!!!

I have one small remaining problem.

Now that I can read in the CSV data, thanks again for that help, I create a data frame from all the REXPDouble and the one REXPString object and I'm then trying to do a boxM (Box's M) test.

The documentation says that a data frame should work with boxM so maybe I'm back in "quote hell"?

I've tried the "problem command" (two lines below) both with putting the entire boxM command inside the quotes and having the "myDf" be outside the quotes.

The line of code below that still throws a parse error is:

REXP boxMResult = rConnection.parseAndEval( "boxM(" + myDf+ "[,-5], " + myDf + " [, 5])");

Thanks again to all...this is an incredible forum!!!

I offer a virtual beer to whomever helps! :)

----

ENTIRE CODE SNIPPET:

rConnection.eval("library('biotools')");

String inputIris = "5.1,3.5,1.4,0.2,setosa\n" +
"4.9,3,1.4,0.2,setosa\n" +
"5.5,2.4,3.7,1,versicolor\n" +
"5.8,2.7,3.9,1.2,versicolor\n" +
"6,2.7,5.1,1.6,versicolor\n" +
"7.9,3.8,6.4,2,virginica\n" +
"6.4,2.8,5.6,2.2,virginica\n" +
"6.3,2.8,5.1,1.5,virginica\n" +
"6.1,2.6,5.6,1.4,virginica";

(some IRIS data removed above for brevity.)

List tableRead = rConnection.eval(
"read.csv(textConnection(\"" + inputIris + "\"), header = FALSE)").asList();    // Yes works!!!

double[] d1 = ((REXPVector) ((RList) tableRead).get(0)).asDoubles();
double[] d2 = ((REXPVector) ((RList) tableRead).get(1)).asDoubles();
double[] d3 = ((REXPVector) ((RList) tableRead).get(2)).asDoubles();
double[] d4 = ((REXPVector) ((RList) tableRead).get(3)).asDoubles();
int[] d5 = ((REXPFactor) ((RList) tableRead).get(4)).asIntegers() ;

// create data frame with data extracted from tableRead List object.REXP myDf = REXP.createDataFrame(new RList(
new REXP[]
{
new REXPDouble(d1),
new REXPDouble(d2),
new REXPDouble(d3),
new REXPDouble(d4),
new REXPInteger(d5)
}));

>>>> ERROR HERE >>>> REXP boxMResult = rConnection.parseAndEval( "boxM(" + myDf+ "[,-5], " + myDf + " [, 5])");

eval failed, request status: R parser: syntax error
org.rosuda.REngine.REngineException: eval failed, request status: R parser: syntax error
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel