5

I was teaching an online course and a student asked me why R only uses / and not \ in file paths when using read.csv and other related functions. I tried looking at the documentation but it didn’t really mention anything about it. Never really thought about it because I use a Mac, and the default in Macs is \, but not so in Windows machines.

I’m not trained in computer science so I was left a bit stumped to answer the question a I’m afraid. Students always ask the darnest things!

2

2 Answers 2

9

Interesting question.

First off, the "forward slash" / is actually more common as it used by Unix, Linux, and macOS.

Second, the "backward slash" \ is actually somewhat painful as it is also an escape character. So whenever you want one, you need to type two in string: "C:\\TEMP".

Third, R on Windows knows this and helps! So you can you use a forward slash whereever you would use a backward slash: "C:/TEMP" works the same!

Fourth, you can have R compute the path for you and it will use use the separator: file.path("some", "dir").

So the short answer: R uses both on Windows and lets you pick whichever you find easier. But remember to use two backward slashes (unless you use the very new R 4.0.0 feature on raw strings which I'll skip for now).

Sign up to request clarification or add additional context in comments.

4 Comments

I think this will actually be my main use case for raw strings. I often copy paths from the windows explorer and just wrapping these in r"(...)" is so much easier than what I have been doing so far.
Thanks for that! So what’s an escape character?
See e.g. the second link I posted above.
Thanks for adding the links, @Henrik. And yes, raw strings are awesome. Small price is the dependency on R >= 4.0.0. Old R 3.* installations are still out there a lot. No issue for local work.
1

(Note: backslashes as directory folder separators on Macs is a recent innovation.See History of Mac folder separators

I think if you review the history (or look it up if you were not there when it occurred as I was) you will find that Unix (which Linux copied completely) got there first. It preceded either MS-DOS or Macs or last of all Windows. R is a work-alike clone of S which was developed like Unix at Bell Labs.

Mac originally used colons (:) as folder separators (and still won't accept them in file names) and converted to slashes sometime during its long transition to BSD Unix which it licensed from ATT.

Shouldn't the question be: why Microsoft chose to use a backslash?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.