String 如何将长引号(带有奇怪字符)存储为字符串

String 如何将长引号(带有奇怪字符)存储为字符串,string,r,String,R,例如,我想将字符串存储在下面(包括端点),但我无法确定函数是什么。我想quote()可以: mystring <- quote( \documentclass{article} \usepackage{graphicx} \begin{document} %\tableofcontents \addcontentsline{toc}{chapter}{List of Figures} \listoffigures \newpage \stepcounter{figure} ) m

例如,我想将字符串存储在下面(包括端点),但我无法确定函数是什么。我想
quote()
可以:

mystring <- quote(
\documentclass{article}

\usepackage{graphicx}

\begin{document}

%\tableofcontents
\addcontentsline{toc}{chapter}{List of Figures}
\listoffigures

\newpage
\stepcounter{figure}
)

mystring您需要避开反斜杠:

mystring <- "
\\documentclass{article}

\\usepackage{graphicx}

\\begin{document}

%\\tableofcontents
\\addcontentsline{toc}{chapter}{List of Figures}
\\listoffigures

\\newpage
\\stepcounter{figure}
"
cat(mystring)
#
# \documentclass{article}
#
# \usepackage{graphicx}
#
# \begin{document}
#
# %\tableofcontents
# \addcontentsline{toc}{chapter}{List of Figures}
# \listoffigures
#
# \newpage
# \stepcounter{figure}
#

# assuming your string is stored in "foo.txt"
mystring <- paste(readLines("foo.txt"), collapse="\n")

mystring谢谢你,约书亚。你确定没有别的办法吗?例如,在perl中,我认为“quote”函数允许您在不需要转义的情况下输入字符。请参见
?引号
。似乎很奇怪,没有其他方法。我想我最好的选择是将我的字符串存储在一个文件中,然后用readline读取它们。但接下来我必须弄清楚如何在向量的每个分量之间插入一个“\n”。你可以这样做,
readLines
将为你转义它们(请参见我的编辑)。以下内容不粘贴直接读取:
readChar('c:/foo.txt',file.info('c:/foo.txt')$size)
Joshua是正确的。quote函数引用表达式,而不是字符串:
quote(1+x)
将返回表达式
1+x
(并且不会抱怨x未定义)。shQuote将引号添加到字符串中,但不能帮助您避开反斜杠。
swave
会有任何帮助吗?