Twitter中的Shiny App - 输入文字挑战

时间:2017-10-11 18:35:02

标签: r shiny

我是新来的闪亮和R。

但是为了通过尝试学习,我尝试使用示例here阅读Twitter API并结合 Shiny 。但是,我无法使 textIinput $ text 无效,因此当用户在闪亮的应用中输入文字时,会生成输出$ distPlot

我试过这个

aga = reactive ({searchTwitter(input$text, n=3200, lang = "en")})

但它永远不会奏效。

我还在stackoverflow.com上查看了这些其他示例 - 12

这是我的代码,我期待着您的帮助和建议。

server.R

library(shiny)
library(twitteR)
library(ROAuth)
library(dplyr)




# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  ##Retrieve tweets from Twitter

  ## Twitter authentication
  #Connecting to Twitter
  consumer_key <- "xxx"
  consumer_secret <-"xxx"
  access_token <- "xxx"
  access_secret <- "xxx"
  setup_twitter_oauth(consumer_key, consumer_secret, access_token,
                      access_secret)

 **# this does not work**

  #aga = reactive ({searchTwitter(input$text, n=3200, lang = "en")}) 

**#this works**
  aga = searchTwitter("@bhp", n=3200, lang = "en") 


  # convert tweets to a data frame
  agadf <- twListToDF(aga) 


  agadf$created<-as.Date(agadf$created)




  #Sentiment Analysis
  require(devtools)
  #install_github("sentiment140", "okugami79", force= TRUE)
  library(sentiment)
  agasentiments <- sentiment(agadf$text)
  table(agasentiments$polarity)

  # sentiment plot
  agasentiments$score <- 0
  agasentiments$score[agasentiments$polarity == "positive"] <- 1
  agasentiments$score[agasentiments$polarity == "negative"] <- -1
  agasentiments$date <- as.Date(agadf$created)
  agaresult <- aggregate(score ~ date, data = agasentiments, sum)
  plot(agaresult, type = "l")


  output$distPlot <- renderPlot({
    plot(agaresult, type = "l")

  })

})

ui.R

library(shiny)
library(twitteR)
library(ROAuth)
library(dplyr)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Real Time Miner Sentiment"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(

        textInput("text", "Text:", "text here"),
        submitButton("Submit")
    ),

    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))

@ zx8754 - 想知道你是否可以提供帮助。我在这个例子here

中努力复制

0 个答案:

没有答案