仅在运行整个代码块时出错 [英] Error only when running whole block of code

查看:42
本文介绍了仅在运行整个代码块时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了数据集附带的代码.此代码应该将因子变量转换为数字.当我单独运行每一行时,它工作正常,但如果我尝试突出显示整个部分,则会出现以下错误:

I have code that came with a dataset that I downloaded. This code is supposed to convert factor variables to numeric. When I run each line individually, it works fine, but if I try to highlight a whole section, then I get the following error:

错误:意外输入...

它对每一行代码都给出了这个错误,但如果我单独运行每一行,那么它工作正常.我以前从未遇到过这种情况.这是怎么回事??谢谢!

It gives me this error for every line of code, but again if I run each line individually, then it works fine. I've never run into this before. What's going on?? Thanks!

这是我要运行的代码:

library(prettyR)
lbls <- sort(levels(DF$myVar))
lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls))
DF$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1", DF$myVar))
DF$myVar <- add.value.labels(DF$myVar, lbls)

这是带有错误的输出:

> library(prettyR)
"rror: unexpected input in "library(prettyR)
> lbls <- sort(levels(DF$myVar))
"rror: unexpected input in "lbls <- sort(levels(DF$myVar))
> lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls))
"rror: unexpected input in "lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls))
> surv.df$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1", DF$myVar))
"rror: unexpected input in "DF$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1",DF$myVar))
> surv.df$BATTLEGROUND <- add.value.labels(DF$myVar, lbls)
Error in add.value.labels(surv.df$myVar, lbls) : 
object 'lbls' not found

推荐答案

我想通了(实际上有人告诉我问题出在哪里)

I figured out the issue (actually someone told me what the problem was)

代码是作为 .R 文件下载的,并且必须是使用具有非标准换行"编码的文本编辑器编写的.所以我只是将代码复制到文本编辑器,确实替换了所有以将\n"切换为#####".然后我再次使用 replace all 切换回换行符并将其复制回 R studio.

The code was downloaded as a .R file and must have been written using a text editor with non-standard "new line" coding. So I just copied the code to a text editor, did replace all to switch "\n" to "#####". Then I used replace all again to switch back to new-lines and copied it back into R studio.

一切正常!

这篇关于仅在运行整个代码块时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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