50

I am using Rscript to plot some figures from a given CSV file in some directory, which is not necessarily my current working directory. I can call it as follows:

./script.r ../some_directory/inputfile.csv

Now I want to output my figures in the same directory (../some_directory), but I have no idea how to do that. I tried to get the absolute path for the input file because from this I could construct the output path, but I couldn't find out how to do that.

6 Answers 6

71
normalizePath() #Converts file paths to canonical user-understandable form

or

library(tools)
file_path_as_absolute()
4
  • 3
    Both normalizePath(".") and file_path_as_absolute(".") still work for me on R 3.2.3.
    – MattBagg
    Commented Feb 26, 2016 at 0:36
  • @Greg can you list which version(s) of R you found that this did not work on? I am about to integrate this in my program and it would be really helpful to know ahead of time, thanks. Commented Oct 10, 2017 at 18:31
  • 1
    @user5359531 great question. I'm not entirely sure but I think I was using R 3.2.3, but I did not try using MattBagg's update
    – Greg
    Commented Oct 17, 2017 at 21:37
  • 1
    normalizePath() is part of base R and should be safe to use in your codebase.
    – MattBagg
    Commented Oct 28, 2017 at 17:21
10

The question is very old but it still misses a working solution. So here is my answer:

Use normalizePath(dirname(f)). The example below list all the files and directories in the current directory.

dir <- "."
allFiles <- list.files(dir)
for(f in allFiles){
  print(paste(normalizePath(dirname(f)), fsep = .Platform$file.sep, f, sep = "")) 
}

Where:

  • normalizePath(dirname(f)) gives the absolute path of the parent directory. So the individual file names should be added to the path.
  • .Platform is used to have an OS-portable code. (here)
  • file.sep gives "the file separator used on your platform: "/" on both Unix-alikes and on Windows (but not on the former port to Classic Mac OS)." (here)

Warning: This may cause some problems if not used with caution. For instance, say this is the path: A/B/a_file and the working directory is now set to B. Then the code below:

dir <- "B"
allFiles <- list.files(dir)
for(f in allFiles){
  print(paste(normalizePath(dirname(f)), fsep = .Platform$file.sep, f, sep = "")) 
}

would give:

> A/a_file

however, it should be:

> A/B/a_file
7

Here the solution:

args = commandArgs(TRUE)

results_file = args[1]

output_path = dirname(normalizePath(results_file))
1
  • It seems dirname() only gives the path minus the current folder. Is there something similar that would give the full path?
    – Phillip
    Commented Oct 17, 2014 at 19:33
3

fs::path_abs() is my preferred way. It avoids the backslashes of normalizePath().

2

To get the absolute path(s) from file(s)

Why not combine the base R function file.path() with the answer that @Marius gave. This appears marginally simpler, will work with a vector of files (files), and take care of system specific separators:

file.path(normalizePath(dirname(files)), files)

And wrapped inside a function (abspath):

abspath <- function(files)file.path(normalizePath(dirname(files)), files)

For instance:

> setwd("~/test")
> list.files()
[1] "file1.txt" "file2.txt"

And then:

> abspath(files)
[1] "/home/myself/test/file1.txt" "/home/myself/test/file2.txt"
1

I see that people gave pieces of the solution, but not all of it. I have used this:

outputFile = paste(normalizePath(dirname(inputFile)),"\\", "my_file.ext", sep = "")

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.