[R] how to put a single backslash into a string, to make file paths usable on windows

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu May 7 17:29:17 CEST 2026


Às 14:18 de 07/05/2026, Chris Ryan escreveu:
> It had been about a year since I had worked in Windows, and apparently I
> had gotten rusty. Forward slashes in path names, as in
> 
> read.csv("e:/path/to/datafileOnWindows.csv")
> 
> work just fine in R on Windows.
> 
> Sorry for the bother.
> 
> --Chris Ryan
> 
> 
> 
> Chris Ryan wrote:
>> I'm trying to generalize one of my scripts so that it will run on my
>> Linux Mint computer at home and my client's Windows 11 computer at work.
>>   I'm encountering trouble when setting file paths, conditional on which
>> computer the script is running on. It's the Windows backslash issue
>>
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Probably off-topic but you can automate switching from "\\" to "/".
If run on Windows, normalizePath uses "\\".
Then, chartr it translates to "/".


path <- normalizePath("~")
chartr("\\", "/", path)


Hope this helps,

Rui Barradas



More information about the R-help mailing list