R“for循环"错误消息{} [英] R "for loop" error messages {}

查看:24
本文介绍了R“for循环"错误消息{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,当我在 R 中运行 for 循环时,它给了我很多与}"相关的错误.只有当我突出显示整个代码并运行它时才会发生.如果我一行一行地执行它,那么它运行得很好.

Since a couple of days ago when I run a for loop in R it gives me plenty of errors related to "}". It only happens if I highlight the whole code and run it. If i execute it line by line, then it runs just fine.

我什至尝试使用最基本的循环:

I tried even with the most basic loop:

foo <- seq(1, 100, by=2)
foo.squared <- NULL
for (i in 1:50 ) {
foo.squared[i] <- foo[i]^2
}

这是控制台:

> foo <- seq(1, 100, by=2)
"rror: unexpected input in "foo <- seq(1, 100, by=2)
> foo.squared <- NULL
"rror: unexpected input in "foo.squared <- NULL
> for (i in 1:50 ){
"rror: unexpected input in "for (i in 1:50 ){
> foo.squared[i] <- foo[i]^2
"rror: unexpected input in " foo.squared[i] <- foo[i]^2
> }
Error: unexpected '}' in "}"
> 

R 会话的详细信息(我在 RStudio 中运行它):

Details of the R session (I run it in RStudio):

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

太烦人了!!我将不胜感激任何建议!!!谢谢,玛丽亚

It has been very annoying!! I would appreciate any advice!!! Thanks, Maria

更新:

这是我怀疑导致此问题的一开始的代码..它应该采用名称向量并从中提取第二个元素.

Here is the code in the very beginning that I suspect causes this problem..It is supposed to take a vector of names and extract the second element from it.

splitnames <- strsplit(as.character(train$Name),"[,.]")
firstelement <- function(x){x[2]}
sapply(splitnames,firstelement)

在我执行之后,R 表现得很奇怪.虽然我不是 100% 确定.

After I execute it R acts weird. Though I am not 100% sure.

推荐答案

我也遇到了同样的问题,发现是 RStudio 的一个 bug 导致的(代码在 R 和 R-gui 中运行良好,但是在 RStudio 中失败.)很难重现,直到 RStudio 保存状态中的某些内容被破坏,之后行为非常一致.

I have been experiencing the same problem, and have found that it is caused by a bug in RStudio (the code runs fine in R and R-gui, but fails in RStudio.) It is hard to reproduce until something gets corrupted in RStudio's saved state, after which the behaviour is pretty consistent.

删除 ~/.rstudio-desktop 为我解决了这个问题.

Removing ~/.rstudio-desktop fixed the issue for me.

mv ~/.rstudio-desktop ~/rstudio-desktop.old

有关在各种平台上重置 RStudio 状态的更多信息 在这里.

More on resetting RStudio's state on various platforms here.

这篇关于R“for循环"错误消息{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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