Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

file io - Escaping backslash () in string or paths in R

Windows copies path with backslash , which R does not accept. So, I wanted to write a function which would convert to /. For example:

chartr0 <- function(foo) chartr('','\/',foo)

Then use chartr0 as...

source(chartr0('E:RStuffest.r'))

But chartr0 is not working. I guess, I am unable to escape /. I guess escaping / may be important in many other occasions.

Also, is it possible to avoid the use chartr0 every time, but convert all path automatically by creating an environment in R which calls chartr0 or use some kind of temporary use like using options

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From R 4.0.0 you can use r"(...)" to write a path as raw string constant, which avoids the need for escaping:

r"(E:RStuffest.r)"
# [1] "E:\RStuff\test.r"

There is a new syntax for specifying raw character constants similar to the one used in C++: r"(...)" with ... any character sequence not containing the sequence )". This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see ?Quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...