对于当前目录中的所有文件,请在另一个目录中查找具有相同前缀的文件。R

对于当前目录中的所有文件,请在另一个目录中查找具有相同前缀的文件。R,r,R,我有一个非常基本的问题,如果别人问我这个问题,我道歉(我试图找到答案,我真的找到了) 我编写了一个脚本,创建了两个目录,并在每个目录中以相同的名称保存给定文件的信息。在一个目录中,我格式化了数据,使用ggplot生成了箱线图,在另一个目录中,我保存了注释信息。然后我制作一个箱线图,我想在注释目录中搜索相应的注释文件,以便将注释添加到箱线图中。代码设置为对给定目录中的“所有文件”执行此操作,因此我不能简单地更改工作目录并按名称加载注释文件。以下是我得到的: 在名为ggplot/data的目录中,文

我有一个非常基本的问题,如果别人问我这个问题,我道歉(我试图找到答案,我真的找到了)

我编写了一个脚本,创建了两个目录,并在每个目录中以相同的名称保存给定文件的信息。在一个目录中,我格式化了数据,使用ggplot生成了箱线图,在另一个目录中,我保存了注释信息。然后我制作一个箱线图,我想在注释目录中搜索相应的注释文件,以便将注释添加到箱线图中。代码设置为对给定目录中的“所有文件”执行此操作,因此我不能简单地更改工作目录并按名称加载注释文件。以下是我得到的:

在名为ggplot/data的目录中,文件保存为:my_data_1.csv

在名为ggplot/annotation的目录中,文件保存为:my_data_1.csv

最终注释的图形保存在ggplot/graph_输出中

# goto ggplot data directory
setwd("/home/path/to/ggplot/data")

#look for all files
inFilePaths = list.files(path=".", pattern=glob2rx("*"), full.names=TRUE)

#make a ggplot2 boxplot for every file with
for (inFilePath in inFilePaths)
{ 
  # Read in each data file as a dataframe
  inFileData = read_csv(inFilePath)

  # Make a ggplot. **This is only part of my code to save space**
  plot1 = ggplot(data =inFileData, mapping= aes(x=Sample, y=Expression)) +
    scale_fill_manual(values=c("#606060", "#29a329"))

  # Change directories to annottaion folder
  setwd("/home/path/to/ggplot/annotation")

  ####Help!!!!#### Write something to find the file with same inFilePath name to get annotations
  ##Maybe something like this:
  inFilePaths2 = list.files(path=".", pattern=glob2rx(inFileData), full.names=TRUE)
  ##This does not work because it cant find the same inFileData file used to make the ggplot

  # annotate gglot with corresponding annotation file
  for (inFilePath in FilePaths2)
  {
    palues = read_csv(...of the file that matches the file name of the ggplot data) 
    plt2_annot <- plot1 +
      geom_text(data=pvalues, aes(x=value, y=breaks,label = paste('P:',format.pval(pval, digits=1))))
  }


  # specify size of ggplot base on number of boxes displayed using total rows of data
  n = 0.25+(0.75*(nrow(unique(select(inFileData, Gene)))))

  # Change directories to graph output folder, and save graph
  setwd("/home/path/to/ggplot/graph_output")
  ggsave(filename = paste(inFilePath, ".png"), plot=plot2, height = 1.5, width = n, units = "in")
}
#转到ggplot数据目录
setwd(“/home/path/to/ggplot/data”)
#查找所有文件
infilePath=list.files(path=“.”,pattern=glob2rx(“*”),full.names=TRUE)
#使用为每个文件创建ggplot2方框图
用于(填充路径中的填充路径)
{ 
#作为数据帧读入每个数据文件
填充数据=读取(填充路径)
#制作ggplot。**这只是我的代码中节省空间的一部分**
plot1=ggplot(数据=inFileData,映射=aes(x=样本,y=表达式))+
刻度填充手册(数值=c(“#606060“,“#29a329”))
#将目录更改为annottaion文件夹
setwd(“/home/path/to/ggplot/annotation”)
####帮助!!!!#####写一些东西来查找具有相同填充路径名称的文件以获取注释
##也许是这样的:
inFilePaths2=list.files(path=“.”,pattern=glob2rx(inFileData),full.names=TRUE)
##这不起作用,因为它找不到用于生成ggplot的同一个内嵌数据文件
#用相应的注释文件注释gglot
用于(文件路径2中的填充路径)
{
palues=read_csv(…与ggplot数据文件名匹配的文件)

plt2_annot利用Gregor的评论,我设法想出了一个非常简单的解决方案

1) 我更改了每个目录中文件的命名方式,以便数据文件和相应的注释文件具有完全相同的名称

2) 与其尝试实现一些功能来查找当前inFilePath数据文件的相应注释文件,只需将目录更改为注释目录并使用read_csv(inFilePath)重新加载inFilePath,即可加载相应的注释文件

以下是最终为我工作的代码:

# goto ggplot data directory
setwd("/home/path/to/ggplot/data")

#look for all files
inFilePaths = list.files(path=".", pattern=glob2rx("*"), full.names=TRUE)

#make a ggplot2 boxplot for every data file
for (inFilePath in inFilePaths)
{ 
  #Need to set directory again due to directory change lower in the loop
  setwd("/home/path/to/ggplot/data")
  # Read in each data file as a dataframe
  inFileData = read_csv(inFilePath)

  #check to see which data is loaded
  print(inFileData)

  #make a ggplot from the ggplot data

  # Change directories to annotation folder
  setwd("/home/path/to/ggplot/annotation")

  #load new annotation data. The file names are the same, so loading the same file name in the annotations
  # directory actually loads the annotations for the corresponding plot
  inFileData2 = read_csv(inFilePath)

  #check to make sure the correct annotation file is loaded
  print(inFileData2)

  #add annotation to ggplot graph
  #now that I can access the correct annotation, I'll work on this part next.
  #then save the graph

  }

谢谢您的帮助。

请澄清,您有CSV文件,但不是使用标准的
.CSV
扩展名,而是使用您自己的
.text\u data
扩展名?因此数据文件的完整文件名是,例如,
my\u data\u 1.txt\u data
,而不是
my\u data\u 1.txt\u data.CSV
my\u data\u 1.CSV
?还有一个问题-您似乎假设相应的注释
.txt\u pval
在那里-如果该文件不存在,您不会有任何错误处理-因此,您不必“查找匹配项”,因为您知道匹配的文件名应该是什么,您只需要构造该名称?是否只需替换
的“数据”
在文件名的末尾加上
“pval”
是否有效?如下所示:
gsub(pattern=“data$”,replacement=“pval”,x=inFilePath)
。在编辑之后,我更加困惑了。现在看起来每个目录中的文件都有相同的名称,所以您不需要“查找”第二个文件-更改工作目录并使用
read_csv
(您已经这样做了)就足够了。您不需要
filePaths2
,只需要再次使用
filePath
。删除
for
循环的内部
,每个数据文件都有一个注释文件,因此不需要任何嵌套循环。太好了关于文件扩展名全名的问题。我使所有文件扩展名都相同,这有助于我生成注释目录中所有文件的列表,但我仍然无法从数据目录中选择与当前填充路径同名的文件。是的!!Gregor,你最后的注释是关键。这可能就是解决方案。我我今晚回家后就试着把答案贴出来。我知道我错过了一些简单的东西。我感谢你的帮助。