我正在研究一个历史日记的主题建模。我在我的电脑上运行了代码,所有内容都已加载并且工作正常。但是,当我的学生在Mac笔记本电脑上运行完全相同的代码时,在我发布的最后一行代码后会出现以下错误:无效的多字节字符串1。
inputDir <- "working directory"
files.v <- dir(path=inputDir, pattern="*\\.txt")
chunk.size <- 50
makeFlexTextChunks <- function(inputDir, file.name, chunk.size=50){
text.file.path<-file.path(inputDir, file.name)
text.lines.v <- scan(text.file.path, what = "character", sep = "\n")
journal.v<- paste(text.lines.v, collapse = " ")
journal.lower.v <- tolower(journal.v)
journal.lower.l <- strsplit(journal.lower.v, "\\W")
journal.word.v <- unlist(journal.lower.l)
not.blanks.v <- which(journal.word.v!="")
journal.word.v <- journal.word.v[not.blanks.v]
x <- seq_along(journal.word.v)
chunks.l <-split(journal.word.v, ceiling(x/chunk.size))
if(length(chunks.l[length(chunks.l)])<=chunk.size/2){
chunks.l[[length(chunks.l)-1]]<- c(chunks.l[[length(chunks.l)-1]],
chunks.l[[length(chunks.l)]])
chunks.l[[length(chunks.l)]] <- NULL
}
chunks.l <- lapply(chunks.l, paste, collapse=" ")
chunks.df <- do.call(rbind, chunks.l)
return(chunks.df)
}
topic.l <- NULL
for(i in 1:length(files.v)){
chunk.m <-makeFlexTextChunks(inputDir, files.v[i], chunk.size)
textname <- gsub("\\..*", "", files.v[i])
segments.m <- cbind(paste(textname, segment=1:nrow(chunk.m), sep = "_"),
chunk.m)
topic.l[[textname]] <- c("id", "text")
}
我检查并仔细检查了我的txt文件。它们都是UTF-8,文件中没有异常字符。为什么代码在我的电脑上运行,而不是在Mac上运行?