Loading multiple RDS files save in the same directory using R
Hi Everyone,
I'm trying to load multiple .rds files that are save in the same directory. I have made a function for that and I iterate on a list of the files dir to load it but it doesn't work, see below that I write:
markerDir="..."
markerFilesList <- list.files(markerDir,pattern = ".rds", recursive = TRUE, include.dirs = TRUE)
readRDSfct <- function(markerFilesList,markerDir,i){
print(paste0("Reading the marker file called :",basename(markerFilesList[[i]])))
nameVariableTmp=basename(markerFilesList[[i]])
nameVariable=gsub(pattern = "\\.rds", '',nameVariableTmp)
print(paste0("file saved in varibale called:", nameVariable))
currentRDSfile = readRDS(paste0(markerDir,markerFilesList[[i]])) #nameVariable
return(currentRDSfile)
}
for (i in 1:length(markerFilesList)){
readRDSfct(markerFilesList, markerDir, i)
}
Does anyone has a suggestion for me to do it ?
thanks in advance!
• 1,371 views
•
link
0 answers
No answers yet.
Log in to answer this question.
Hello clementpch!
We believe that this post does not fit the main topic of this site.
This is a pure R question that is not related to bioinformatics.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
hi,
I don't understand your reaction. Sorry bioinformatics is intrinsically computer science. Since to answer a biological question it is necessary to use tools developed by computer language. what I am trying to achieve is of little use to any bioinformatician knowing that I am a biologist myself.
There is no biological context to your question. "Read multiple rds files" is something that a search on stack overflow might solve.
You should look at the
applyfamily of functions.lapplywill solve your problem - you don't need the loop or the namedreadRDSfctfunction.