我收到了其中一个错误.

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>"        

What does the error mean, and how can I fix it?

一些简单的例子重现了错误和常见的变体:

a a
## Error: unexpected symbol in "a a"
a\
## Error: unexpected input in "a\"
a""
## Error: unexpected string constant in "a"""
""1
## Error: unexpected numeric constant in """1"
%%
## Error: unexpected SPECIAL in "%%"
,
## Error: unexpected ',' in ","
=
## Error: unexpected '=' in "="
)
## Error: unexpected ')' in ")"
else
## Error: unexpected 'else' in "else"

推荐答案

这些错误意味着您试图运行的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中,ifforreturn等都是函数,所以需要将它们的内容用括号括起来.

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,或创建带引号的xPathsql查询时,这是一个常见问题.

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

"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

R相关问答推荐

如何通过Exams2黑板对非整数字的问题进行评分

geom_raster不适用于x比例中超过2,15的值

使用sensemakr和fixest feols模型(R)

如何对数据集进行逆向工程?

在发布到PowerBI Service时,是否可以使用R脚本作为PowerBI的数据源?

将年度数据插入月度数据

ggplot2中的X轴显示数值,单位为百,而不是十

RStudio中相关数据的分组箱形图

使用tidy—select创建一个新的带有mutate的摘要变量

如何自定义3D散点图的图例顺序?

如何在所有绘图中保持条件值的 colored颜色 相同?

对于变量的每个值,仅 Select 包含列表中所有值的值.R

以字符格式导入的ExcelElectron 表格日期列标题

将选定的索引范围与阈值进行比较

从R中发出的咕噜声中的BUG?

在具有多个响应变量的比例堆叠条形图上方添加总计

如何删除设置大小的曲线图并添加条形图顶部数字的百分比

在ggplot2上从多个数据框创建复杂的自定义图形

计算使一组输入值最小化的a、b和c的值

按组和连续id计算日期差