I think you want normalizePath() :
> setwd("~/tmp/bar") > normalizePath("../tmp.R") [1] "/home/gavin/tmp/tmp.R" > normalizePath("~/tmp/tmp.R") [1] "/home/gavin/tmp/tmp.R" > normalizePath("./foo.R") [1] "/home/gavin/tmp/bar/foo.R"
On Windows, there is a winslash argument that you can set all the time, since it is ignored by nothing but Windows, so it does not affect other OSs:
> normalizePath("./foo.R", winslash="\\") [1] "/home/gavin/tmp/bar/foo.R"
(You need to avoid \ , therefore, \\ ) or
> normalizePath("./foo.R", winslash="/") [1] "/home/gavin/tmp/bar/foo.R"
depending on how you want the path to be presented / used. The first is the default ( "\\" ), so you can stick to this if that's enough, without having to explicitly specify.
In R 2.13.0, the bit "~/file.ext" also works (see comments):
> normalizePath("~/foo.R") [1] "/home/gavin/foo.R"
Gavin simpson
source share