[英]selectinput in Shiny R
我无法获得选择输入的值,我需要生成一个词云,但是云的内容将取决于所选选项。
该应用程序正在无限加载。 请帮帮我。
UI.R
fluidPage(
# Application title
titlePanel("Word Cloud"),
sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
selectInput("selection", "Choose a book:",
choices = c(coletor1,coletor2),
selected = coletor1),
actionButton("update", "Change"),
hr(),
sliderInput("freq",
"Minimum Frequency:",
min = 1, max = 1000, value = 1),
sliderInput("max",
"Maximum Number of Words:",
min = 1, max = 100, value = 100)
),
# Show Word Cloud
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot"))
)
)))
Server.R
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)
library(RPostgreSQL)
con <- dbConnect(PostgreSQL(), user="postgres", password="123456",dbname="postgres")
coletor1=dbGetQuery(con,"SELECT REPLACE(aux_coletprinc,' ','') aux_coletprinc from jabot.detacesso2 where aux_coletprinc ilike '%forz%a%'
")
coletor2=dbGetQuery(con,"SELECT REPLACE(aux_coletprinc,' ','') aux_coletprinc from jabot.detacesso2 where aux_coletprinc ilike '%vian%a%'
")
dbDisconnect(con)
list<-c(coletor1,coletor2)
# Using "memoise" to automatically cache the results
getTermMatrix <- memoise(function(list) {
text <- list
myCorpus = Corpus(VectorSource(text))
myCorpus = tm_map(myCorpus, content_transformer(tolower))
myDTM = TermDocumentMatrix(myCorpus,
control = list(minWordLength = 1))
m = as.matrix(myDTM)
sort(rowSums(m), decreasing = TRUE)
})
global.R
在server.R
和ui.R
之前运行。 因此,尚无input$selection
。
您只需要在global.R
定义 getTermMatrix
函数global.R
。 有没有需要定义a
也没有text
是要被读取。
您可以在input
列表已存在的server.R
进行这两个操作。 总体来说,请更好地研究getTermMatrix()
函数,从不使用book
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.