I have two variables, work.dir and my.file. This means the user would like to save my.file to work.dir. User is asked to enter work.dir path and this is where it gets tricky. If user enters work.dir path
c:/temp/
and I try to paste this with a my.name, I will get
c:/temp/my.file.
But if the user enters
c:/temp
I will get
c:/tempmy.file.
So far I’ve been battling this with extracting various parts of the work.dir and sticking it together to achieve consistency, but I was wondering if there’s another way (that would be perhaps more resilient)?
So far this has been my solution of getting a consistent directory that can be used to be pasted together with a file name.
work.dir <- "c:/temp"
work.dir <- paste(dirname(work.dir), basename(work.dir), sep = "")
James rightfully points out that in most cases the directory will be interpreted correctly. In case this doesn’t satisfy you and assuming your user knows he or she shouldn’t use backslashes in his directory, you can use
file.path()to solve your problem, eg like this:If your user might use Windows backslashes (and forget he has to escape them with another backslash), you can add the following controls:
This gives :