我以前从未使用过 Shiny,所以如果这是一个非常愚蠢的问题,我深表歉意。我正在尝试制作一个闪亮的应用程序,您可以在其中输入将从 Twitter 中提取的搜索词并创建一个词云。我觉得我几乎在那里,但它不起作用,老实说,我真的不知道我在做什么。我正在尝试自学 Shiny,但我也知道像这样的论坛对学习非常有用。
library(tm)
library(wordcloud)
library(memoise)
ui <- fluidPage(
# Application title
titlePanel("Word Cloud"),
sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
textInput("selection", "Input your search term:",
""),
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")
)
)
)
#Define server logic
server <- function(input, output, session) {
# Define a reactive expression for the document term matrix
terms <- reactive({
consumer_key <- "XXXX"
consumer_secret <- "XXXX"
access_token <- "XXXX"
access_secret <- "XXXX"
#Here we are creating the "handshake" with Twitter
setup_twitter_oauth(consumer_key= consumer_key, consumer_secret=
consumer_secret,access_token= access_token, access_secret= access_secret)
#Once you have created your handshake, you can start searching for tweets
#Note that if you select a common term like "Atlanta" you will generate a lot
of Tweets quickly
#But if you select an esoteric term like "heteroscedasticity", it might take
a while to get any
tw<-searchTwitter("selection", n=1000, lang='en', resultType = "recent")
# Using "memoise" to automatically cache the results
getTermMatrix <- memoise(function(tw) {
text <- readLines(sprintf(tw),
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))
m = as.matrix(myDTM)
sort(rowSums(m), decreasing = TRUE)
})
# 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 <- reactive({
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"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
感谢任何试图提供帮助的人!!
编辑:啊,对不起,没有澄清什么是错的!到目前为止,它打开了一个包含我想要的所有输入框的应用程序,但输入搜索词似乎并没有真正做任何事情。它只是永远加载。没有错误。
