Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

Execute a Perl script from R

I need to execute a Perl program as part of a larger R program.

The R code generates a series of output files with different extensions, for instance .out or .lis.

I have a Perl program that converts those files to CSV.

I've seen Perl arguments executed on R, but nothing with this complexity.

@outfiles = glob( "*.lis" );

foreach $outfile ( @outfiles ) {

    print $outfile, "
";

    $outfile =~ /(S+)lis$/;
    $csvfile = $1 . "lis.csv";
    print $csvfile, "
";

    open( OUTFILE, "$outfile" )  || die "ERROR: Unable to open $outfile
";
    open( CSVFILE, ">$csvfile" ) || die "ERROR: Unable to open $csvfile
";

    $lineCnt = 0;

    while ( $outline = <OUTFILE> ) {
        chomp( $outline );

        $lineCnt++;

        $outline =~ s/^s+//;    # Remove whitespace at the beginning of the line
        if ( $lineCnt == 1 ) {
            $outline =~ s/,/./g;    # Replace all the commas with periods in the hdr line
        }

        $outline =~ s/s+/,/g;       # Replace remaining whitespace delimiters with a comma
        print CSVFILE "$outline
";
    }

    close( OUTFILE );
    close( CSVFILE );
}

Is there any way I can integrate the Perl code into my R code? I could develop an R program that does the same. But I wouldn't know where to start to convert a .lis or .out file to .csv.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Call it by using R's system call:

my.seed <- as.numeric(try(system(" perl -e 'print int(rand(1000000))'", intern = TRUE))) #get random number :D

However, I must agree with @ikegami, there are better modules to handle CSV data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...