Replace single backslash in R

I read a few questions and answers on this topic in a stack overflow, but still don't know how to solve this problem:

My goal is to convert the directory lines of files in Windows Explorer to a form that is recognized in R, for example. C: \ Users \ Public needs to be converted to C: / Users / Public, basically, one slash should be replaced by a slash. However, R was unable to save the original string "C: \ Users \ Public" because \ U and \ P are considered an escape character.

dirTransformer <- function(str){
   str.trns <- gsub("\\", "/", str)
   return(str.trns)
   }

str <- "C:\Users\Public"
dirTransformer(str)

> Error: '\U' used without hex digits in character string starting ""C:\U"

What I am actually writing is a graphical interface where the end effect is that the user types or inserts a directory into the input field, presses a button, and then the program will automatically process it.

Someone please suggest me how to solve this problem?

+4
2

R, . , gsub("\\", "/", str), , , , - . gsub fixed=TRUE.

normalizePath, . SO.

+3
dirTransformer <- function(str){
  str.trns <- gsub("\\\\", "/", str)
  return(str.trns)
}

str <- readline()
C:\Users\Public

dirTransformer(str)

, GUI, readline(), C:\Users\Public unquoted, R :

> str
[1] "C:\\Users\\Public"

"\\" "/", "\\", "\\\\" gsub.

, R , R, , \s , readline. , "C:\Users\Public", .

+1

Source: https://habr.com/ru/post/1652587/


All Articles