相关疑难解决方法(0)

在R代码中使用分号或显式新行有什么区别

我原本以为分号;相当于添加一个明确的新行,例如

x <- 1; y <- 2
Run Code Online (Sandbox Code Playgroud)

是相同的

x <- 1
x <- 2
Run Code Online (Sandbox Code Playgroud)

当然,R文档似乎没有区别于语法上完整的语句:

分号和新行都可用于分隔语句.分号始终表示语句的结尾,而新行可以表示语句的结束.如果当前语句在语法上不完整,则评估者将忽略新行.

但是,我发现至少在Rstudio服务器中,分号的行为与新行不同.例如:

> temp_a ; temp_b <- 1 ; temp_c <- 2
Error: object 'temp_a' not found
> exists("temp_b")
[1] FALSE
> exists("temp_c")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)

相比

> temp_a
Error: object 'temp_a' not found
> temp_b <- 1
> temp_c <- 2
> 
> exists("temp_b")
[1] TRUE
> exists("temp_c")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)

为什么会这样?还有其他问题我应该留意吗?

r

31
推荐指数
1
解决办法
1万
查看次数

标签 统计

r ×1