Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R how can you tell if a string includes escape sequences?

Tags:

r

escaping

I have a string in R, e.g. x <- "c:\tmp\rest.zip". How can I detect that it has escape sequences in it, vis. \t and \r? Us DOS/Windows guys have a habit of using backslashes that R doesn't like and I'm writing a function where I would like to be able to protect the user from themselves.

Thanks.

like image 612
russellpierce Avatar asked Dec 26 '10 16:12

russellpierce


1 Answers

Doubling of the back-slashes in the grep pattern is the path to success:

 xtxt <- c("test\n", "of\t", "escapes")
 grep("\\n|\\t", xtxt)
# [1] 1 2

Another way to be to search for control characters:

 grep("[[:cntrl:]]", xtxt)
#[1] 1 2
like image 75
IRTFM Avatar answered Nov 15 '22 00:11

IRTFM