I often find myself writing R scripts that generate a lot of output. I find it cleaner to put this output into it's own directory(s). What I've written below will check for the existence of a directory and move into it, or create the directory and then move into it. Is there a better way to approach this?

I'm sure I've seen an R function that creates a temporary directory with a randomly generated name and returns the name. I think there's a similar one that creates a temp file. I can't find them offhand, but the Databel package (cran.r-project.org/web/packages/DatABEL/index.html) has a function get_temporary_file_name.
–
PaulHurleyukNov 18 '10 at 19:37

6

You should never use setwd() in R code - it basically defeats the idea of using a working directory because you can no longer easily move your code between computers.
–
hadleyNov 20 '10 at 23:44

3

@hadley interesting topic to ponder, I'd appreciate your thoughts on other methods to the same end. At work, all computers are sync'd to the same network so file paths are consistent. If they aren't, we have bigger issues to deal with than portability of a script. In this particular example, I was writing a script that would be loaded on a machine that will be carried around our national parks for 2 years. This script will grab data from a local SQL instance, do some processing, and spit out a .csv. The end product will be a .bat file that the end user will never have to modify.
–
ChaseNov 22 '10 at 4:20

@Chase But you don't need to setwd to work with network paths. You just need to provide paths to save results and still work with current path (that one that is established when R session started). Or start R with desire working directory.
–
MarekNov 29 '10 at 22:17

1

Yep. Or parametrize out_dir <- "path/to/output/directory" and then use write.table(file = file.path(out_dir,"table_1.csv"), ...). Or even out_file <- function(fnm) file.path("path/to/output/directory", fnm) and then write.table(file = out_file("table_1.csv"), ...) (similar method I use when working with network drives).
–
MarekNov 29 '10 at 22:23

In terms of general architecture I would recommend the following structure with regard to directory creation. This will cover most potential issues and any other issues with directory creation will be detected by the dir.create call.