【问题标题】:Commands are not executed when running code through system in a for loop在 for 循环中通过系统运行代码时不执行命令
【发布时间】:2019-03-22 19:48:49
【问题描述】:

这是我第一次创建包含两个变量的 for 循环。我有一堆想要检查的 DNA 测序样本。每组数据有两个文件需要同时运行。

当我在没有 for 循环变量的情况下在 R 中运行 system(paste()) 时,它工作得很好。所以我知道问题出在循环本身。当我运行下面的脚本时,我没有收到错误,它运行但没有任何反应。

我显然搞砸了。我只是不知道从这里去哪里。

for (j in list.files(pattern = "R1_001.trim.paired.fastq.gz")) {
  for (h in list.files(pattern = "R2_001.trim.paired.fastq.gz")) {
    outname=paste(substr(j, start=1, stop=7), sep= "")
    system(paste("docker run -v /path/:/path/ -w /path/ combinelab/salmon salmon quant -i /path/CanFam3.1_index -l A -1 ",j,"-2 ",h,"-o ",outname, ,sep="")
    )
  }
}

【问题讨论】:

  • 查看粘贴输出是否正确。如果您在终端上运行所有粘贴输出会发生什么。效果好吗?
  • @Sonny 你到底想让我在终端运行什么?
  • 我的意思是在 shell 提示符下运行 Docker 命令
  • 一件事可能是sep 之前的额外空间应该给出argument is missing, with no default 错误:,outname, ,sep="") 应该是,outname,sep="")
  • 循环是否正常工作?如果你把print(c(j, h))放进去,你看到正确的文件了吗?

标签: r for-loop


【解决方案1】:

在您提供的代码中,我唯一能立即看到的是一些缺失的空格,这可能会导致观察到的行为。您也可以使用paste0 代替paste(...,sep=""),希望以下内容有所帮助:

for (j in list.files(pattern = "R1_001.trim.paired.fastq.gz")) {
  for (h in list.files(pattern = "R2_001.trim.paired.fastq.gz")) {
    outname=substr(j, start=1, stop=7)
    system(paste0("docker run -v /path/:/path/ -w /path/ combinelab/salmon salmon quant -i /path/CanFam3.1_index -l A -1 ",j," -2 ",h," -o ",outname))
  }
}

编辑

在 for 循环之外通过 list.files 命令获取 character(0) 表明您没有将 wd 中的任何文件与您的模式匹配。

如果您确定文件在正确的wd 中,或者您直接在list.files 命令中设置wdlist.files(path = ".", pattern = NULL),您可以尝试结合 cmets 中提到的 Sonny 和 Parfait上面运行你的for循环用print替换system并删除list.files命令中的pattern规范。这将告诉你是否发送了正确的docker字符串(尽管可能还会有打印出不正确的文件,您可以稍后通过更新模式进行过滤)。

【讨论】:

  • 得到与paste(...,sep="") 相同的结果我确实在运行print(c(j,h)) and got this Error in print(c(j, h)) : object 'h' not found` 时遇到了错误,所以可能存在问题和 h?
  • 坏信号,j 看起来怎么样?当您单独使用list.files 时,您是否发现任何问题?
  • 这是我在运行 list.files function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE) .Internal(list.files(path, pattern, all.files, full.names, recursive, ignore.case, include.dirs, no..)) <bytecode: 0x1096fc188> <environment: namespace:base> 时得到的结果
  • 尝试运行 list.files(pattern = "R2_001.trim.paired.fastq.gz") 并使用 getwd() 检查您的工作目录
  • 我发现鬃毛我真是个白痴.. 模式功能错误,名称较短。我下载了一个子集数据。我觉得自己像个白痴。另外要修复上面的系统功能,我只需要运行system(paste("docker run -v /path/:/path/ -w /path/ combinelab/salmon salmon quant -i /path/canine.3.30.19_index -l A -1 ",j," -2 ",h," -o ",outname,sep="") 我只需要在.gz's 之后添加空格
猜你喜欢
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多