从 R 执行 Perl 脚本



>我需要执行一个Perl程序作为更大的R程序的一部分。

R 代码生成一系列具有不同扩展名的输出文件,例如 .out.lis

我有一个Perl程序可以将这些文件转换为CSV。

我见过在 R 上执行的 Perl 参数,但没有这么复杂的内容。

@outfiles = glob( "*.lis" );
foreach $outfile ( @outfiles ) {
    print $outfile, "n";
    $outfile =~ /(S+)lis$/;
    $csvfile = $1 . "lis.csv";
    print $csvfile, "n";
    open( OUTFILE, "$outfile" )  || die "ERROR: Unable to open $outfilen";
    open( CSVFILE, ">$csvfile" ) || die "ERROR: Unable to open $csvfilen";
    $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 "$outlinen";
    }
    close( OUTFILE );
    close( CSVFILE );
}

有什么方法可以将Perl代码集成到我的R代码中吗?我可以开发一个做同样事情的R程序。但是我不知道从哪里开始将.lis.out文件转换为.csv.

使用 R 的系统调用来调用它:

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

但是,我必须同意@ikegami,有更好的模块来处理CSV数据。

最新更新