set API key for Shiny App

Hello everyone! I'm rather new to R, so please bear with me.

I'm building a Shiny app, using the tidytransit package to do some analysis on transit data. I rely on tidytransit to fetch data from transitfeeds, which requires an API key.

I set my API key for transitfeeds locally using a function included in the tidytransit package. It just prompts me to paste my API key in the console, but that's not really an option when I deploy the app on shinyapps. The code below causes a "disconnected from server error," and including set_api_key() in my code also causes an error because I can't actually set the key.

Is there anyway to make my personal API key accessible to the app? The data from transitfeeds is public, so there are no privacy concerns that I'm aware of.

Thanks for your help!

``` r
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#


library(shiny)
library(tidytransit)
#> Warning: package 'tidytransit' was built under R version 3.5.3
library(tidyr)
#> Warning: package 'tidyr' was built under R version 3.5.3

ui <- fluidPage(
  

  titlePanel("transitMaps"),
  
  # Show a tidytransit summary plots
  mainPanel(
    plotOutput("summaryPlot")
    
  )
)

server <- function(input, output) {
  tidytransit::get_feedlist()

  # hardcode to select first result for Houston data
  houston_feedlist <- dplyr::filter(tidytransit::feedlist, loc_t == 'Houston, TX, USA')
  houston_gtfs_url <- houston_feedlist[1, "url_d"]
  
  houston <- tidytransit::read_gtfs(houston_gtfs_url,geometry=TRUE)
  
  output$summaryPlot <- renderPlot({
    
    plot(houston)
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
2019-05-14T20:03:56.913949+00:00 shinyapps[929595]: Warning: Error in api_check: API key not found. Please obtain a key from transitfeeds.com, and set using function 'set_api_key()'
2019-05-14T20:03:56.919342+00:00 shinyapps[929595]:   59: server [/srv/connect/apps/transitMaps/app.R#32]
2019-05-14T20:03:56.919338+00:00 shinyapps[929595]:   64: stop
2019-05-14T20:03:56.919339+00:00 shinyapps[929595]:   63: api_check
2019-05-14T20:03:56.919341+00:00 shinyapps[929595]:   60: tidytransit::get_feedlist
2019-05-14T20:03:56.919503+00:00 shinyapps[929595]: Error in api_check() : 
2019-05-14T20:03:56.919340+00:00 shinyapps[929595]:   62: has_api_key
2019-05-14T20:03:56.919341+00:00 shinyapps[929595]:   61: tfeeds_get
2019-05-14T20:03:56.919505+00:00 shinyapps[929595]:   API key not found. Please obtain a key from transitfeeds.com, and set using function 'set_api_key()'

It seems from the code in tidytransit setting API key is in fact setting an environment variable TRANSITFEED_API. For your app, you need to provide this environment variable on the deployment server.

It does not seem to be in the documentation but you can find it in the code

You could consider opening an issue in the repo to suggest better documentation on this API key setting.

So for your question, I would set the environment variable correctly depending on where you deploy your app.

3 Likes

Thanks so much! I added the key to my .Renviron file and it works. :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.