假设我有一个如下所示的数据框:
SNP Frequency
A 20
B 50
C 7
(当然,真正的数据帧还有更多行。)
我想要做的是将一些参数传递给命令行,这样我就可以在命令行中设置输入数据帧和频率。这是我尝试过的:
args = commandArgs()
df <-args[1]
freqsub <- subset(df, args[2],header=TRUE)
在args[2]
部分,我通常会Frequency > somenumber
我知道如何处理df <- args[1]
,但args[2]
没有。
$ Rscript sumtest.R test.txt Frequency>20
"Error in subset.default(df, args[2], header = TRUE) :
argument "subset" is missing, with no default
Calls: subset -> subset.default
Execution halted"
有什么想法吗?如果需要更多信息,我很乐意编辑(我无法判断是否属于这种情况,抱歉)。
答案 0 :(得分:0)
我认为你必须使用选项trailingOnly = TRUE
:
args = commandArgs (trailingOnly = TRUE)
否则args [1] args [2]不是你所期待的......
使用trailingOnly = FALSE
,您在args
的第一个位置获得的信息是有关R进程如何运行的信息。
你可以这样做:
print (args)
在你的shell中查看你在args
向量中真正拥有的内容。
除了“频率&gt; 20”将在args [2]中作为一个字符...所以如果你想要作为subset
函数的参数,你必须处理它。
在这种情况下,我将只将数字作为参数传递给args [2]。然后你可以这样做:
subset(df, Frequency > as.numeric (args[2]), header=TRUE)
因此,根据您的意见,我将做2个R脚本:
第一个只是为了确保您阅读正确的参数:
args = commandArgs (trailingOnly = TRUE)
myfile = args[1]
myfreq = as.numeric (args[2])
print (myfile)
print (myfreq)
你必须在你的shell中运行它:
Rscript script1.R file.txt 5
你应该得到一个输出:
file.txt
5
在你的第二个脚本中执行:
myfile = "file.txt"
myfreq = 5
## and all computations you need
df = read.table (myfile, ...
subset(df, myfreq, ...)
调试第二个文件(以交互方式)直到它工作,然后更改前两行:通过第一个文件中的(3)commandArgs行。