我正致力于创建一个Shiny应用程序来对简历进行文本分析。在R Studio中一切正常,但是当我尝试运行Shiny应用程序时,我得到一个错误"下标超出范围"我的合格函数(输出)。
在R Studio中,我运行此代码,它运行正常(您必须插入一个类似于1的.txt文件的文件夹才能实际运行):
cname <- file.path("insert any file of .txt documents")
dir(cname)
length(dir(cname))
library(tm)
docs <- Corpus(DirSource(cname))
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/|@|\\|")
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, removeWords, stopwords ("english"))
docs <- tm_map(docs, removeNumbers)
dtm <- DocumentTermMatrix(docs)
freq <- colSums(as.matrix(dtm))
length(freq)
list<-DocumentTermMatrix(docs,list(dictionary = c("python", "machine")))
relist=as.data.frame(as.matrix(list))
machine = ifelse(relist$machine > 0, 1,0)
python = ifelse(relist$python > 0, 1,0)
as.numeric(machine)
as.numeric(python)
newlist=cbind(machine, python)
totals=rowSums(newlist)
docname=dir(cname)
wordtotals=cbind(docname, totals)
qualified=wordtotals[wordtotals[,2]>=2,]
在R Shiny中,我有以下代码:
## ui.R
shinyUI(fluidPage(
titlePanel("Resume Text Analysis"),
sidebarLayout(position = "right",
mainPanel(h2("Qualified Applicants"), dataTableOutput("table")),
sidebarPanel(h2("Specifications"),
textInput("filepath", label = h4("Paste the file path for the folder of '.txt' files you would like included in the analysis.")),
helpText("Choose up to 10 words that a qualified applicant should have in their resume. These can be skills, programming languages, etc. Please put '' '' on either side of each word."),
textInput("word1", label = h3("Term 1"),
value = ""),
textInput("word2", label = h3("Term 2"),
value = ""),
helpText("A qualified applicant will have a resume with at least ___ of the terms above."),
numericInput("morethan",
label = h3("Number of terms required:"),
min = 1, max = 2, value = 1)
)
)))
## server.R
library(tm)
shinyServer(
function(input, output) {
observeEvent(input$filepath,{
if(is.null(input$filepath) || nchar(input$filepath) ==0) return(NULL)
cname <- file.path(input$filepath)
dir(cname)
length(dir(cname))
docs <- Corpus(DirSource(cname))
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/|@|\\|")
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, removeWords, stopwords ("english"))
docs <- tm_map(docs, removeNumbers)
one = input$word1
two = input$word2
list<-DocumentTermMatrix(docs,list(dictionary = c(one, two)))
relist=as.data.frame(as.matrix(list))
one = ifelse(relist$one > 0, 1,0)
two = ifelse(relist$two > 0, 1,0)
as.numeric(one)
as.numeric(two)
newlist=cbind(one, two)
totals=rowSums(newlist)
docname=dir(cname)
wordtotals=cbind(docname, totals)
num = input$morethan
as.numeric(num)
output$table <- renderDataTable({
wordtotals[wordtotals[,2]>=num,]
})
})
}
)
这是R Shiny中的错误(因为代码在R Studio中运行正常)或者我在编码中遗漏了什么?
由于
答案 0 :(得分:0)
重做你的服务器。如上所述,你的问题来自尝试使用$
运算符访问datafram中的列,df$str
等于df["str"]
其中str是实际的列名而不是变量。试试这个,这对你有用。
shinyServer(function(input, output) {
observe({
# Check path input
if(is.null(input$filepath) || nchar(input$filepath) == 0) return(NULL)
# Check if valid
if(!dir.exists(input$filepath)) return(NULL)
output$table1 <- renderTable({
as.data.frame(qualified)
})
cname <- input$filepath
docs <- Corpus(DirSource(cname))
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/|@|\\|")
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, removeWords, stopwords ("english"))
docs <- tm_map(docs, removeNumbers)
dtm <- DocumentTermMatrix(docs)
d <- c( input$word1, input$word2, input$word3, input$word4, input$word5,
input$word6, input$word7, input$word8, input$word9, input$word10)
list <- DocumentTermMatrix(docs,list(dictionary = d))
relist <- as.data.frame(as.matrix(list))
res <- do.call(cbind,lapply(names(relist),function(n){ ifelse(relist[n] > 0, 1,0)}))
totals <- rowSums(res, na.rm=TRUE)
docname=dir(cname)
wordtotals=cbind(docname, totals)
num=input$morethan
df <- data.frame("document"=docname,"total"=totals)
output$table1 <- renderTable({
df[df$total >= as.numeric(num), ]
})
})
}
)