简体   繁体   English

如何将 chr 变量传递给 r"(...)"?

[英]How to pass a chr variable into r"(...)"?

I've seen that since 4.0.0, R supports raw strings using the syntax r"(...)" .我已经看到,自 4.0.0 以来,R 支持使用语法r"(...)"的原始字符串。 Thus, I could do:因此,我可以这样做:

r"(C:\THIS\IS\MY\PATH\TO\FILE.CSV)"
#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"

While this is great, I can't figure out how to make this work with a variable, or better yet with a function. See this comment which I believe is asking the same question.虽然这很好,但我无法弄清楚如何使用变量或更好地使用 function 来实现这一点。请参阅我认为提出相同问题的评论

This one can't even be evaluated:这个甚至无法评估:

construct_path <- function(my_path) {
  r"my_path"
}

Error: malformed raw string literal at line 2错误:第 2 行格式错误的原始字符串文字
} }
Error: unexpected '}' in "}"错误:“}”中出现意外的“}”

Nor this attempt:也不是这种尝试:

construct_path_2 <- function(my_path) {
  paste0(r, my_path)
}

construct_path_2("(C:\THIS\IS\MY\PATH\TO\FILE.CSV)")

Error: '\T' is an unrecognized escape in character string starting ""(C:\T"错误:'\T' 是字符串中无法识别的转义符,以“”(C:\T" 开头


Desired output所需 output

# pseudo-code
my_path <- "C:\THIS\IS\MY\PATH\TO\FILE.CSV"
construct_path(path)

#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"

EDIT编辑


In light of @KU99's comment , I want to add the context to the problem.鉴于@KU99 的评论,我想为问题添加上下文。 I'm writing an R script to be run from command-line using WIndows's CMD and Rscript.我正在编写一个 R 脚本,使用 WIndows 的 CMD 和 Rscript 从命令行运行。 I want to let the user who executes my R script to provide an argument where they want the script's output to be written to.我想让执行我的 R 脚本的用户提供一个参数,他们希望将脚本的 output 写入其中。 And since Windows's CMD accepts paths in the format of C:\THIS\IS\MY\PATH\TO , then I want to be consistent with that format as the input to my R script.由于 Windows 的 CMD 接受格式为C:\THIS\IS\MY\PATH\TO的路径,因此我希望与该格式保持一致,作为我的 R 脚本的输入。 So ultimately I want to take that path input and convert it to a path format that is easy to work with inside R. I thought that the r"()" thing could be a proper solution.所以最终我想采用该路径输入并将其转换为易于在 R 内部使用的路径格式。我认为r"()"可能是一个合适的解决方案。

I think you're getting confused about what the string literal syntax does.我认为您对字符串文字语法的作用感到困惑。 It just says "don't try to escape any of the following characters".它只是说“不要试图转义以下任何字符”。 For external inputs like text input or files, none of this matters.对于文本输入或文件等外部输入,这些都不重要。

For example, if you run this code例如,如果您运行此代码

path <- readline("> enter path: ")

You will get this prompt:你会得到这个提示:

> enter path:

and if you type in your (unescaped) path:如果您输入您的(未转义的)路径:

> enter path: C:\Windows\Dir

You get no error, and your variable is stored appropriately:您没有收到任何错误,并且您的变量已正确存储:

path
#> [1] "C:\\Windows\\Dir"

This is not in any special format that R uses, it is plain text.这不是 R 使用的任何特殊格式,它是纯文本。 The backslashes are printed in this way to avoid ambiguity but they are "really" just single backslashes, as you can see by doing反斜杠以这种方式打印以避免歧义,但它们“实际上”只是单个反斜杠,正如您可以看到的那样

cat(path)
#> C:\Windows\Dir

The string literal syntax is only useful for shortening what you need to type.字符串文字语法仅对缩短您需要输入的内容有用。 There would be no point in trying to get it to do anything else, and we need to remember that it is a feature of the R interpreter - it is not a function nor is there any way to get R to use the string literal syntax dynamically in the way you are attempting.试图让它做任何其他事情是没有意义的,我们需要记住它是 R解释器的一个特性 - 它不是 function也没有任何方法可以让 R 动态使用字符串文字语法以你尝试的方式。 Even if you could, it would be a long way for a shortcut.即使可以,走捷径也是很长的路要走。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM