R 错误:代码中出现意外的符号/输入/字符串常量/数字常量/特殊

R 错误:代码中出现意外的符号/输入/字符串常量/数字常量/特殊,r,r-faq,R,R Faq,我收到了其中一个错误 Error: unexpected symbol in "<my code>" Error: unexpected input in "<my code>" Error: unexpected string constant in "<my code>" Error: unexpected numeric constant in "<my code>" Error: unexpected SPECIAL in

我收到了其中一个错误

Error: unexpected symbol in "<my code>"    
Error: unexpected input in "<my code>"
Error: unexpected string constant in "<my code>"  
Error: unexpected numeric constant in "<my code>"   
Error: unexpected SPECIAL in "<my code>"         
Error: unexpected '<some punctuation>' in "<my code>" 
Error: unexpected '<reserved word>' in "<my code>"        

这些错误意味着您试图运行的R代码或源代码在语法上不正确。也就是说,你有一个打字错误

要解决此问题,请仔细阅读错误消息。错误消息中提供的代码显示R认为问题所在的位置。在原始代码中找到这一行,并查找输入错误


预防措施,防止您再次出现错误

避免语法错误的最好方法是编写时髦的代码。这样,当您输入错误时,问题将更容易发现。有许多从页面链接的R样式指南。您还可以使用
formator
包自动将代码格式化为更可读的格式。在RStudio中,键盘快捷键CTRL+SHIFT+A将重新格式化代码

考虑使用IDE或文本编辑器,突出显示匹配的括号和大括号,并以不同颜色显示字符串和数字


产生这些错误的常见语法错误

不匹配的括号、大括号或括号

如果有嵌套的圆括号、大括号或方括号,很容易将它们关闭太多次或太少

{}}
## Error: unexpected '}' in "{}}"
{{}} # OK

执行乘法时缺少
*

这是数学家常犯的错误

5x
Error: unexpected symbol in "5x"
5*x # OK

如果、for或返回括号中的值,则不换行

这是MATLAB用户的一个常见错误。在R中,
if
for
return
等都是函数,因此需要将它们的内容用括号括起来

if x > 0 {}
## Error: unexpected symbol in "if x"
if(x > 0) {} # OK

不使用多行代码

尝试在一行上编写多个表达式而不使用分号分隔会导致R失败,并使代码更难阅读

x + 2 y * 3
## Error: unexpected symbol in "x + 2 y"
x + 2; y * 3 # OK

其他
从新行开始

if
-
else
语句中,关键字
else
必须出现在
if
块末尾的同一行

if(TRUE) 1
else 2
## Error: unexpected 'else' in "else"    
if(TRUE) 1 else 2 # OK
if(TRUE) 
{
  1
} else            # also OK
{
  2
}

=
而不是
=

=
用于赋值和为函数参数赋值<代码>=测试两个值是否相等

if(x = 0) {}
## Error: unexpected '=' in "if(x ="    
if(x == 0) {} # OK

参数之间缺少逗号

调用函数时,每个参数必须用逗号分隔

c(1 2)
## Error: unexpected numeric constant in "c(1 2"
c(1, 2) # OK

不引用文件路径

文件路径只是字符串。它们需要用双引号或单引号括起来

path.expand(~)
## Error: unexpected ')' in "path.expand(~)"
path.expand("~") # OK

字符串中的引号

当试图通过
system
将引用值传递给shell,或创建引用的
xPath
sql
查询时,这是一个常见问题

双引号字符串中的双引号需要转义。同样,需要转义单引号字符串中的单引号。或者,您可以在双引号字符串中使用单引号,而无需转义,反之亦然

"x"y"
## Error: unexpected symbol in ""x"y"   
"x\"y" # OK
'x"y'  # OK  

使用卷曲引号

所谓的“智能”引号对于R编程来说并不那么智能

path.expand(“~”)
## Error: unexpected input in "path.expand(“"    
path.expand("~") # OK

使用不带反引号的非标准变量名

描述构成有效变量名的内容。如果您创建了一个无效的变量名(可能使用
assign
),那么您需要使用反引号访问它

assign("x y", 0)
x y
## Error: unexpected symbol in "x y"
`x y` # OK
这也适用于使用
check.names=FALSE
创建的数据框中的列名

dfr <- data.frame("x y" = 1:5, check.names = FALSE)
dfr$x y
## Error: unexpected symbol in "dfr$x y"
dfr[,"x y"] # OK
dfr$`x y`   # also OK

采购非R代码

source
函数从文件运行R代码。如果您试图使用它来读取数据,它将断开。也许你想要


损坏的RStudio桌面文件

由于
.RStudio desktop
文件损坏,RStudio用户出现错误的源代码错误。这些报告仅发生在2014年3月左右,因此可能是IDE特定版本的问题。可以使用支持页面上的重置RStudio


在数学绘图注释中使用不带粘贴的表达式

尝试在绘图中创建数学标签或标题时,创建的表达式必须是页面上描述的语法有效的数学表达式。否则,内容应该包含在粘贴调用中

plot(rnorm(10), ylab = expression(alpha ^ *)))
## Error: unexpected '*' in "plot(rnorm(10), ylab = expression(alpha ^ *"
plot(rnorm(10), ylab = expression(paste(alpha ^ phantom(0), "*"))) # OK
对我来说,错误是:

Error: unexpected input in "�"
修复方法是在十六进制编辑器中打开脚本,并从文件中删除前3个字符。该文件以UTF-8 BOM开头,Rscript似乎无法读取该BOM

编辑:OP请求了一个示例。来了

➜  ~ cat a.R
cat('hello world\n')
➜  ~ xxd a.R
00000000: efbb bf63 6174 2827 6865 6c6c 6f20 776f  ...cat('hello wo
00000010: 726c 645c 6e27 290a                      rld\n').
➜  ~ R -f a.R        

R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> cat('hello world\n')
Error: unexpected input in "�"
Execution halted

如果您将粘贴代码复制到R中,它有时不接受某些特殊字符,例如“~”,而是显示为�". 因此,如果某个字符出现错误,请确保使用键盘输入该字符,或者如果不起作用,请查找另一个网站进行复制粘贴。

他们可以避免多次重复。Richie,这是常见问题解答中的一个不错的补充。虽然答案列表很丰富,但“选择”却很有用“这个问题实际上可能会把用户赶走。你有没有可能把包含99%错误的链接减少到10个或更少?如果每个读到这篇文章的人都点击几个链接,然后投票关闭任何看起来质量不高的链接呢。一旦垃圾被清除,我会减少这个链接列表。顺便说一句,每个类别的链接都是从最旧到最新排序的。我不确定这在实践中会有多大用处,但我不认为这是个糟糕的主意。不过,我确实认为,如果没有大量的链接列表,这个问题会好得多。(IMO)如果您提供了一个可复制的示例,这将很有帮助。你能把文件编码设置为一个解决方案吗?我不确定我会如何提供一个可复制的例子。粘贴
plot(rnorm(10), ylab = expression(alpha ^ *)))
## Error: unexpected '*' in "plot(rnorm(10), ylab = expression(alpha ^ *"
plot(rnorm(10), ylab = expression(paste(alpha ^ phantom(0), "*"))) # OK
Error: unexpected input in "�"
➜  ~ cat a.R
cat('hello world\n')
➜  ~ xxd a.R
00000000: efbb bf63 6174 2827 6865 6c6c 6f20 776f  ...cat('hello wo
00000010: 726c 645c 6e27 290a                      rld\n').
➜  ~ R -f a.R        

R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> cat('hello world\n')
Error: unexpected input in "�"
Execution halted