object [seq_len(ile)]中的错误:“符号"类型的对象不可子集化 [英] Error in object[seq_len(ile)] : object of type 'symbol' is not subsettable

查看:68
本文介绍了object [seq_len(ile)]中的错误:“符号"类型的对象不可子集化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用渲染语句将rmd文件转换为pdf.

I am trying to convert an rmd file to pdf using render statement.

render("MiningReport.Rmd", "pdf_document",output_dir = "C:/ProjectSocial/Reports/Twitter/Maybelline")

我收到如下错误

Quitting from lines 109-113 (MiningReport.Rmd) 
Error in object[seq_len(ile)] : 
  object of type 'symbol' is not subsettable

这对我来说似乎很奇怪,因为当我编织Rmd文件时就没有这样的错误,并且pdf报告已成功生成,而当我尝试使用render语句执行此操作时,它给出了错误.任何人都可以解释发生了什么事吗?以下是错误蔓延到

This looks very strange to me because when I knit the Rmd file then is no such error and the pdf report is generated successfully and when I try to do the same using render statement it gives error. Can anyone please explain what's going on ? Below is the code chunk where the error creeps in

```{r assoc ,echo=F,message=FALSE}
library(tm)
findAssocs(myTdm,df$term[1:10],0.5)
```

当我删除上面的代码块时,下一个代码块会发生相同的错误.以下是我的Rmd文件.我正在读取存储在指定目录中文件中的推文.

When I remove the above chunk then the same error occurs for the next chunk of code. Below is my Rmd file. I am reading in tweets stored in files present in the directory specified.

```{r computedate,echo = FALSE}
date1 <-format(Sys.Date() - 7,"%B %d")
date2 <-format(Sys.Date() - 1,"%B %d, %Y")

```

# This report has been created on twitter data from `r date1` to `r date2`.
# Analysis of Tweets
## Below we can see the most frequent words.

```{r frequent,echo=FALSE,message=FALSE,warning=FALSE,cache=TRUE}

setwd("C:/ProjectSocial/Data/TwitterData/Maybelline")

library(devtools)
library(twitteR)
library(tm)
library(ggplot2)
library(graph)
library(Rgraphviz)
library(wordcloud)
library(topicmodels)
library(data.table)
library(fpc)
library(igraph)
library(xlsx)
library(stringr)

tweets.df<-data.frame(text=character(),favorited=character(),favoriteCount=numeric(),replyToSN=character(),
                created=as.POSIXct(character()),truncated=character(),replyToSID=character(),id=character(),replyToUID=character(),statusSource=character(),screenName=character(),retweetCount=numeric(),
isRetweet=character(),retweeted=character(),longitude=character(),latitude=character(),stringsAsFactors =F)
i<-1
while(i<=7){
  since<-Sys.Date()-i
  file<-read.xlsx2(file=paste("Maybelline",since,".xlsx",sep=""), 1,colClasses = c(rep("character",2),
  "numeric","character","POSIXct",rep("character",6),"numeric",rep("character",4)),   stringsAsFactors=F)

  tweets.df<-rbind(tweets.df,file)
  i<-i+1
}

j<-1
HashTagsList<-c()
HashTags<-str_extract_all(tweets.df$text,"#\\S+")
HashTags<-HashTags[!HashTags %in% c("character(0)")]

while (j<=length(HashTags)){

  HashTagsList<-c(HashTagsList,HashTags[[j]])
  j<-j+1
}
HashTagsList<- gsub("#", "", HashTagsList) 
HashTagsList<-unique(HashTagsList)
HashTagsList<-gsub("[^[:alnum:] ]", "", HashTagsList)

k<-1
HandleTagsList<-c()
HandleTags<-str_extract_all(tweets.df$text,"@\\S+")
HandleTags<-HandleTags[!HandleTags %in% c("character(0)")]
while (k<=length(HandleTags)){

  HandleTagsList<-c(HandleTagsList,HandleTags[[k]])
  k<-k+1
}

HandleTagsList<- gsub("@", "", HandleTagsList) 
HandleTagsList<-unique(HandleTagsList)
HandleTagsList<-gsub("[^[:alnum:] ]", "", HandleTagsList)

tweets.df$text<-gsub("#\\S+", "", tweets.df$text)
tweets.df$text<-gsub("@\\S+", "", tweets.df$text)

Tweets.df<-subset(tweets.df,isRetweet=="FALSE")
Tweets.df$text<-gsub("[^[:alpha:] ]", " ", Tweets.df$text)
Tweets.df$text<-tolower(Tweets.df$text)

myCorpus <-Corpus(VectorSource(Tweets.df$text))
myStopwords<-c(stopwords("english"),"maybelline","https","like","bring","make","thought","please","maybe",
               "know","just","want","wearing","really","last","better","best","first")
myCorpus<-tm_map(myCorpus,removeWords,myStopwords)
myCorpus<-tm_map(myCorpus,removeWords,HashTagsList)
myCorpus<-tm_map(myCorpus,removeWords,HandleTagsList)

myCorpus <- tm_map(myCorpus, PlainTextDocument)
myTdm<-TermDocumentMatrix(myCorpus,control=list(wordLengths=c(4,13)))
freq.Terms<- findFreqTerms(myTdm,lowfreq=20)
termFrequency <- rowSums(as.matrix(myTdm))
termFrequency <- subset(termFrequency, termFrequency>=20)
df <- data.frame(term=names(termFrequency), freq=termFrequency,stringsAsFactors = F)
df <- df[order(-df$freq),] 
rownames(df) <- NULL
print(head(df,50), row.names = FALSE)
df<-head(df,40)
ggplot(df,aes(x=term,y=freq)) + geom_bar(stat="identity") + xlab("Terms") +ylab("Count") +coord_flip()

```

## Below we can find all the words which are associated with the top 10 most frequent words and having correlation > 0.5.

```{r assoc ,echo=F,message=FALSE}

library(tm)
findAssocs(myTdm,df$term[1:10],0.5)

```

任何帮助表示赞赏 谢谢

Any help appreciated Thanks

推荐答案

由于我使用echo = F而不是echo = FALSE,因此出现了错误. F或T被视为符号,因此会产生问题. 这就是为什么F(或T)是符号的原因(请参阅?is.symbol以了解什么是符号):

The error was coming because I used echo=F instead of echo=FALSE. F or T are considered as symbols and hence it creates issue. Here is why F (or T) is a symbol (see ?is.symbol to know what a symbol is):

> str(alist(warning = F)) 
List of 1 $ warning: symbol F > str(alist(warning = FALSE)) List of 1 $ warning: logi FALSE 

这篇关于object [seq_len(ile)]中的错误:“符号"类型的对象不可子集化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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