0

I load my files (.RData) from a particular folder, and i created a subfolder to save some samples and subsets. So, i want to save these elements in the subfolder, and they don't have the same name structure because i have multiple datasets (for example it cannot be sub1, sub2 etc, i have to write try1, full_sample, sub_2021 and so on).

I tried the following :

subsets_samples <- file.path <-("/Volumes/WD_BLACK/Merge/SAMPLES_SUBSETS")
fname <- file.path(subsets_samples, ".RData")
save(mydata, file=fname)

But obviously there is a problem with the saving part. My goal is to have something like :

save(mydata, file = "newname")

With the .RData format from fname that is put automatically.

I saw some answers with loops and so on but i don't really understand the process i'm sorry.

Thanks !

katdataecon
  • 185
  • 8

1 Answers1

0

The problem with file.path is that it will place a separator (e.g., /¸) between each of the elements. So you would have to use paste0 in addition for the actual file name:

# If I understand you correctly, you want the iteration, like try1, full_sample, sub_2021 and so on in your file name. define them somewhere in your loop/script
iteration <- "full_sample"
fname <- file.path("Volumes", "WD_BLACK", "Merge", "SAMPLES_SUBSETS", paste0(iteration, ".Rds"))

Additionally, I would suggest to use saveRDS instead of save, since it is the appropriate function if you want to save just one object.

saveRDS(mydata, file = fname)
JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Well, for instance, if my subset is named "sample2021" in my global environment, i want it to be saved as "sample_2021.RData" (or .Rds) in my "SAMPLES_SUBSETS" subfolder. In addition, i have multiple subsets from several datasets, so another subset can be named "supply_y1" in my GE and so on. Here is the result with your code :" Error : impossible to open the compressed file 'Volumes/WD_BLACK/MSI/SAMPLES_SUBSETSfull_sample.Rds', probable cause : 'No such file or directory' – katdataecon Apr 23 '21 at 09:08
  • Sorry, my bad. I never actually come across `file.path` before. Should work now. – JBGruber Apr 23 '21 at 09:44
  • Well the path and file name look correct now. Do you have such a folder on your computer? – JBGruber Apr 23 '21 at 10:24
  • When i do saveRDS(sample2021, file=fname), i get this : 'Volumes/WD_BLACK/MSI/SAMPLES_SUBSETS/full_sample.Rds', : 'No such file or directory' ; I don't understand the "iteration" line in fact, i just want to save what is in my global environment in the folder "SAMPLES_SUBSETS", but not with the name "full_sample". What i meant is that when i looked on Stack for this problem before to poste my message, i only saw this topic with datasets having the same structure : "sub1, sub2" and so on, which make their loops impossible to work for me has i have different structure each time. – katdataecon Apr 23 '21 at 10:25
  • Like, one my sample is "full_sample", the other one is "p1", the other one "suppliers_21" and so on, there is no common structure in the name. – katdataecon Apr 23 '21 at 10:32
  • `'No such file or directory'` means just that. the folder doesn't exist. You are creating the file so that part does not apply. You can call the file anything you want. But I don't understand where the names come from. You can define the name of the file in your script by assigning it to an object. I called the object iteration since it seems you want to iterate over some list of things. – JBGruber Apr 23 '21 at 10:39
  • I'll look better during the afternoon, because honestly i feel so dumb lol, it still doesn't work. I'll give you a feedback ! – katdataecon Apr 23 '21 at 11:06
  • I would love to help you but I honestly don't get what your problem is. That said, describing your problems in a way so random people on stackoverflow.com can understand them is a skill one must practice to learn like many other things. You'll get better at it and better at solving your own problems if you keep spending time here. – JBGruber Apr 23 '21 at 11:35