AnswerBun.com

Escaping backslash () in string or paths in R

Stack Overflow Asked on January 1, 2022

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:RStufftest.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

4 Answers

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:RStufftest.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.

Answered by Henrik on January 1, 2022

Your fundamental problem is that R will signal an error condition as soon as it sees a single back-slash before any character other than a few lower-case letters, backslashes themselves, quotes or some conventions for entering octal, hex or Unicode sequences. That is because the interpreter sees the back-slash as a message to "escape" the usual translation of characters and do something else. If you want a single back-slash in your character element you need to type 2 backslashes. That will create one backslash:

nchar("\")
#[1] 1

The "Character vectors" section of _Intro_to_R_ says:

"Character strings are entered using either matching double (") or single (') quotes, but are printed using double quotes (or sometimes without quotes). They use C-style escape sequences, using as the escape character, so is entered and printed as , and inside double quotes " is entered as ". Other useful escape sequences are n, newline, t, tab and b, backspace—see ?Quotes for a full list."

 ?Quotes

Answered by IRTFM on January 1, 2022

The problem is that every single forward slash and backslash in your code is escaped incorrectly, resulting in either an invalid string or the wrong string being used. You need to read up on which characters need to be escaped and how. Take a look at the list of escape sequences in the link below. Anything not listed there (such as the forward slash) is treated literally and does not require any escaping.

http://cran.r-project.org/doc/manuals/R-lang.html#Literal-constants

Answered by Ryan C. Thompson on January 1, 2022

chartr0 <- function(foo) chartr('\','/',foo)
chartr0('E:\RStuff\test.r')

You cannot write E:Rxxxx, because R believes R is escaped.

Answered by Dieter Menne on January 1, 2022

Add your own answers!

Related Questions

Seperate strings with regex and panda

1  Asked on January 11, 2021 by sara-daniel

         

Login into SAP using admin user fails

1  Asked on January 11, 2021 by gram77

 

Using LIKE with multiple columns in a table

3  Asked on January 10, 2021 by soldfor

   

Does the value of global variable persist in multiple API calls

2  Asked on January 10, 2021 by shubroto-shuvo

 

React Component not rendering to HTML

1  Asked on January 10, 2021 by red-apple

     

How can I count a pandas dataframe over duplications

4  Asked on January 10, 2021 by toby-chamberlain

     

javascript .eval() gives me undifined

2  Asked on January 10, 2021 by nourza

     

Loop for column names in python

3  Asked on January 10, 2021 by essegn

         

Return array of values from object based on array

2  Asked on January 9, 2021 by user4584963

         

How do I get out of ‘screen’ without typing ‘exit’?

5  Asked on January 9, 2021 by meder-omuraliev

   

How can I regroup ‘sublists’ in a list with Python?

2  Asked on January 9, 2021 by zhengrong

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir