ropensci-archive / rtweet

🐦 R client for interacting with Twitter's [stream and REST] APIs
https://docs.ropensci.org/rtweet
Other
786 stars 200 forks source link

Shiny app rtweet fail #206

Closed diplodata closed 6 years ago

diplodata commented 6 years ago

I'm trying to build a Shiny app with rtweet and find that what works fine on my local machine fails when uploaded as a Shinyapp (shinyapps.io). A minimal example:

require(shiny)
require(rtweet)

load('token.Rdata')

server = function(input, output, session) {
  u = lookup_users('realdonaldtrump', parse=F)
  output$table <- renderDataTable(u)
}

ui = basicPage(dataTableOutput('table'))

shinyApp(ui = ui, server = server)

This fails when uploaded to shinyapps with the following log:

2018-03-26T14:02:24.464079+00:00 shinyapps[308715]: Server version: 1.5.14-6
2018-03-26T14:02:24.464084+00:00 shinyapps[308715]: LANG: en_GB.UTF-8
2018-03-26T14:02:24.464114+00:00 shinyapps[308715]: R version: 3.4.3
2018-03-26T14:02:24.464116+00:00 shinyapps[308715]: shiny version: 1.0.3
2018-03-26T14:02:24.464117+00:00 shinyapps[308715]: rmarkdown version: NA
2018-03-26T14:02:24.464117+00:00 shinyapps[308715]: knitr version: NA
2018-03-26T14:02:24.464118+00:00 shinyapps[308715]: jsonlite version: 1.5
2018-03-26T14:02:24.464134+00:00 shinyapps[308715]: RJSONIO version: NA
2018-03-26T14:02:24.464136+00:00 shinyapps[308715]: htmltools version: 0.3.6
2018-03-26T14:02:24.676426+00:00 shinyapps[308715]: Using jsonlite for JSON processing
2018-03-26T14:02:24.797865+00:00 shinyapps[308715]: 
2018-03-26T14:02:24.797870+00:00 shinyapps[308715]: Starting R with process ID: '104'
2018-03-26T14:02:24.838799+00:00 shinyapps[308715]: 
2018-03-26T14:02:24.838805+00:00 shinyapps[308715]: Listening on http://127.0.0.1:46615
2018-03-26T14:02:27.032655+00:00 shinyapps[308715]: Warning: Error in fix.by: 'by' must specify a uniquely valid column
2018-03-26T14:02:27.036479+00:00 shinyapps[308715]: Stack trace (innermost first):
2018-03-26T14:02:27.036483+00:00 shinyapps[308715]:     63: fix.by
2018-03-26T14:02:27.036484+00:00 shinyapps[308715]:     62: merge.data.frame
2018-03-26T14:02:27.036484+00:00 shinyapps[308715]:     61: merge
2018-03-26T14:02:27.036485+00:00 shinyapps[308715]:     60: unique
2018-03-26T14:02:27.036485+00:00 shinyapps[308715]:     59: join_rtweet
2018-03-26T14:02:27.036486+00:00 shinyapps[308715]:     58: users_with_tweets
2018-03-26T14:02:27.036486+00:00 shinyapps[308715]:     57: lookup_users_
2018-03-26T14:02:27.036487+00:00 shinyapps[308715]:     55: lookup_users
2018-03-26T14:02:27.036487+00:00 shinyapps[308715]:     56: do.call
2018-03-26T14:02:27.036488+00:00 shinyapps[308715]:     54: server [/srv/connect/apps/test/app.R#10]
2018-03-26T14:02:27.036488+00:00 shinyapps[308715]:     13: runApp
2018-03-26T14:02:27.036489+00:00 shinyapps[308715]:     12: fn
2018-03-26T14:02:27.036489+00:00 shinyapps[308715]:     11: doTryCatch
2018-03-26T14:02:27.036490+00:00 shinyapps[308715]:     10: tryCatchOne
2018-03-26T14:02:27.036490+00:00 shinyapps[308715]:      9: tryCatchList
2018-03-26T14:02:27.036491+00:00 shinyapps[308715]:      8: tryCatch
2018-03-26T14:02:27.036491+00:00 shinyapps[308715]:      7: connect$retry
2018-03-26T14:02:27.036492+00:00 shinyapps[308715]:      6: eval
2018-03-26T14:02:27.036492+00:00 shinyapps[308715]:      5: eval
2018-03-26T14:02:27.036492+00:00 shinyapps[308715]:      4: eval
2018-03-26T14:02:27.036493+00:00 shinyapps[308715]:      3: eval
2018-03-26T14:02:27.036493+00:00 shinyapps[308715]:      2: eval.parent
2018-03-26T14:02:27.036494+00:00 shinyapps[308715]:      1: local
2018-03-26T14:02:27.036724+00:00 shinyapps[308715]: Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

Any idea what is failing?

systats commented 6 years ago

Hi, exact the same happens for Shiny Server. However in a normal Rstudio set up rtweet works well. The package twitteR works for shiny because it does not need browser authentification. But their API is not as tidy as yours :). The problem seems to be that rtweet does not produce a .httr-auth file which might be neccessary for a server only application. A bug fix would be great 👍 Best, Simon

joel23888 commented 6 years ago

@systats can you try what I suggested in https://github.com/mkearney/rtweet/issues/202#issuecomment-380692983? The function createTokenNoBrowser (not part of rtweet so you will need to define it in R) will allow you to authenticate without a browser/callback as in twitteR then use rtweet.

systats commented 6 years ago

Thanks for the hint! It is working perfectly :) Lets close this issue.

lollex90 commented 2 years ago

Hello, I am having the same issue. My shiny app works fine on the local machine but I am unable to launch it on shinyapps.io. A minimal example:

library(shiny)
library(rtweet)

ui <- fluidPage(
    textOutput("tweet")
)

server <- function(input, output) {

  output$tweet <- renderText({
    tweets <- get_timeline(user = "JoeBiden", n = 1)
    tweets$full_text[1]
  })

}

shinyApp(ui = ui, server = server)

I tried running the createTokenNoBrowser function and adding tokens argument to get_timeline but that didn't work (I got 403 Twitter API error). I also tried using rtweet_user(), rtweet_bot(), and rtweet_app() but that didn't work either. Ideally, I would like to run the app on the server without the need to include the keys in the code explicitly for security reasons but I am happy with any solution that just allows the app to run on the server. Any help would be much appreciated!

llrs commented 2 years ago

Hi @lollex90, if you are concerned with security you should check what shinyapps.io has to say on this matters. Please, post questions for shinyapps to the community.rstudio.com.

The current code doesn't authenticate before making any requests. You would need to create or load a token and use it via auth_as before calling get_timeline.

lollex90 commented 2 years ago

Hi @llrs, thank you for your help. From what I understand, I need to include authentication in the code before calling get_timeline. I tried the following:

auth_as(rtweet_app())
tweets <- get_timeline(user = "JoeBiden", n = 1)

Entered my bearer token (I tried regenerating it and entering it multiple times) but then I get a 403 error. However, later if I call :

auth_as()
tweets <- get_timeline(user = "JoeBiden", n = 1)

it works. Not sure if this is relevant but in authentication settings on the developer portal I chose "web App" for the type of app.

llrs commented 2 years ago

Please @lollex90 , let's keep this issue clean and not bother the other 4 people in the issue. Open a clear question in some other forum like https://discuss.ropensci.org/tag/rtweet with a clear explanation of your problem (it is unclear to me if you have problems in shinyapps in your local computer or both) and what you tried so far and a clear explanation of how it failed.