这些错误意味着您试图运行的R代码或源代码在语法上不正确.也就是说,你有一个拼写错误.
要解决此问题,请仔细阅读错误消息.错误消息中提供的代码显示了R认为问题所在的位置.在你的原始代码中找到这一行,并查找输入错误.
Prophylactic measures to prevent you getting the error again
避免语法错误的最佳方法是编写时髦的代码.这样,当你输入错误时,问题就会更容易被发现.有许多R风格的指南从SO R tag info页链接.您还可以使用formatR
包自动将代码格式化为更可读的格式.在RStudio中,键盘快捷键CTRL+SHIFT+A将重新格式化代码.
考虑使用IDE或文本编辑器,突出显示匹配的括号和括号,并显示不同 colored颜色 的字符串和数字.
Common syntactic mistakes that generate these errors
Mismatched parentheses, braces or brackets
如果有嵌套的括号、大括号或括号,很容易将它们关闭太多次或太少.
{}}
## Error: unexpected '}' in "{}}"
{{}} # OK
Missing *
when doing multiplication
这是数学家常犯的错误.
5x
Error: unexpected symbol in "5x"
5*x # OK
Not wrapping if, for, or return values in parentheses
这是MATLAB用户的一个常见错误.在R中,if
、for
、return
等都是函数,所以需要将它们的内容用括号括起来.
if x > 0 {}
## Error: unexpected symbol in "if x"
if(x > 0) {} # OK
Not using multiple lines for code
试图在一行中编写多个表达式,而不使用分号分隔它们,会导致R失败,并使代码更难阅读.
x + 2 y * 3
## Error: unexpected symbol in "x + 2 y"
x + 2; y * 3 # OK
100 starting on a new line
在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
}
100 instead of 101
=
用于赋值和给函数参数赋值.==
测试两个值是否相等.
if(x = 0) {}
## Error: unexpected '=' in "if(x ="
if(x == 0) {} # OK
Missing commas between arguments
调用函数时,每个参数必须用逗号分隔.
c(1 2)
## Error: unexpected numeric constant in "c(1 2"
c(1, 2) # OK
Not quoting file paths
文件路径只是字符串.它们需要用双引号或单引号括起来.
path.expand(~)
## Error: unexpected ')' in "path.expand(~)"
path.expand("~") # OK
Quotes inside strings
当试图通过system
将带引号的值传递给shell,或创建带引号的xPath
或sql
查询时,这是一个常见问题.
双引号字符串中的双引号需要转义.同样,单引号字符串中的单引号也需要转义.或者,可以在双引号字符串中使用单引号,而无需转义,反之亦然.
"x"y"
## Error: unexpected symbol in ""x"y"
"x\"y" # OK
'x"y' # OK
Using curly quotes
所谓的"智能"引号对于R编程来说并不那么智能.
path.expand(“~”)
## Error: unexpected input in "path.expand(“"
path.expand("~") # OK
Using non-standard variable names without backquotes
?make.names
描述如何构成有效的变量名.如果您创建了一个无效的变量名(可能使用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
它也适用于向函数传递运算符和其他特殊值.例如,在%in%
上查找帮助.
?%in%
## Error: unexpected SPECIAL in "?%in%"
?`%in%` # OK
Sourcing non-R code
source
函数从文件中运行R代码.如果你试图用它来读取数据,它就会崩溃.也许你想要read.table
.
source(textConnection("x y"))
## Error in source(textConnection("x y")) :
## textConnection("x y"):1:3: unexpected symbol
## 1: x y
## ^
Corrupted RStudio desktop file
RStudio用户由于.rstudio-desktop
文件损坏导致have reported个错误源错误.这些报告仅发生在2014年3月左右,因此可能是IDE的特定版本存在问题.RStudio可以使用支持页面上的the instructions重置.
Using expression without paste in mathematical plot annotations
当试图在绘图中创建数学标签或标题时,所创建的表达式必须是语法上有效的数学表达式,如?plotmath
页所述.否则,内容应该包含在粘贴调用中.
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