How to escape backslashes in R string

后端 未结 3 1964
误落风尘
误落风尘 2020-11-22 09:56

I\'m writing strings which contain backslashes (\\) to a file:

x1 = "\\\\str"

x2 = "\\\\\\str"
# Error: \'\\s\' is an unreco         


        
3条回答
  •  星月不相逢
    2020-11-22 10:30

    Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:

    > tmp <- scan(what='')
    1: \\\\\str
    2: 
    Read 1 item
    > print(tmp)
    [1] "\\\\\\\\\\str"
    > cat(tmp, '\n')
    \\\\\str 
    > 
    

提交回复
热议问题