R函数中无法识别的转义和对象未找到错误

时间:2017-10-10 18:41:51

标签: r function plyr stringr

在尝试自学R时,我跟随以下示例:

https://www.r-bloggers.com/how-to-use-r-to-scrape-tweets-super-tuesday-2016/

在这个例子中,作者Kris Eberwein描述了以下功能:

score.sentiment = function(sentences, good_text, bad_text, .progress='none')
{
    require(plyr)
    require(stringr)
    # we got a vector of sentences. plyr will handle a list
    # or a vector as an "l" for us
    # we want a simple array of scores back, so we use
    # "l" + "a" + "ply" = "laply":
    scores = laply(sentences, function(sentence, good_text, bad_text) {

        # clean up sentences with R's regex-driven global substitute, gsub():
        sentence = gsub('[[:punct:]]', '', sentence)
        sentence = gsub('[[:cntrl:]]', '', sentence)
        sentence = gsub('\d+', '', sentence)
        #to remove emojis
        sentence <- iconv(sentence, 'UTF-8', 'ASCII')
        sentence = tolower(sentence)        
        # split into words. str_split is in the stringr package
        word.list = str_split(sentence, '\s+')
        # sometimes a list() is one level of hierarchy too much
        words = unlist(word.list)

        # compare our words to the dictionaries of positive & negative terms
        pos.matches = match(words, good_text)
        neg.matches = match(words, bad_text)

        # match() returns the position of the matched term or NA
        # we just want a TRUE/FALSE:
        pos.matches = !is.na(pos.matches)
        neg.matches = !is.na(neg.matches)

        # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
        score = sum(pos.matches) - sum(neg.matches)

        return(score)
    }, good_text, bad_text, .progress=.progress )

    scores.df = data.frame(score=scores, text=sentences)
    return(scores.df)
}

然而,当我运行此命令时,我收到以下错误:

> score.sentiment = function(sentences, good_text, bad_text, .progress='none')
+ {
+   require(plyr)
+   require(stringr)
+   # we got a vector of sentences. plyr will handle a list
+   # or a vector as an "l" for us
+   # we want a simple array of scores back, so we use
+   # "l" + "a" + "ply" = "laply":
+   scores = laply(sentences, function(sentence, good_text, bad_text) {
+     
+     # clean up sentences with R's regex-driven global substitute, gsub():
+     sentence = gsub('[[:punct:]]', '', sentence)
+     sentence = gsub('[[:cntrl:]]', '', sentence)
+     sentence = gsub('\d+', '', sentence)
Error: '\d' is an unrecognized escape in character string starting "'\d"
>     #to remove emojis
>     sentence <- iconv(sentence, 'UTF-8', 'ASCII')
Error in iconv(sentence, "UTF-8", "ASCII") : object 'sentence' not found
>     sentence = tolower(sentence)        
Error in tolower(sentence) : object 'sentence' not found
>     # split into words. str_split is in the stringr package
>     word.list = str_split(sentence, '\s+')
Error: '\s' is an unrecognized escape in character string starting "'\s"
>     # sometimes a list() is one level of hierarchy too much
>     words = unlist(word.list)
Error in unlist(word.list) : object 'word.list' not found
>     
>     # compare our words to the dictionaries of positive & negative terms
>     pos.matches = match(words, good_text)
>     neg.matches = match(words, bad_text)
>     
>     # match() returns the position of the matched term or NA
>     # we just want a TRUE/FALSE:
>     pos.matches = !is.na(pos.matches)
>     neg.matches = !is.na(neg.matches)
>     
>     # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
>     score = sum(pos.matches) - sum(neg.matches)
>     
>     return(score)
Error: no function to return from, jumping to top level
>   }, good_text, bad_text, .progress=.progress )
Error: unexpected '}' in "  }"
>   
>   scores.df = data.frame(score=scores, text=sentences)
Error in data.frame(score = scores, text = sentences) : 
  object 'scores' not found
>   return(scores.df)
Error: object 'scores.df' not found
> }
Error: unexpected '}' in "}"

由于此博客帖子的评论已经结束,我以为我会求助于SO。谁能解释我做错了什么;或者,至少,解释这些错误意味着什么?我在R的这个会话中加载了以下包:

library(twitteR)
library(ROAuth)
library(httr)
library(plyr)
library(stringr)

感谢您的帮助!

0 个答案:

没有答案