如何将R中的变量传递给系统命令?

时间:2017-11-14 03:57:26

标签: r

我有一个我这样读过的文件:

input.bed <- as.data.frame(read.table("lamina_filtered.bed", header = FALSE, sep = "\t", 
    stringsAsFactors = FALSE, quote = ""));

我正在尝试调用系统命令。我提到了上一个问题,答案建议使用sprintfpaste,但两者都给我同样的错误:

system(sprintf("/usr/local/bin/KentUtils/bedToPsl %s hg38.chom.sizes inp_hg38.psl", input.bed));

system(paste("/usr/local/bin/KentUtils/bedToPsl  hg38.chom.sizes " , input.bed," inp_hg38.psl"));

我收到此错误:

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `/usr/local/bin/KentUtils/bedToPsl c("chr1", "chr1",         "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", "chr1", '

这是输入的样本。它是一个简单的床文件R:

   V1       V2       V3
1 chr1 11323785 11617177
2 chr1 12645605 13926923
3 chr1 14750216 15119039
4 chr1 18102157 19080189
5 chr1 29491029 30934636
6 chr1 33716472 35395979

1 个答案:

答案 0 :(得分:1)

仔细看看你的错误。您尝试执行的命令类似于:

/usr/local/.../bedToPsl c("chr1","chr1","chr1") ...

您使用pastedata.frame粘贴到系统命令中。您的操作系统无法理解R数据结构。鉴于您的问题不包含有关该系统调用应该接收的内容的详细信息,因此无法告诉您如何解决此问题。但这里有一些可能性

  • 标量值 - 您可能需要执行input.bed$V1[1]之类的操作才能传入一个字符串。确保适当引用(或不引用)
  • 文件 - 通常系统命令用于接收文件路径。在这种情况下,您可以跳过加载数据并只传递文件名。