【问题标题】:Twitter analysis Shiny AppTwitter 分析 Shiny App
【发布时间】:2015-08-06 22:35:06
【问题描述】:

我想创建一个基本的闪亮应用程序,其中我可以在文本输入框中键入关键字,当我单击提交时,输出应该是最近推文的数据表,其中在文本输入框中键入了关键字。我还需要找到一种方法来使用setup_twitter_oauth 自动启用我的应用程序和推特之间的握手。我创建了以下app.R 文件

library(shiny)
library(twitteR)


ui <- fluidPage(
  titlePanel("Basic Twitter Search App"),
  textInput("twitter", "Search Keyword"),
  actionButton("click", label = "Search Tweets"),
  dataTableOutput("table")
)

server <- function(input, output){
  source(file = 'oauth.RData') #file containing the credentials
  output$table <- renderDataTable
    (
      {
    observeEvent(input$twitter, {searchTwitter(input$twitter, n=1500)
    })

  })
}

shinyApp(ui = ui, server = server)

但是当我运行代码(运行应用程序)时,出现以下错误:

原始错误(名称 = 名称,闪亮会话 = 自我): 未使用的参数(名称 = 名称,闪亮会话 = 自我) 警告:观察者中未处理的错误:客户端错误:(400)错误请求 观察事件(输入$twitter)

【问题讨论】:

    标签: twitter twitter-oauth shiny


    【解决方案1】:

    谢谢@Jimbo。经过多次失败的实验,以下代码起作用了:

    library(shiny)
    
    ui <- fluidPage(
      textInput("handle", "Search Tweets:"),
      sliderInput("maxTweets","Number of recent tweets to use for analysis:",min=5,max=1500, value = 5),
      downloadButton("download", "Download File"),
      dataTableOutput("table")
    )
    
    
    server <- function(input, output) {
    
    
      library(twitteR)
    
      consumerKey = "My key"   
      consumerSecret = "My secret"
      accessToken = "My token"
      accessSecret = "My secret"
      my_oauth <- setup_twitter_oauth(consumer_key = consumerKey, consumer_secret = consumerSecret,
                                      access_token = accessToken, access_secret = accessSecret)
    
      output$table <- renderDataTable({
        TweetFrame<-function(searchTerm, maxTweets)
        {
          twtList<-searchTwitter(searchTerm,n=maxTweets)
          twtList1<- do.call("rbind",lapply(twtList,as.data.frame))
          twtList1$text<-iconv(twtList1$text, 'UTF-8', 'ASCII') #WILL THIS SOLVE THE UTF ENCODING PROBLEM: http://lists.hexdump.org/pipermail/twitter-users-hexdump.org/2013-May/000335.html
          return(twtList1)
    
        }
        entity1<-reactive({entity1<-TweetFrame(input$handle, input$maxTweets)})
        output$table <- renderDataTable({tab<-entity1()[1]})
        output$download <- downloadHandler(filename = function() {paste(input$handle, '.csv', sep='')},
                                           content = function(file){
                                             write.csv(entity1(), file)
                                           }
                                           )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    虽然我仍然无法弄清楚如何自动启用用户身份验证(无需用户干预)。在这方面的任何帮助将不胜感激。

    【讨论】:

    • 请参阅此stackoverflow.com/questions/31713119/… 在答案中,Scott Chamberlain 提出了一种替代方法来实现您想要实现的目标。认为您可以在上述方法的基础上进行构建。
    【解决方案2】:
    server <- function(input, output){
      source(file = 'oauth.RData') #file containing the credentials
      output$table <- renderDataTable({
        test <- searchTwitter(input$twitter, n=1500)
        return(test)
      })
    }
    

    只要 searchTwitter 返回一个 df 或一个矩阵,这应该可以工作

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 2019-12-22
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多