在R字符串中打印反斜杠 [英] print backslash in R strings

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

问题描述

GNU R 3.02

GNU R 3.02

> bib <- "\cite"
Error: '\c' is an unrecognized escape in character string starting ""\c"
> bib <- "\\cite"
> print(bib)
[1] "\\cite"
> sprintf(bib)
[1] "\\cite"
> 

如何仅用一个"\"打印出字符串变量bib?

how can I print out the string variable bib with just one "\"?

(我尝试了所有可能的方法,发现R将"\\"视为一个字符.)

(I've tried everything conceivable, and discover that R treats the "\\" as one character.)

我看到在很多情况下这都不是问题,因为这通常是由R内部处理的,例如,如果要将字符串用作绘图的文本.

I see that in many cases this is not a problem, since this is usually handled internally by R, say, if the string were to be used as text for a plot.

但是我需要将其发送到LaTeX.所以我真的必须将其删除.

But I need to send it to LaTeX. So I really have to remove it.

我看到cat可以解决问题.如果只能使cat将其结果发送到字符串.

I see cat does the trick. If cat could only be made to send its result to a string.

推荐答案

您应使用cat.

bib <- "\\cite"
cat(bib)
# \cite


您可以通过在knitr中设置一些选项来删除##[1].这是一个示例块:


You can remove the ## and [1] by setting a few options in knitr. Here is an example chunk:

<<newChunk,echo=FALSE,comment=NA,background=NA>>=
bib <- "\\cite"
cat(bib)
@

这会使您\cite.还要注意,您可以全局设置这些选项.

which gets you \cite. Note as well that you can set these options globally.

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

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