错误:我的代码中出现意外的符号/输入/字符串常量/数字常量/SPECIAL [英] Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code

查看:426
本文介绍了错误:我的代码中出现意外的符号/输入/字符串常量/数字常量/SPECIAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这些错误之一.

I received one of these errors.

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

错误是什么意思,我该如何解决?

再现错误的一些简单示例以及常见变体:

Some simple examples that reproduce the errors, and common variants:

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代码在语法上不正确.也就是说,您有错字.

These errors mean that the R code you are trying to run or source is not syntactically correct. That is, you have a typo.

要解决此问题,请仔细阅读错误消息.错误消息中提供的代码显示R认为问题出在哪里.在原始代码中找到该行,然后查找错字.

To fix the problem, read the error message carefully. The code provided in the error message shows where R thinks that the problem is. Find that line in your original code, and look for the typo.

预防措施,以防止您再次收到错误消息

避免语法错误的最佳方法是编写时尚的代码.这样,当您键入错误的内容时,将更容易发现问题.从 SO R标签信息页链接了许多R样式指南.您还可以使用formatR包将代码自动格式化为更易读的格式.在RStudio中,键盘快捷键 CTRL + SHIFT + A 将重新格式化您的代码.

The best way to avoid syntactic errors is to write stylish code. That way, when you mistype things, the problem will be easier to spot. There are many R style guides linked from the SO R tag info page. You can also use the formatR package to automatically format your code into something more readable. In RStudio, the keyboard shortcut CTRL + SHIFT + A will reformat your code.

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

Consider using an IDE or text editor that highlights matching parentheses and braces, and shows strings and numbers in different colours.

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

括号,括号或括号不匹配

如果您有嵌套的括号,括号或括号,则很容易将它们关闭一次或多次.

If you have nested parentheses, braces or brackets it is very easy to close them one too many or too few times.

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


缺少 * 乘法时


Missing * when doing multiplication

这是数学家的常见错误.

This is a common mistake by mathematicians.

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


如果在括号中返回,返回或返回值,则不包装

这是MATLAB用户常见的错误.在R中,ifforreturn等是函数,因此需要将其内容括在括号中.

This is a common mistake by MATLAB users. In R, if, for, return, etc., are functions, so you need to wrap their contents in parentheses.

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


不使用多行代码

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

Trying to write multiple expressions on a single line, without separating them by semicolons causes R to fail, as well as making your code harder to read.

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


else从新行开始


else starting on a new line

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

In an if-else statement, the keyword else must appear on the same line as the end of the if block.

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


=代替==


= instead of ==

=用于分配和给函数参数赋值. ==测试两个值是否相等.

= is used for assignment and giving values to function arguments. == tests two values for equality.

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


参数之间缺少逗号

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

When calling a function, each argument must be separated by a comma.

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


不引用文件路径

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

File paths are just strings. They need to be wrapped in double or single quotes.

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


在字符串内引用

当尝试通过system将带引号的值传递给外壳程序或创建带引号的xPathsql查询时,这是一个常见问题.

This is a common problem when trying to pass quoted values to the shell via system, or creating quoted xPath or sql queries.

需要对双引号字符串中的双引号进行转义.同样,单引号中的单引号也需要转义.另外,您也可以在双引号引起来的字符串中使用单引号而不进行转义,反之亦然.

Double quotes inside a double quoted string need to be escaped. Likewise, single quotes inside a single quoted string need to be escaped. Alternatively, you can use single quotes inside a double quoted string without escaping, and vice versa.

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


使用弯引号

对于R编程,所谓的智能"引号不是那么聪明.

So-called "smart" quotes are not so smart for R programming.

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


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

?make.names 描述了什么构成有效的变量名.如果创建一个无效的变量名(也许使用assign),则需要使用反引号对其进行访问,

?make.names describes what constitutes a valid variable name. If you create a non-valid variable name (using assign, perhaps), then you need to access it with backquotes,

assign("x y", 0)
x y
## Error: unexpected symbol in "x y"
`x y` # OK

这也适用于使用check.names = FALSE创建的数据框中的列名.

This also applies to column names in data frames created with 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%上查找帮助.

It also applies when passing operators and other special values to functions. For example, looking up help on %in%.

?%in%
## Error: unexpected SPECIAL in "?%in%"
?`%in%` # OK


采购非R代码

source函数从文件运行R代码.如果尝试使用它读取数据,它将中断.可能您想要 read.table .

The source function runs R code from a file. It will break if you try to use it to read in your data. Probably you want read.table.

source(textConnection("x y"))
## Error in source(textConnection("x y")) : 
##   textConnection("x y"):1:3: unexpected symbol
## 1: x y
##       ^


RStudio桌面文件已损坏

RStudio用户已报告由于损坏的.rstudio-desktop文件而导致的错误源错误.这些报告仅在2014年3月左右发生,因此特定版本的IDE可能存在问题.可以使用说明重置RStudio.在支持页面上.

RStudio users have reported erroneous source errors due to a corrupted .rstudio-desktop file. These reports only occurred around March 2014, so it is possibly an issue with a specific version of the IDE. RStudio can be reset using the instructions on the support page.

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

当尝试在绘图中创建数学标签或标题时,创建的表达式必须是语法上有效的数学表达式,如

When trying to create mathematical labels or titles in plots, the expression created must be a syntactically valid mathematical expression as described on the ?plotmath page. Otherwise the contents should be contained inside a call to paste.

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

这篇关于错误:我的代码中出现意外的符号/输入/字符串常量/数字常量/SPECIAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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