【问题标题】:Pass imput file to R script via command line通过命令行将输入文件传递给 R 脚本
【发布时间】:2015-04-17 02:03:27
【问题描述】:

我有 3 个 data.frames(tab1、tab2),我需要一个 R 脚本将它们与 cbind 组合并将输出保存到另一个表中。

所以我这样做:

tab1=read.table("imput_tab1.txt",header=TRUE,fill=TRUE,stringsAsFactor=FALSE)
tab2=read.table("imput_tab2.txt",header=TRUE,fill=TRUE,stringsAsFactor=FALSE)
myfunction <- function(tab1, tab2 )
{
tab3=cbind(tab1,tab2)
}
write.table(tab3, file="output_table.txt",sep="\t", row.names = FALSE, col.names=T, qmethod = "double", quote=F) 

我想在命令行上完成所有这些。 像

R Myscript.r imput_tab1.txt imput_tab2.txt > output_table.txt

这可能吗?

【问题讨论】:

标签: r command-line-arguments


【解决方案1】:

你的脚本应该是:

#the variable args below captures the arguments you pass from the 
#command line i.e. the names of the two files and stores them in a vector
args <- commandArgs(trailingOnly = TRUE)

tab1=read.table(args[1],header=TRUE,fill=TRUE,stringsAsFactor=FALSE)
tab2=read.table(args[2],header=TRUE,fill=TRUE,stringsAsFactor=FALSE)

#you never used your function in your code so just do the below
tab3=cbind(tab1,tab2) #you don't necessarily need a function to cbind two tables

write.table(tab3, file="output_table.txt",sep="\t", row.names = FALSE, col.names=T, qmethod = "double", quote=F) 

假设您上面的脚本名为 test.R

你应该从命令行运行它:

Rscript test.R imput_tab1.txt imput_tab2.txt

它会起作用(假设文件在正确的位置)。

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2019-12-23
    • 2021-12-10
    • 2011-06-16
    相关资源
    最近更新 更多