Hide API Key in Knitted HTML?

I'll do that, open an issue. I was able to write a small script using xml2. I suppose that is the best solution for now! Here's my solution if others have trouble. I saved the js file that the API called into api.js.

library(xml2)

files = list.files(pattern = ".html")

replace_api_key <- function(html_filename) {
  h2 = read_html(html_filename)
  
  # Find api key
  g_api <- xml_find_first(h2, "//script[contains(@src, 'maps.googleapis.com')]")
  xml_attrs(g_api) <- c(src = 'lib/api.js')
  
  write_html(h2, html_filename,
             options = c("as_html"))
}
1 Like