自动生成pdf和txt文件的名称

时间:2018-05-28 21:46:01

标签: r

我正在尝试为函数和图形运行for循环。我有不同的向量(特征)我从数据运行。我想通过名为i的for循环运行每个特征,并将每个结果输出到具有相应特征名称的单独文件中,因此我只需要运行一次命令而不是9次。应调用该文件(“bristleX trait 1.txt”,“bristleX trait 2.txt”,...,“bristleX trait i.txt”)同样也与.pdf命令相同。 (“bristleX trait 1.pdf”,“bristleX trait 2.pdf”,...,“bristleX trait i.pdf”) 我在接收器和pdf函数方面遇到了麻烦;它们不会使用paste()函数输出与常规标题或名称相同的内容。在特征中使用i的函数只需要数字1到9即可工作,但一次只能执行一个。

  library(qtl)
  data("bristleX")
    traits<-c(1:9)
    for(i in traits){
      sink(paste(file="bristleX trait",i".txt"))
      pdf(paste(file = "brixtleX trait",i".pdf", paper="special",width = 8.5, 
      height = 11, 
          family="Times", pointsize=11,bg="white",fg="black"))
      print("MR QTLs")

      out.mr <- scanone(bristleX, pheno.col=i, method="mr") # estimate LOD 
      #for a single phenotype
      summary(out.mr, threshold=3)
      plot(out.mr) # 

     sink()
     dev.off()
    }

2 个答案:

答案 0 :(得分:0)

我没有尝试你的例子,但我可以看到你在这里有错误:

...
      sink(paste(file="bristleX trait",i".txt"))
      pdf(paste(file = "brixtleX trait",i".pdf", paper="special",width = 8.5, 
...

粘贴没有文件参数。你可能想要:

...
      sink(file=paste("bristleX trait",i".txt"))
      pdf(file = paste("brixtleX trait",i".pdf", paper="special",width = 8.5, 
...

答案 1 :(得分:0)

你在过去的功能中有错,你可以使用

paste("bristleXtrait",i,".txt")

而不是

paste("bristleXtrait",i".txt")

此外,您必须重新设置文件名中的空格。你可以使用str_replace_all()函数

sink(file = str_replace_all(paste("bristleX trait",i,".txt"), pattern=" ", repl=""))