简体   繁体   中英

Why R uses forward slash (/) and not backslash (\) in file paths

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!

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).

(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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM