http://qs321.pair.com?node_id=1230256

msanchez78 has asked for the wisdom of the Perl Monks concerning the following question:

seeking the wisdom of the perl monks ...

within R i submit this code:

> data <- c("2016-03-15 13","2016-03-16 23","2016-03-17 06","2016-03-1 +8 15","2016-03-19 08","2016-03-20 21") > datevec <- strptime(data,"%Y-%m-%d %H") > difftime(datevec[-length(datevec)],datevec[-1],units="hours")
which correctly produces this output:
Time differences in hours
1 -34 -7 -33 -17 -37

notice the two lines. now, in perl i have this code:
$R->set( 'data', "2016-03-15 13","2016-03-16 23","2016-03-17 06","2016 +-03-18 15","2016-03-19 08","2016-03-20 21" ); $R->run( q`datevec <- strptime(data,"%Y-%m-%d %H")` ); $R->run( q`xx <- difftime(datevec[-length(datevec)],datevec[-1],units= +"hours")` ); @Rres = $R->get('xx'); print Dumper(\@Rres);
which correctly produces this result:
$VAR1 = [ [ 'Time', 'difference', 'of', 'hours' ] ];

the problem is that no matter what i do i cannot get to the second output line from R ... the line 1 -34 -7 -33 -17 -37

any suggestion on how i can get to the second (and in many cases n-number) of subsequent output lines from R? thanks!

Replies are listed 'Best First'.
Re: Entire Results from R
by Veltro (Hermit) on Feb 20, 2019 at 16:31 UTC

    Can you try to print the raw result? Maybe this will help to see what is wrong.

    my $tmp_str = $R->run(qq`print($varname)`); print $tmp_str . "\n" ;

    I got the previous line from the 'get' method of the module Statistics::R which I assume you are using. The 'get' method is doing a whole bunch of other stuff (which may cause the problem), so this line might show the entire result. Another small thing is that you are using @Rres = $R->get('xx'); and the method is returning a array reference. Although it is not entirely incorrect I do suggest using my $Rres = $R->get('xx'); print Dumper($Rres); instead

    Further I don't know much about R

    edit: Oh, and $varname should be 'datevec' or 'xx' or in this case of course.

      hi ... thank you for your prompt reply. i tried
      my $tmp_str = $R->run(qq`print($varname)`); print $tmp_str . "\n" ;
      the result is the same --- the first line only:

      Time difference of hours

      yikes!

        I would go in and manually edit this line to 1

        use constant DEBUG => 0; # debugging messages

        And see what you get.

Re: Entire Results from R
by haukex (Archbishop) on Feb 21, 2019 at 09:14 UTC

    Your use of $R->set() is incorrect: to set multiple values, you have to pass an arrayref: $R->set( 'data', ["2016-03-15 13","2016-03-16 23","2016-03-17 06","2016-03-18 15","2016-03-19 08","2016-03-20 21"] ); Then the code works for me.

      ++haukex I put together a short script to confirm that your solution works (I was curious about Statistics::R since I had not used it before). This code,
      #!/usr/bin/env perl use Statistics::R; use Data::Dumper; # Create a communication bridge with R and start R my $R = Statistics::R->new(); $R->set( 'data', [ "2016-03-15 13", "2016-03-16 23", "2016-03-17 06", "2016-03-18 15", "2016-03-19 08", "2016-03-20 21" ] ); $R->run( q`datevec <- strptime(data,"%Y-%m-%d %H")` ); $R->run( q`xx <- difftime(datevec[-length(datevec)],datevec[-1],units= +"hours")` ); @Rres = $R->get('xx'); print Dumper(\@Rres); exit;
      Gives this output,
      $VAR1 = [ [ '[1]', '-34', '-7', '-33', '-17', '-37' ] ];

        Thanks for the example! I also submitted a pull request that adds a "too many arguments" error to ->set.