Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

OK. Statistics::R seens to be working fine now

I just ignores the error message in the test and went ahead with the installation, just as was told me in here:

But still I have two problems (that I hope are the last ones).

First of all, when I try to pass my table from perl to R with the comand "$R -> set( 'table', \@lines );", it returns this error:

Problem running this R command: g1 <- table[, 3]; Got the error: wrong number of dimmensions

I scaped that creating an output file from perl (named "data_freq.txt.out" and telling R to import from that. But this doesnt seens the right way to do it.

Second. When I try to return a table from R to perl, importing it to an array (in "my @results1 = $R->get('g1');"), I would like all the values in the table to be passed to this array (@results1). So I could shift and pop values from that array to use in other perl things (Mostly to create an nice output file)

Instead, what I get is an array with a single element that is some kind of octodecimal value (I guess)

How can I fix that?

Someone already told me to use "$results1" instead of "@results1". But that gives me the same output. Besides, it will create an scalar and that is not what I want

NOTE: "my $media = $R->get('meang1');" do work! I guess thats is because its a single value.

HERE are my codes, wich I call with "./ data_freq.txt"

#!/usr/bin/perl use strict; use warnings; use Statistics::R; my @lines; my $line; my $csv_check = shift @ARGV; #Mark for csv check----- @ARGV = $csv_check; while ( defined( $line = <> ) ) { push @lines, $line; } shift @lines; save_array_output(@lines, ".out"); print "\@lines = \n @lines "; my $R = Statistics::R->new() ; $R->startR ; $R->run('library(genetics);'); $R->run('setwd("/home/user/tests/");'); #$R->set( 'table', \@lines ); $R->run('table <- read.table("/home/user/tests/data_freq.txt.out");'); $R->run('g1 <- table[, 3];'); $R->run('meang1 <- mean(g1)'); $R->run('print(meang1);'); $R->run('write.table(g1, "g1.txt");'); $R->run('write.table(meang1, "meang1.txt");'); my $media = $R->get('meang1'); my $results1 = $R->get('g1'); print "\$results1 = ", $results1, "\n"; print "\$media = ", $media, "\n"; $R->stopR() ; #----------------------------------------------- sub save_array_output { @lines = @_; my $mid_name = pop @lines; my $output_name = $csv_check.$mid_name; open(MYOUTFILE, ">:utf8", $output_name); # Open output file print MYOUTFILE "@lines \n"; close(MYOUTFILE); # Close output file }


Locus Alleles frequencies Bet01 230 0.166666666666666657415 Bet01 238 0.500000000000000000000 Bet01 244 0.333333333333333314830 Bet05 101 0.285714285714285698425 Bet05 103 0.500000000000000000000 Bet05 105 0.142857142857142849213 Bet05 109 0.071428571428571424606 Bet06 138 0.055555555555555552472 Bet06 140 0.111111111111111104943 Bet06 146 0.833333333333333370341 Bet12 108 0.111111111111111104943 Bet12 110 0.055555555555555552472 Bet12 112 0.444444444444444419773 Bet12 114 0.277777777777777790114 Bet12 116 0.055555555555555552472 Bet12 118 0.055555555555555552472

In reply to Re: Calling R in perl by blackzero
in thread Calling R in perl by blackzero

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-06-16 20:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.