简体   繁体   English

R循环打开文件

[英]R loop opening files

I am trying to run a simple 5 lines command but over 9000 different files. 我试图运行一个简单的5行命令,但超过9000个不同文件。 I wrote the following for loop 我写了以下for循环

setwd("/Users/morandin/Desktop/Test")
output_file<- ("output_file.txt")
files <- list.files("/Users/morandin/Desktop/Test")
for(i in files) {
  chem.w.in <- scan(i, sep=",")
  pruned.tree<-drop.tip(mytree,which(chem.w.in %in% NA))  
  plot(pruned.tree)
  pruned.tree.ja.chem.w.in <- phylo4d(pruned.tree, c(na.omit(chem.w.in)))
  plot(pruned.tree.ja.chem.w.in) 
  out <- abouheif.moran(pruned.tree.ja.chem.w.in)
  print(out)
}

Hey I am editing my question: the above code does the for loop perfectly now (thanks for all your help). 嘿,我在编辑我的问题:上面的代码现在完美地完成了for循环(感谢您的所有帮助)。 I am still having an issue with the output. 我的输出仍然有问题。

I can redirect the entire output using R through bash commands but I would need the name of the processed file. 我可以通过bash命令使用R重定向整个输出,但是我需要已处理文件的名称。 My output looks like this: 我的输出如下所示:

class: krandtest 
Monte-Carlo tests
Call: as.krandtest(sim = matrix(res$result, ncol = nvar, byrow = TRUE), 
    obs = res$obs, alter = alter, names = test.names)

Number of tests:   1 

Adjustment method for multiple comparisons:   none 
Permutation number:   999 
  Test       Obs   Std.Obs   Alter Pvalue
1   dt 0.1458514 0.7976225 greater    0.2

other elements: adj.method call

Is there a way to print Pvalue results and name of the file (element i)?? 有没有一种方法可以打印Pvalue结果和文件名(元素i)?

Thanks 谢谢

I suspect what is going wrong here is that list.files() by default returns a list of only the names of the files, not the entire path to the file. 我怀疑这里出了list.files() ,默认情况下list.files()返回仅文件名的列表,而不返回文件的完整路径。 Setting full.names to TRUE will fix this issue. full.names设置为TRUE将解决此问题。 Note that you will not have to add the txt add the filename as list.files() already returns the full path to an existing file. 请注意,您不必添加txt将文件名添加为list.files()已经返回了现有文件的完整路径。

Since Paul Hiemstra's answer answered #1, here's an answer to #2, assuming that by "answers" you mean "the printed output of abouheif.moran(pruned.tree.ja.chem.w.in) ". 由于Paul Hiemstra的答案回答了#1,因此这是#2的答案,假设“答案”是指“ abouheif.moran(pruned.tree.ja.chem.w.in)的打印输出”。

Use cat() with the argument append = true . cat()与参数append = true For example: 例如:

output_file = "my_output_file.txt"
for(i in files) {
    # do stuff
    # make plots
    out <- abouheif.moran(pruned.tree.ja.chem.w.in)
    out <- sprintf("-------\n  %s:\n-------\n%s\n\n", i, out)
    cat(out, file = output_file, append = TRUE)
}

This will produce a file called my_output_file.txt that looks like: 这将产生一个名为my_output_file.txt的文件,如下所示:

-------
file_1:
-------
output_goes_here

-------
file_2:
-------
output_goes_here

Obviously the formatting is entirely up to you; 显然,格式完全取决于您; I just wanted to demonstrate what could be done here. 我只是想展示在这里可以做什么。

An alternative solution would be to sink() the entire script, but I'd rather be explicit about it. 一种替代解决方案是将整个脚本sink() ,但是我宁愿对此进行明确说明。 A middle road might be to sink() just a small piece of the code, but except in extreme cases it's a matter of preference or style. 中间的道路可能是只sink()一小部分代码,但在极端情况下,则是偏好或样式问题。

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

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