在R中运行Shell命令脚本(Call shell commands from R)

aaa.R
Args <- commandArgs()
cat("Args[1]=",Args[1],"\n")
cat("Args[2]=",Args[1],"\n")
cat("Args[3]=",Args[3],"\n")
cat("Args[4]=",Args[4],"\n")
cat("Args[5]=",Args[5],"\n")
cat("Args[6]=",Args[6],"\n")

shell_cmd<-paste0("grep -n CHR ",Args[6])
grep_out<-system(shell_cmd, intern = TRUE)
cat(grep_out)

 

how to run:

/myPathToRscript/Rscript aaa.R  bbb.vcf

 

=============================================

Description

system invokes the OS command specified by command.

Usage

system(command, intern = FALSE,
       ignore.stdout = FALSE, ignore.stderr = FALSE,
       wait = TRUE, input = NULL, show.output.on.console = TRUE,
       minimized = FALSE, invisible = TRUE)

Arguments

command

the system command to be invoked, as a character string.

intern

a logical (not NA) which indicates whether to capture the output of the command as an R character vector.

ignore.stdout, ignore.stderr

a logical (not NA) indicating whether messages written to ‘stdout’ or ‘stderr’ should be ignored.

wait

a logical (not NA) indicating whether the R interpreter should wait for the command to finish, or run it asynchronously. This will be ignored (and the interpreter will always wait) if intern = TRUE.

input

if a character vector is supplied, this is copied one string per line to a temporary file, and the standard input of command is redirected to the file.

show.output.on.console, minimized, invisible

arguments that are accepted on Windows but ignored on this platform, with a warning.

 

=============================================

Invoke a System Command, using a Shell

Description

shell runs the command specified by cmd, usually under a shell.

Usage

shell(cmd, shell, flag="/c", intern=FALSE, wait=TRUE,
      translate=FALSE, mustWork=FALSE, ...)

Arguments

cmd the system command to be invoked, as a string.
shell a string giving the name of the shell to be used, or NULL (no shell). If missing, a suitable shell is chosen: see ‘Details’.
flag the switch to run a command under the shell. If the shell is bash or tcsh the default is changed to "-c".
intern a logical, indicates whether to make the output of the command an R object.
wait should the R interpreter wait for the command to finish? The default is to wait, and the interpreter will always wait if intern = TRUE.
translate If TRUE, "/" in cmd is translated to "\".
mustWork a logical; if TRUE failure to run the command will give an R error.

 

REF:

http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html

https://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html

 

posted @   emanlee  阅读(8168)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2014-06-01 python 捕获 shell 脚本的输出结果
2012-06-01 由于这台计算机没有终端服务器客户端访问许可证无法远程
2011-06-01 Astra SiteManager
2009-06-01 软件配置管理工具列表大全
点击右上角即可分享
微信分享提示