【问题标题】:Problems running perl programe through R using system command使用系统命令通过 R 运行 perl 程序的问题
【发布时间】:2012-07-28 04:09:47
【问题描述】:

当我使用系统运行我的 perl 程序时,我收到以下错误。

> system("perl txt2xlsx.pl", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl txt2xlsx.pl' had status 2 

但是,当我从终端/控制台运行脚本时,它工作得非常好。 出于测试目的,我将另一个仅在屏幕上打印“输出”的 perl 脚本放入同一目录并运行它:

> system("perl test.pl",     intern=TRUE, wait=TRUE)

[1]“输出”

工作正常。

当您不带任何参数运行我的 txt2xlsx.pl 脚本时,它会打印:

Usage: txt2xlsx.pl <directory> <output file>

谁能想象为什么 R 会返回这个错误?在我更新到最新的 perl 版本之前它运行良好:

This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-multi-2level

干杯

这是我的一些 perl 脚本:

#!/usr/bin/perl

use Excel::Writer::XLSX;

# Check command line args
if($#ARGV != 1) {
     die("Usage: $0 <directory> <output file>\n\n");
}

my $dir = $ARGV[0];
my $output = $ARGV[1];

print "Output    : $output\n";
print "Directory : $dir\n";

# Create excel workbook
my $wb = Excel::Writer::XLSX->new($output);

# Process files
foreach (glob("$dir/*.txt")) {

....

}
#EOF

我做了一些更多的测试并减少了 perl 脚本以仅打印两个参数:

#!/usr/bin/perl

#use Excel::Writer::XLSX;

# Check command line args
if($#ARGV != 1) {
     die("Usage: $0 <directory> <output file>\n\n");
}

my $dir = $ARGV[0];
my $output = $ARGV[1];

print "Output    : $output\n";
print "Directory : $dir\n";

当我使用它的两个预期参数调用程序时系统工作:

> system("perl test.pl asd dasd", intern=TRUE, wait=TRUE)
[1] "Output    : dasd" "Directory : asd" 

没有它会产生相同的错误消息:

> system("perl test.pl", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 255
Warning message:
running command 'perl test.pl asd dasd' had status 255

当我评论 #use Excel::Writer::XLSX; 时,我注意到使用 system 的系统调用失败:

> system("perl test.pl asd dasd", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl test.pl asd dasd' had status 2

所以看起来它不喜欢 XLSX perl 模块。但是,正如我已经提到的,在终端中运行脚本绝对可以。而且它还可以在更新前与 R 系统调用一起使用...

--------

我正在使用 StatET 和 Eclipse,并尝试在普通 R 版本中运行它。那里的错误信息更加清晰。当我通过 R 调用 perl 调用时,perl 找不到代码中使用的 XLSX 包:

> system("perl test.pl asd asd", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl test.pl asd asd' had status 2 
Can't locate Excel/Writer/XLSX.pm in @INC (@INC contains: /Library/Perl/5.12/darwin- 
thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-
2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4 
/System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 
/System/Library/Perl/Extras/5.12/darwin-thread-multi-2level 
/System/Library/Perl/Extras/5.12 .) at test.pl line 3.
BEGIN failed--compilation aborted at test.pl line 3.

有人知道怎么解决吗?

【问题讨论】:

  • 你能提供 tst2xlsx.pl 的代码吗?具体来说,当您不提供任何参数时,它会以什么形式退出?

标签: perl r system


【解决方案1】:

我怀疑您的 perl 脚本确实返回了退出代码 2,这是一个非标准值,意在发出信号。

正是因为通过system() 进行通信非常乏味,您可以考虑在 R 本身中执行此操作。如果源文件是ascii,则很容易阅读。并且包XLConnectxlsx 允许您在R 支持的任何平台上用xlsx 编写(并且支持Java,因为xlsx 读/写代码来自Java jar 文件库)。

【讨论】:

  • 嘿,我知道所有这些 R xlsx 函数,我们使用它们将 xlsx 文件读入系统。然而,我们的输出文件太大了,以至于在 R 中使用 xlxx 函数需要很长时间,并且在大多数情况下会失败并出现错误,没有人真正知道如何修复它。我什至联系了开发人员,但这也没有导致任何结果......所以我需要让这个脚本再次运行。
  • xlsx 有两个接口,越轻量级的速度越快。你试过吗?但无论如何:查看 Perl 脚本并找出它返回 2 的原因。这也可能表示错误情况。
  • 嗯,正如我提到的,当我从控制台运行它时,它工作得非常好。我不明白为什么只通过系统(“perl txt2xlsx.pl”)调用它就应该以不同的方式工作。好奇怪!
  • 我知道它可以运行,但我要求您(现在两次)在编辑器中打开 Perl 并尝试了解它为什么/如何结束退出代码 2。
  • 如果您的数据太大而无法有效地通过管道传输到 xslx 文件,您可以选择放弃 Excel 并使用更合适的工具。我意识到考虑到您的情况,这可能是不可能的,但是使用错误的工具来完成这项工作可能会严重限制您。 Excel 是最常被滥用的工具之一……
【解决方案2】:

您的 perl 文件需要命令行上的参数。它应该像这样执行:

perl txt2xlsx.pl dir_to_outputfile outputfile.xls

尝试将您的 R 文件更改为

system("perl txt2xlsx.pl dir_to_outputfile output.xls", intern=TRUE, wait=TRUE)

【讨论】:

  • 这是正确的,但在更新之前,当您在没有任何参数的情况下运行它时发生了什么,系统返回了我定义的“死”字符串。当然,我尝试使用两个参数但相同的东西来运行它。它失败并显示上述错误消息...
  • 因此,如果从命令行运行 perl txt2xlsx.pl outputfile.xls 它可以工作,但从 R 运行 system("perl txt2xlsx.pl output.xls", intern=TRUE, wait=TRUE) 会失败? perl 是否接收参数?您可以修改 test.pl 以打印 $#ARGV$ARGV[0] $ARGV[1] 吗?也许 R 正在“保护”系统命令不传递参数?
  • 我遇到了类似的问题,但如果我执行setwd("location of perl script"),然后运行system 命令,它就会消失。无论如何都值得一试。
【解决方案3】:

当您在终端中运行程序时,您将设置一些环境变量集(如 PERL5LIB),在通过 R 运行时缺少这些环境变量集,它是指示 perl 查找 Excel::Writer::XLSX 的那个。

运行env | grep PERL 找出哪个

然后您可以将其导出,或将所需的值传递给 -I 参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多