I have a shiny app which uses the config
package, to use different configurations depending on the App-environment (testing / QA / production environment).
As I am also using a JS-file for the app, I was wondering if it is possible to get the values from the config.yaml
file in the JS file?
Right now I have hardcoded the values I would need.
In this small example, I would like to pass the value of val
in the config file to JavaScript, so I dont have to manually change the value when deploying to a certain environment.
In the app.R file in the js part I want to access config$val
, instead of hardcoding the value like var val = "abcdef"
;
config.yaml
default:
val: 'default123'
qa:
val: 'qa123'
prod:
val: 'prod123'
app.R
sys <- Sys.info()
ifelse("Windows" %in% sys[1],
{Sys.setenv(R_CONFIG_ACTIVE = "default")},
{ip <- system("ip address | grep -A 1 'eth0' | tail -2", intern = TRUE)
ip <- gsub(pattern = "inet ", "", regmatches(ip, regexpr("inet [0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", ip)), fixed = T)
ifelse(ip == "prodip",
Sys.setenv(R_CONFIG_ACTIVE = "prod"),
Sys.setenv(R_CONFIG_ACTIVE = "qasys"))
})
js <- HTML('
$(document).on("shiny:connected", function (event) {
// How can i access values from the config file in here?
//var val = config$val; // I wanna do this line, rather than
var val = "default123"; // this line
console.log(val);
})
')
library(shiny)
ui <- fluidPage(
tags$head(tags$script(js))
)
server <- function(input, output, session) {}
shinyApp(ui, server)