R Shiny - cex值错误 - 上传文本文件,wordcloud包

4
我刚开始学习Shiny,并尝试做一个简单的项目,以了解它作为开发工具的感觉。
我的目标:做一个词云应用程序。输入:一个.txt文件。输出:一个词云。
我得到了一个"不正确的cex值"错误,我的猜测是我的文件没有正确上传... 我是正确的吗?如果是这样,那么对于文本文件来说,read.csv有什么等效的替代方法?我已经收集到了read.table,但显然我是错的,因为我在使用read.table时出现了错误。
这是我的代码,大幅改编自WordCloud
* global.r *
library(tm)
library(wordcloud)
library(memoise)

# Using "memoise" to automatically cache the results
getTermMatrix <- function(text) {
    # Careful not to let just any name slip in here; a
    # malicious user could manipulate this value.

    myCorpus = Corpus(VectorSource(text))
    myCorpus = tm_map(myCorpus, content_transformer(tolower))
    myCorpus = tm_map(myCorpus, removePunctuation)
    myCorpus = tm_map(myCorpus, removeNumbers)
    myCorpus = tm_map(myCorpus, removeWords,
    c(stopwords("SMART"), "thy", "thou", "thee", "the", "and", "but"))

    myDTM = TermDocumentMatrix(myCorpus,
    control = list(minWordLength = 1))

    m = as.matrix(myDTM)

    sort(rowSums(m), decreasing = TRUE)
}

server.r

function(input, output, session) {
    # Define a reactive expression for the document term matrix

    my_data <- reactive({
        inFile <- input$files
        if (is.null(inFile))
        return(NULL)
        data <- read.table(inFile, header=T, sep="\t", fileEncoding="UTF-8")
        data
    })

    terms <- reactive({
        # Change when the "update" button is pressed...

        input$update
        # ...but not for anything else
        isolate({
            withProgress({
                setProgress(message = "Processing corpus...")
                getTermMatrix(input$inFile)
            })
        })
    })

    # Make the wordcloud drawing predictable during a session
    wordcloud_rep <- repeatable(wordcloud)

    output$plot <- renderPlot({
        v <- terms()
        wordcloud_rep(names(v), v, scale=c(4,0.5),
        min.freq = input$freq, max.words=input$max,
        colors=brewer.pal(8, "Dark2"))
    })
}

ui.r

fluidPage(
# Application title
titlePanel("Word Cloud"),

sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
#######
fileInput("selection", "Choose a text:"),


#
actionButton("update", "Change"),
hr(),
sliderInput("freq",
"Minimum Frequency:",
min = 1,  max = 50, value = 15),
sliderInput("max",
"Maximum Number of Words:",
min = 1,  max = 300,  value = 100)
),

# Show Word Cloud
mainPanel(
plotOutput("plot")
)
)
)

**样例输入文件 **

如您所要求的。您可以使用这个.txt文件(莎士比亚的作品):http://www.gutenberg.org/cache/epub/2242/pg2242.txt


你能提供一个样本输入文件吗? - Shiva
http://www.gutenberg.org/cache/epub/2242/pg2242.txt - abourbaki
1
@abourbaki 使用debug()函数来协助分析代码:https://stat.ethz.ch/R-manual/R-devel/library/base/html/debug.html - CinchBlue
1个回答

2
您需要对应用程序进行一些更改/编辑才能使其正常工作!您处理文件输入的方式完全错误:)。 您可以直接将input $ selection 放入getTermMatrix()函数中,然后在global.R 中读取文件内容。请查看此处,了解如何在Shiny中上传文件并读取其内容。
错误是因为没有读取文件,因此没有数据可供输入Corpus()函数。 在以下代码中,由于启动应用程序时没有文件输入,因此显示错误消息指出未读取文件。但是,在上传文件后,错误消息消失并显示语料库。为了不显示错误,请在ui.R中包含一个小的tags()。也许您可以找到更好的解决方法。
请查看以下工作代码,并尝试将其扩展到您未来的目的。 ui.R
shinyUI(
fluidPage(
  # Application title
  titlePanel("Word Cloud"),
  tags$style(type="text/css",
             ".shiny-output-error { visibility: hidden; }",
             ".shiny-output-error:before { visibility: hidden; }"
  ),

  sidebarLayout(
    # Sidebar with a slider and selection inputs
    sidebarPanel(
      #######
      fileInput("selection", "Choose a text:"),



      actionButton("update", "Change"),
      hr(),
      sliderInput("freq",
                  "Minimum Frequency:",
                  min = 1,  max = 50, value = 15),
      sliderInput("max",
                  "Maximum Number of Words:",
                  min = 1,  max = 300,  value = 100)
    ),

    # Show Word Cloud
    mainPanel(
      plotOutput("plot")
    )
  )
)
)

server.R

library(shiny)

shinyServer(function(input, output, session) {
  # Define a reactive expression for the document term matrix

  terms <- reactive({
    # Change when the "update" button is pressed...

    input$update

    # ...but not for anything else
    isolate({
      withProgress({
        setProgress(message = "Processing corpus...")
        getTermMatrix(input$selection)
      })
    })
  })

  # Make the wordcloud drawing predictable during a session
  wordcloud_rep <- repeatable(wordcloud)

  output$plot <- renderPlot({
    v <- terms()
    wordcloud_rep(names(v), v, scale=c(4,0.5),
                  min.freq = input$freq, max.words=input$max,
                  colors=brewer.pal(8, "Dark2"))
  })
})

global.R

library(tm)
library(wordcloud)
library(memoise)

# Using "memoise" to automatically cache the results
getTermMatrix <- function(f) {
  # Careful not to let just any name slip in here; a
  # malicious user could manipulate this value.

  text <- readLines(f$datapath,encoding = "UTF-8")

  myCorpus = Corpus(VectorSource(text))

  myCorpus = tm_map(myCorpus, content_transformer(tolower))
  myCorpus = tm_map(myCorpus, removePunctuation)
  myCorpus = tm_map(myCorpus, removeNumbers)
  myCorpus = tm_map(myCorpus, removeWords,
                    c(stopwords("SMART"), "thy", "thou", "thee", "the", "and", "but"))

  myDTM = TermDocumentMatrix(myCorpus,
                             control = list(minWordLength = 1,wordLengths=c(0,Inf)))

  m = as.matrix(myDTM)

  sort(rowSums(m), decreasing = TRUE)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接