java字符串常量的符号_错误:我的代码中出现意外的符号/输入/字符串常量/数字常量/ SPECIAL...

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

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

Prophylactic measures to prevent you getting the error again

避免语法错误的最好方法是编写时髦的代码 . 这样,当你输入错误的东西时,问题会更容易被发现 . 从SO R tag info页面链接了许多R样式指南 . 您还可以使用 formatR 包自动将代码格式化为更具可读性的代码 . 在RStudio中,键盘快捷键CTRL SHIFT A将重新格式化您的代码 .

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

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

else 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

}

= instead of ==

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

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

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-desktop 文件,RStudio用户have reported错误的源错误 . 这些报告仅发生在2014年3月左右,因此可能是特定版本IDE的问题 . 可以使用支持页面上的the instructions重置RStudio .

Using expression without paste in mathematical plot annotations

尝试在图中创建数学标签或 Headers 时,创建的表达式必须是语法上有效的数学表达式,如?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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值