Paste windows path converting backslashes to slashes automatically


I use RStudio 1.1.419 on Windows 7.
When I need to copy a file or directory path into my script, from an Explorer window for example, i then need to manually replace \ by / in order to get a valid path.
Is there a kind of "paste path replacing \" option in Rstudio ?

thanks for your help !

For your specific issue, one R based solution I'm aware of is to use the utils function readClipboard()

For example, if you copy "C:\Program Files\R" from the address bar of windows explorer


Then in the console do:

fp <- readClipboard()
#> [1] "C:\\Program Files\\R"

You'll see
"C:\Program Files\R" has automagically been converted to
"C:\\Program Files\\R" (which also works in the same way as using a forward slash / does)

In more general terms there's also the base function file.path() and also the here package if you'd like to construct file paths in a more platform independent way...

Hope this helps...


markdly's suggestion is pretty neat. I don't use windows much but have seen this windows-explorer file path box and can see how you'd rely on it.

Auto Complete File Path - Another suggestion, I quite like just using RStudio IDE's autocomplete when you're filing out a filepath string. If you're unfamiliar with this, if you start with a function that requires a filepath, e.g. read.csv(file = "") and put your cursor between the two quote-marks, and then hit tab, RStudio will help guide you to a file path. You can start typing C: to start that path from your C-drive.

1 Like

thanks markdly,
i think the best solution for me is to have an add-in which executes the following code : normalizepath(readClipboard(),winslash = "/")
With a keyboard shortcut for it, it will be perfect :slight_smile:


Dear All
I would like to present a quick / working solution, which is not given in above topic.

Issue is:
You are programming in RStudio and you are coping a path from your Windows Explorer to utilize it in RStudio.
RStudio itself can not work properly with \ (backslash) it needs / (slash) in its code.

Solution for example to create a directory:
> dir.create(file.path(readClipboard(), "testdir-02"))
#This will create a directory (testdir-02) in the path you provided into the readClipboard() function earlier. This means that previously to utilize the above code line you have to copy the needed path to your clipboard. (Ctrl. + C)
#The result is in my case: U:\Data Science\testdir-02
#If checking what I had in the clipboard, I can show it below:

fp <- readClipboard()
[1] "U:\Data Science"