0

I am having the following issue. I want to publish a shiny app that uses rtweet package. I managed to run the app on my local machine but I get an error message "Error detecting locale: Error in read.table(file = file, header = header, sep = sep, quote = quote: incomplete final line found by 'readTableHeader' in 'raw'" when I try to publish it. 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 from here as suggested elsewhere 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 (as suggested here) but I am happy with any solution that just allows the app to run on the server. Any help would be much appreciated!

Kuba
  • 13
  • 4
  • How are you serving your shiny app? If it's via shiny-server, shinyapps.io, or Connect (formerly RStudio Connect), then add your API key in an environment variable, then retrieve it in the app with `Sys.getenv(...)`. This is the best way (at the moment) for making "secrets" available to your shiny app when you don't want them in the code itself. – r2evans Sep 06 '22 at 16:25
  • I use shinyapps.io, thank you for your help. However, I am still unsure how to successfully deploy an app on the server, even with the keys inside. I tried setting auth_as(auth = rtweet_user("my_key", "my_secret")) at the beginning of the script but that gave me an error (HTTP 403). – Kuba Sep 06 '22 at 16:48
  • You're saying that the code you use locally - with your apikey hard-coded - does not work the same on shinyapps with the apikey still hard-coded? – r2evans Sep 06 '22 at 18:26
  • I did some additional checks and it seems it's a more general issue. If I just run ```run_timeline(...)``` it works fine but if I first run ```rtweet_bot()``` or ```rtweet_app()```, provide my keys and then run ```run_timeline(...)``` it does not work. I am not even sure how to successfully hard-code the keys in the first place. – Kuba Sep 06 '22 at 19:24
  • Hard-coding your key is bad practice in general, but it just `do_something(..., apikey="mysecretpassphrase")`, nothing fancy. To use it more securely, use env-vars in the deployed app, one method is via https://stackoverflow.com/a/39680458/3358272 – r2evans Sep 06 '22 at 20:14
  • Unfortunately, adding ```apikey="mysecretpassphrase"``` as an argument to ```get_timeline()``` doesn't solve it. – Kuba Sep 06 '22 at 21:04
  • Okay, I apologize, I didn't mean that literal text, I'm trying to be general. By that last comment, translate `do_something` to be the function you're calling, perhaps `get_timeline`; translate `apikey="mysecretpassphrase"` to be **whatever argument the function requires** and then the literal text of your passphrase. Nothing fancy here. – r2evans Sep 06 '22 at 21:13
  • Sorry for my mix-up, I am quite new to this. What you're saying is that the api key should be somehow included in the function I call. However, the get_timeline() function does not require the api key nor am I able to add it as an additional argument. From what I see in the function description, it seems to me that I should sepcify ```token = ...``` argument or change the authentication method using ```auth_as()``` and one of ```rtweet_user(), rtweet_app(), rtweet_bot()``` which take api keys as arguments. Unfortunately, when I try to do this I get 403 error. – Kuba Sep 06 '22 at 21:33
  • I'm trying to understand what you mean when you say you don't know how to "hard-code" something, so I explained it. It is the anti-pattern of safe/secure code development and deployment, but it is often the first step (though still a mistake) that people take when trying to deploy something onto some type of shiny server. You should not consider deploying a shiny app using rtweet unless and until you can successfully use it locally. Until you can do so, don't ask a question about deploying it in shiny. – r2evans Sep 07 '22 at 01:09

0 Answers0