简体   繁体   中英

Save R output to a different directory

I am running some R code on a Windows computer using RStudio and my code generates Excel files and netCDF files periodically (dozens of them eventually). I don't want them to clutter my working directory. Is there a way to save the files to a directory called "Output" (ex: C:/.../original file path/Output) in the parent directory? I would like a way to change my current working directory to a different directory. I understand there is getwd() and setwd() but how do I set the path to the output directory without typing out the entire windows path (for example: setwd(current source file path for windows or Mac/output). My collaborator uses a Mac and he would have his output stored there as well.

You can specify an argument in your write.csv function and other similar write functions which specifies your path.

#Output path
OutPath<- "C:/blah/blahblah/op/"

#Table to dump as output
OutTbl <- iris 

write.csv(OutTbl, file = OutPath)

Source: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html

You have a file argument in your write* function. If your Output directory is in your working directory, it works like this:

write.xlsx(df, file = "Output/table.xlsx")
write.csv(df, file = "Output/table.csv")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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