gpt4 book ai didi

r - 在 Shiny R 中选择输入

转载 作者:行者123 更新时间:2023-11-29 12:57:00 26 4
gpt4 key购买 nike

无法获取select input的值,需要生成词云,但是词云的内容会根据选择的option而定。

应用程序正在无限加载。请帮助我。

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

)

) ) )

服务器.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"))
})
}

全局.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.Rserver.Rui.R 之前运行。因此,还没有 input$selection

您只需在 global.R定义 getTermMatrix 函数。无需定义 a 或要读取的 text

您可以在 server.R 中执行这两项操作,其中 input 列表已经存在。虽然,仔细查看您的 getTermMatrix() 函数,从未使用过 book 参数。

关于r - 在 Shiny R 中选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618309/

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