简体   繁体   English

R-命令行错误中的参数

[英]R - Arguments in command line error

Suppose I have a dataframe that looks like this: 假设我有一个看起来像这样的数据框:

SNP   Frequency
A     20
B     50
C     7

(The real dataframe has many more rows, of course.) (当然,实际的数据帧有更多的行。)

What I would like to do is pass some arguments to the command line that would allow me to set the input dataframe and the frequency in the command line. 我想做的是将一些参数传递给命令行,这使我可以在命令行中设置输入数据帧和频率。 Here is what I have tried: 这是我尝试过的:

args = commandArgs()
df <-args[1]
freqsub <- subset(df, args[2],header=TRUE)

In the args[2] part I would ordinarily have Frequency > somenumber args[2]部分中,我通常会使Frequency > somenumber

I know how to work it when I have df <- args[1] , but args[2] doesn't. 当我有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"

Any ideas? 有任何想法吗? Happy to edit if more information is required (I can't tell if it is the case, sorry). 如果需要更多信息,很高兴进行编辑(抱歉,我无法确定是这样)。

I think you have to use the option trailingOnly = TRUE : 我认为您必须使用选项trailingOnly = TRUE

args = commandArgs (trailingOnly = TRUE)

Otherwise args[1] args[2] are not what you are expecting... 否则args [1] args [2]不是您所期望的...

With trailingOnly = FALSE what you get in the first positions of args is information about how the R process is being run. 使用trailingOnly = FALSE ,您在args的第一位置得到的信息是有关R进程如何运行的信息。

You can do: 你可以做:

print (args)

to see in your shell what are you really having in the args vector. 看看您的外壳中args向量中到底有什么

Besides that "Frequency>20" will be in args[2] as a character... so you will have to process it if you want to have as a parameter of the subset function. 此外,“ Frequency> 20”将作为字符出现在args [2]中,因此,如果要作为subset函数的参数,则必须对其进行处理。

In this case I will pas just the number as a parameter to be read in args[2]. 在这种情况下,我仅将数字作为参数在args [2]中读取。 Then you can do: 然后,您可以执行以下操作:

subset(df, Frequency > as.numeric (args[2]), header=TRUE)

So following your comments I will do 2 R scripts: 因此,按照您的意见,我将做2个R脚本:

The first one just to make sure that you read the right parameters will be: 为了确保您阅读正确的参数,第一个将是:

args = commandArgs (trailingOnly = TRUE)
myfile = args[1]
myfreq = as.numeric (args[2])
print (myfile)
print (myfreq)    

This you have to run it in your shell as: 您必须在shell中以如下方式运行它:

Rscript script1.R file.txt 5

and you should get an output like: 并且您应该得到如下输出:

file.txt
5

In your second script do: 在第二个脚本中执行:

myfile = "file.txt"
myfreq = 5

## and all computations you need
df = read.table (myfile, ...
subset(df, myfreq, ...)

Debug this second file (interactively) until it works and then change the first two lines: by the (3) commandArgs lines in the first file. (交互式地)调试第二个文件,直到它起作用为止,然后更改前两行:通过第一个文件中的(3)commandArgs行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM