如何在R和Shell脚本中调用Perl变量?

时间:2018-12-26 07:35:39

标签: r shell perl

  

我有一个Shell脚本和一个R脚本,它们都与Perl脚本链接。在Perl脚本中,有一个用户定义的路径,我也想将该路径用于Shell脚本和R脚本。需要哪些更改?   这是Perl脚本:

print "Enter the path to your input file:";
chomp(my $user_path = <STDIN>);
my $path = $user_path // $default_path;  # $path  is path for Perl script
print "The path we'll use is: $path";
........................
........
  

这是R脚本:

x <- read.table($path."/text.txt", header=F) # how to introduce $path as path for R script
library(plyr)
df <- ddply(x, .(x$V1,x$V2, x$V3), nrow)
..............
.............
  

($ path)适用于Perl脚本,但不适用于R.我想在R和Shell脚本中调用Perl变量($ path)。如何在R和Shell脚本中使用$ path作为路径?

1 个答案:

答案 0 :(得分:0)

只需在您的systemexec ini perl上使用$ path作为参数。它将arg发送到shell脚本,然后将其用作Rscript的arg。

例如,我有3个脚本。我们走了。

root@analist:~/test# cat file.pl
#!/usr/bin/perl
#Input file
print "[Perl] Enter file:";
chomp(my $filepath = <STDIN>);
#Send to bash script
my @cmd = ('./file.sh');
push @cmd, $filepath;
system(@cmd);
root@analist:~/test# cat file.sh
#!/bin/bash
echo "[Bash] Filename: $1"
Rscript file.R $1
root@analist:~/test# cat file.R
args <- commandArgs(TRUE)
fileTxt <- args[1]
cat(paste('[R] File:', fileTxt, '\n'))
root@analist:~/test# ./file.pl
[Perl] Enter file:test.txt
[Bash] Filename: test.txt
[R] File: test.txt

我希望这个答案能对您有所帮助。

相关问题