忽略R字符串中的转义字符(反斜杠) [英] Ignore escape characters (backslashes) in R strings

查看:348
本文介绍了忽略R字符串中的转义字符(反斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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).

I但是无法找到可以用foward斜杠替换反斜杠的功能,或者双重转义它们。所有这些功能都假定这些角色被转义。

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


推荐答案

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

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

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

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

这篇关于忽略R字符串中的转义字符(反斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆