4

I'm attempting to store app secrets for my Shiny app (API keys, database credentials) using the secret package. I have the vault correctly set up and accessible from the console, and have added a test secret (name "test", with some foobar text inside).

I have a two-file Shiny setup (ui.r/server.r), and am attempting to print the test value in the UI to prove it works before building more complex stuff.

ui.r:

library(shiny)
library(leaflet)
library(secret)

# Leaflet example UI.

ui <- fluidPage(
  leafletOutput("mymap"),
  p(get_secret("test")),
  actionButton("recalc", "New points")
)

server.R:

library(shiny)
library(leaflet)

server <- function(input, output, session) {

points <- eventReactive(input$recalc, {
 cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
 }, ignoreNULL = FALSE)

output$mymap <- renderLeaflet({
  leaflet() %>%
  addProviderTiles(providers$Stamen.TonerLite,
                   options = providerTileOptions(noWrap = TRUE)
  ) %>%
  addMarkers(data = points())
 })
}

Bizarrely, I get an error when running using the "Run App" command at the top of ui.R or server.R - Error : secret ‘test’ does not exist, or use cmd+shift+enter. As I understand it, this runs runApp('app').

However, if I explicitly call shinyApp(ui = ui, server = server), as if this was a one-file app, I get it running normally with everything included: see foobar text below the map.

If I remove the call to get_secret() in the UI.r, the runApp() method works (just, obviously, then, no secret).

I really prefer to use a two-file structure to help manage complexity, but can't figure out how to run it within RStudio and have my secret vault be accessible.

Thanks for your help!

0 Answers0