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

R Shiny - Incorrect cex value - uploading text file, wordcloud package

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

我的目标:做一个wordcloud app。输入:.txt 文件。输出:一个词云。

我收到 "incorrect cex value" 错误,我猜是我的文件没有正确上传...我说得对吗?如果是这样,什么相当于文本文件的 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

要使您的应用正常运行,还需要完成一些 changes/edits!您处理文件输入的方式是完全错误的:)。可以直接把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)
}