0

Firstly, I understand that "\" is an R escape character and also the file path separator on windows.

I know that it can be escaped by using either / or \.

I am developing a package and I want a function for the user to literally just be able to call it like:

makeFileLocationRCompatable("H:\Temp") 

and for the function to return

"H:/Temp" 

or

"H:\\Temp"

but it seems to be impossible in R due to the fact that \ escapes the following character.

I don't want my users to have to change the way they input the file path.

Any ideas?

JamesM95
  • 21
  • 2
  • this might be helpful (see the comment below the accepted answer): https://stackoverflow.com/questions/25424382/how-to-replace-single-backslash-in-r. – huan Dec 10 '18 at 10:16
  • Possible duplicate of [how to replace single backslash in R](https://stackoverflow.com/questions/25424382/how-to-replace-single-backslash-in-r) – phiver Dec 10 '18 at 18:42

2 Answers2

1

You can use Rstudio snippetsaddin to convert the slash download it from here.

devtools::install_github("sfr/RStudio-Addin-Snippets", type = "source")

Restart Rstudio.
Select the path or the code where slashes needs to be replaced. 
Click on Addin -> select convert slash
It will reverse all slashes if the path is selected.
Hunaidkhan
  • 1,411
  • 2
  • 11
  • 21
1

normalizePath from the base package might provide this functionality? (I cannot test on Windows myself; sorry if this is a moot proposal)

For example

normalizePath('H:\\Temp', winslash = '\\')

See also ?normalizePath