R Shiny - 不正确的cex值 - 上传文本文件,wordcloud包

时间:2015-07-14 18:26:49

标签: r shiny

我刚开始学习Shiny,我正在尝试做一个简单的项目,以此来了解它是如何作为开发工具。

我的目标:做一个wordcloud应用程序。输入:.txt文件。输出:wordcloud。

我得到一个"不正确的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

1 个答案:

答案 0 :(得分:2)

要使您的应用正常运行,需要进行一些更改/修改!你处理文件输入的方式是完全错误的:)。您可以直接将input$selection放入getTermMatrix()函数中,然后阅读global.R中的文件内容。查看this以了解如何上传文件并在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)
}