忽略R字符串中的转义字符(反斜杠)

时间:2022-02-01 22:28:03

While running an R-plugin in SPSS, I receive a Windows path string as input e.g.

在SPSS中运行R插件时,我收到一个Windows路径字符串作为输入,例如

'C:\Users\mhermans\somefile.csv'

I would like to use that path in subsequent R code, but then the slashes need to be replaced with forward slashes, otherwise R interprets it as escapes (eg. "\U used without hex digits" errors).

我想在后续的R代码中使用该路径,但是斜杠需要用正斜杠替换,否则R将其解释为转义(例如“\ UU,不使用十六进制数字”错误)。

I have however not been able to find a function that can replace the backslashes with foward slashes or double escape them. All those functions assume those characters are escaped.

然而,我无法找到一个可以用斜线替换反斜杠或双重逃避它们的函数。所有这些函数都假设这些字符被转义。

So, is there something along the lines of:

那么,是否有类似的东西:

>gsub('\\', '/', 'C:\Users\mhermans')
C:/Users/mhermans

3 个解决方案

#1


12  

You can try to use the 'allowEscapes' argument in scan()

您可以尝试在scan()中使用'allowEscapes'参数

X=scan(what="character",allowEscapes=F)
C:\Users\mhermans\somefile.csv

print(X)
[1] "C:\\Users\\mhermans\\somefile.csv"

#2


5  

First you need to get it assigned to a name:

首先,您需要将其分配给名称:

pathname <- 'C:\\Users\\mhermans\\somefile.csv'

Notice that in order to get it into a name vector you needed to double them all, which gives a hint about how you could use regex:

请注意,为了将其添加到名称向量中,您需要将它们全部加倍,这提示了如何使用正则表达式:

 gsub("\\\\", "/", pathname)
# [1] "C:/Users/mhermans/somefile.csv"

You needed to doubly double the backslashes. The first of each couple of \'s is to signal to the grep machine that what nextcomes is a literal.

你需要加倍反斜杠。每一对中的第一个是向grep机器发出信号,表明接下来的是文字。

#3


1  

If file E:\Data\junk.txt contains the following text (without quotes): C:\Users\mhermans\somefile.csv

如果文件E:\ Data \ junk.txt包含以下文本(不带引号):C:\ Users \ mhermans \ somefile.csv

You may get a warning with the following statement, but it will work:

您可能会收到以下声明的警告,但它会起作用:

 texinp <- readLines("E:\\Data\\junk.txt")

If file E:\Data\junk.txt contains the following text (with quotes): "C:\Users\mhermans\somefile.csv"

如果文件E:\ Data \ junk.txt包含以下文本(带引号):“C:\ Users \ mhermans \ somefile.csv”

The above readlines statement might also give you a warning, but will now contain:

上面的readlines语句也可能会给你一个警告,但现在包含:

"\"C:\Users\mhermans\somefile.csv\""

“\” C:\用户\ mhermans \ somefile.csv \ “”

So, to get what you want, make sure there aren't quotes in the incoming file, and use:

因此,要获得所需内容,请确保传入文件中没有引号,并使用:

 texinp <- suppressWarnings(readLines("E:\\Data\\junk.txt"))

#1


12  

You can try to use the 'allowEscapes' argument in scan()

您可以尝试在scan()中使用'allowEscapes'参数

X=scan(what="character",allowEscapes=F)
C:\Users\mhermans\somefile.csv

print(X)
[1] "C:\\Users\\mhermans\\somefile.csv"

#2


5  

First you need to get it assigned to a name:

首先,您需要将其分配给名称:

pathname <- 'C:\\Users\\mhermans\\somefile.csv'

Notice that in order to get it into a name vector you needed to double them all, which gives a hint about how you could use regex:

请注意,为了将其添加到名称向量中,您需要将它们全部加倍,这提示了如何使用正则表达式:

 gsub("\\\\", "/", pathname)
# [1] "C:/Users/mhermans/somefile.csv"

You needed to doubly double the backslashes. The first of each couple of \'s is to signal to the grep machine that what nextcomes is a literal.

你需要加倍反斜杠。每一对中的第一个是向grep机器发出信号,表明接下来的是文字。

#3


1  

If file E:\Data\junk.txt contains the following text (without quotes): C:\Users\mhermans\somefile.csv

如果文件E:\ Data \ junk.txt包含以下文本(不带引号):C:\ Users \ mhermans \ somefile.csv

You may get a warning with the following statement, but it will work:

您可能会收到以下声明的警告,但它会起作用:

 texinp <- readLines("E:\\Data\\junk.txt")

If file E:\Data\junk.txt contains the following text (with quotes): "C:\Users\mhermans\somefile.csv"

如果文件E:\ Data \ junk.txt包含以下文本(带引号):“C:\ Users \ mhermans \ somefile.csv”

The above readlines statement might also give you a warning, but will now contain:

上面的readlines语句也可能会给你一个警告,但现在包含:

"\"C:\Users\mhermans\somefile.csv\""

“\” C:\用户\ mhermans \ somefile.csv \ “”

So, to get what you want, make sure there aren't quotes in the incoming file, and use:

因此,要获得所需内容,请确保传入文件中没有引号,并使用:

 texinp <- suppressWarnings(readLines("E:\\Data\\junk.txt"))