I have a function that looks like this:
read_data <- function(filename, header) {
path <- paste("./output/", filename, sep = "")
if (file.exists(path)) {
data <- read.csv(file = path, header = header, sep = ",")
}
# Partially removed for brevity.
}
What I want to achieve is that, given a filename, I want to search whether that filename is available inside the output
subdirectory, which is a subdirectory where my script is located, and if it is available, I want to read that file. The problem is that as long as I know read.csv
function's file argument requires a full path for the file. So, I somehow need to get the directory where my script is located, so I can concatenate it with the rest of the subdirectory and filename. I can get the current working directory with getwd()
, but that's not quite the same thing, as my working directory seems always to be fixed, whereas the script can be located anywhere in the computer. Any ideas how to get the directory of the script, and concatenate it with the output subdirectory and the provided filename in R?