从R执行Perl脚本 [英] Execute a Perl script from R
问题描述
我需要将Perl程序作为较大的R程序的一部分来执行.
I need to execute a Perl program as part of a larger R program.
R代码生成一系列具有不同扩展名的输出文件,例如.out
或.lis
.
The R code generates a series of output files with different extensions, for instance .out
or .lis
.
我有一个Perl程序,可以将这些文件转换为CSV.
I have a Perl program that converts those files to CSV.
我已经看到Perl参数在R上执行,但是没有这种复杂性.
I've seen Perl arguments executed on R, but nothing with this complexity.
@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 $outfile\n";
open( CSVFILE, ">$csvfile" ) || die "ERROR: Unable to open $csvfile\n";
$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\n";
}
close( OUTFILE );
close( CSVFILE );
}
有什么方法可以将Perl代码集成到我的R代码中?我可以开发一个执行相同功能的R程序.但是我不知道从哪里开始将.lis
或.out
文件转换为.csv
.
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
.
推荐答案
使用R的系统调用进行调用:
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
但是,我必须同意@ikegami,那里有更好的模块来处理CSV数据.
However, I must agree with @ikegami, there are better modules to handle CSV data.
这篇关于从R执行Perl脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!