r - 如何在 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 作为路径?
解决方案
只需在您的system
或exec
ini perl 上使用 $path 作为参数。它会将 arg 发送到 shell 脚本,然后将其用作 arg 的Rscript
.
例如,我有 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
我希望这个答案能有所帮助。
推荐阅读
- spring-boot - 添加 CorsConfiguration 后 LoadBalance 失败
- vue.js - 使用 babel-jest 覆盖失败
- flutter - 第二次导航到时无响应的 Web 视图
- python - 如何在 Pytorch 中实现 DQN 的重要性权重采样
- css - 桌面中的 4 列移动视图引导程序中的 2 列
- node.js - 无法读取未定义 discord.js 的属性“名称”尝试读取 json 中的数组
- kotlin - Kafka 迁移/处理“UNKNOWN_TOPIC_OR_PARTITION”有必要吗?
- sql - 插入后计数返回更多行
- python - 为什么按住鼠标左键不打印“1”?
- jquery - 使用 jquery 在 wp-login 页面中添加段落