gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:40:15 25 4
gpt4 key购买 nike

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

我的目标:做一个 wordcloud 应用程序。输入:一个 .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

最佳答案

需要进行一些更改/编辑才能使您的应用程序正常运行!您处理文件输入的方式完全错误:)。可以直接放input$selectiongetTermMatrix()函数,然后读取 global.R 中的文件内容.看看this了解如何在 Shiny 中上传文件并阅读其内容。

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

查看以下工作代码并尝试将其扩展到您的 future 用途。

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

关于R Shiny - cex 值不正确 - 上传文本文件,wordcloud 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31414464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com