1

I want to replace '\' by '/' regardless of how many times it is present in the input string.

I have read these : Replace "\" with "/" in r

Efficiently convert backslash to forward slash in R

But both of them gives solution where the directory_path is read through clipboard paste or through scan().

I want to give directory path as a variable.

Example Code :

directory_path = 'D:\demo\app'

gsub( '\', '/', directory_path )

But it is giving error.

> directory_path = 'D:\demo\app'
Error: '\d' is an unrecognized escape in character string starting "'D:\d"
alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Escape like so: `test<-c("D:\\demo\\app")`. – NelsonGon Feb 16 '19 at 16:03
  • 1
    I wonder how many questions about R's need to doubly escape the `"\"` there are. I suspect they number in the hundreds. Well, maybe not. Here's the result of a search, but most of the answers are not on point: https://stackoverflow.com/search?q=%5Br%5D+replace+%27%5C%27 – IRTFM Feb 16 '19 at 16:22
  • 1
    the path will be input by user say and he cannot be asked to give input with double slash. How to tackle that part ? I initiated this question just for this. Haven't found answer to this form in other questions. – Amar Nath Boral Feb 17 '19 at 16:27

1 Answers1

2

Escape them like:

test <- c("D:\\demo\\app")

Then:

gsub("\\\\", "/", test)
alistaire
  • 42,459
  • 4
  • 77
  • 117
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    the directory path will be given input by a user say, who does not know about escape characters. He will give 'D:\demo\app' and cannot be asked to give otherwise. How to tackle it ? – Amar Nath Boral Feb 17 '19 at 16:25