【问题标题】:Error "Subscript out of bounds" in Shiny, but not in R?Shiny中的错误“下标越界”,但在R中却没有?
【发布时间】:2016-01-31 01:47:28
【问题描述】:

我正在创建一个闪亮的应用程序来对简历执行文本分析。在 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 中运行良好)还是我在编码中遗漏了什么?

谢谢

【问题讨论】:

  • 加入一些browser() 调用,看看你的对象R里面有什么闪亮的。

标签: r datatable dataframe shiny render


【解决方案1】:

像这样重做您的 server.R,您的问题来自尝试使用 $ 运算符访问数据帧中的列,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), ]
    })

  })
}
)

【讨论】:

  • 这似乎已经部分解决了!现在,当我运行代码时,我不再收到错误消息,但我仍然没有得到输出。 RECV {"method":"update","data":{"filepath":"我的 .txt 文件文件夹,"word1":"'r'","word2":"'python'"}} RECV { "方法":"update","data":{"word3":"'java'","word4":"'pig'","word5":"'hive'","word6":"'shiny '","word7":"'machine'"}} RECV {"method":"update","data":{"word8":"'learning'","word9":"'studio'"," word10":"'mining'","morethan:shiny.number":4}} 似乎输出没有被送回。有什么想法吗?
  • 我回到“wordtotals[,2] 中的错误:下标超出范围”我将尝试缩小我的示例。您将如何包括错误检查?我找到了一些代码,可以告诉我服务器正在发送/接收的内容,但这就是我所遇到的。
  • 我不确定,但我认为这个错误来自wordtotals=cbind(docname, totals) 部分,因为没有总数,因此矩阵没有第二列。
  • 我认为可能是我设置了闪亮的应用程序,以便用户将他们的术语(word1,word2)输入为“python”或“机器”(因为它们需要在行 list
  • 我现在看到了你的问题,它来自对 data.frame 的索引,我更新了我的答案来解决这个问题。此外,我将“observeEvent”更改为观察,以便在任何输入更改后执行代码(对不起,漫长的一天)
猜你喜欢
  • 2015-04-03
  • 2014-06-19
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多