R闪亮-cex值不正确-上传文本文件,wordcloud软件包
我刚刚开始学习Shiny,我正在尝试做一个简单的项目,以此来了解它作为开发工具的情况 我的目标:做一个wordcloud应用程序。输入:一个.txt文件。输出:wordcloud 我得到一个“错误的cex值”错误,我猜我的文件没有正确上传。。。我说得对吗?如果是这样,文本文件的read.csv等效于什么?我推测它是read.table,但显然我错了,因为我使用read.table时出错 以下是我的代码,大量改编自: *global.r*R闪亮-cex值不正确-上传文本文件,wordcloud软件包,r,shiny,R,Shiny,我刚刚开始学习Shiny,我正在尝试做一个简单的项目,以此来了解它作为开发工具的情况 我的目标:做一个wordcloud应用程序。输入:一个.txt文件。输出:wordcloud 我得到一个“错误的cex值”错误,我猜我的文件没有正确上传。。。我说得对吗?如果是这样,文本文件的read.csv等效于什么?我推测它是read.table,但显然我错了,因为我使用read.table时出错 以下是我的代码,大量改编自: *global.r* library(tm) library(wordcloud
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)
}
**示例输入文件**
按要求。你可以使用这个.txt(它是莎士比亚):为了让你的应用程序正常运行,需要做一些更改/编辑!处理文件输入的方式完全错误:)。您可以直接将
input$selection
放入gettermmary()
函数中,然后在global.R
中读取文件内容。看一看,了解如何上传一个文件,并在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")
)
)
)
)
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"))
})
})
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)
}
server.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")
)
)
)
)
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"))
})
})
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)
}
您能提供一个示例输入文件吗?是:)@abourbaki使用debug()
函数帮助分析代码: