I was able to run my codes for enrichment analysis using the following codes , however I am not familiar with for loops. Hope you guys can help me out. My codes are as below:
Isn't a good practice asking here someone to write code for you. Besides that, be more clear, explaining against what you want to loop (which is the filename structure?) and read something online about basic for looping in R before asking basic question.
@Shred I edited my question, hopefully its more clear. I don't know how for loops work, however its not that I am asking everything. I am able to do this with my individual files. I just need some idea on how to run this on multiple files with different output names with file name.
I recommend you to do some "googling" on your own before asking as @Shred recommended. Have you seen this post for example? I hope it helps you to code the solution. If you try and you get stuck, you can always come back here with your attempt and the error you are getting :).
Per comments from iraun and @Shred online search will give you what you want. I believe you almost there, just need to put your code in for loop and define some variable inside that. Something like the following :
Put your files in a directory (d), then save the file names in a variable (fn) ;
fn = list.files(d)
# define a list to save enrichment result from for loop
enrich_list = list()
# for loop
for (f in 1:length(fn)){
file <- read.delim(fn[f], header = FALSE, sep="\t")
colnames(file) <- c("ProteinID", "KO")
file<- (file$KO)
kegg_file <- enrichKEGG(file, organism = "ko", keyType = "kegg", pvalueCutoff = 0.05, pAdjustMethod = "none", qvalueCutoff = 0.3)
enrich_list[f] = kegg_file
names(enrich_list)[f] = fn[f]
png(paste0(fn[f], ".png"),res=90, w=1000, h=800)
p<- dotplot(kegg_22470, showCategory=10) + ggtitle(paste0("Enriched Pathways ", fn[f]))
p + theme( plot.title = element_text(color="black", size=14, face="bold" hjust=0.5))
dev.off()
}
Isn't a good practice asking here someone to write code for you. Besides that, be more clear, explaining against what you want to loop (which is the filename structure?) and read something online about basic for looping in R before asking basic question.
@Shred I edited my question, hopefully its more clear. I don't know how for loops work, however its not that I am asking everything. I am able to do this with my individual files. I just need some idea on how to run this on multiple files with different output names with file name.
I recommend you to do some "googling" on your own before asking as @Shred recommended. Have you seen this post for example? I hope it helps you to code the solution. If you try and you get stuck, you can always come back here with your attempt and the error you are getting :).
Since you have come this far, convert your code to function and use lapply.