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

我刚开始学习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

最佳答案 要使应用正常运行,需要进行一些更改/修改!你处理文件输入的方式是完全错误的:).您可以直接在getTermMatrix()函数中输入input $selection,然后读取global.R中的文件内容.看一下
this,了解如何上传文件并在Shiny中阅读其内容.

该错误是因为没有文件读取,因此没有数据被输入Corpus()函数.在以下代码中,由于启动应用程序时没有文件输入,因此显示错误,表明没有文件被读取.但是,在上传文件后,错误消失并显示语料库.为了不显示错误,我在ui.R中包含了一个小标签().也许你可以找到一个更好的解决方案.

查看以下工作代码并尝试将其扩展到您的未来目的.

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)
}
点赞