1

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)
SeGa
  • 9,454
  • 3
  • 31
  • 70

1 Answers1

1

Intersting question (+1). You could read the data into R and then use Shiny.addCustomMessageHandler(...) to pass the data from R to javascript.

On server side you would use:

  observeEvent(input$showAlert,{
    message = config$default$val
    session$sendCustomMessage("handler1", message)
  })                     

And in javascript:

Shiny.addCustomMessageHandler("handler1", showConfig );
function showConfig(message){
   alert(message);
}

Reproducible example:

library(yaml)
library(shiny)
config = read_yaml("config.yaml")

ui = shinyUI(
  bootstrapPage(
    tags$script('
                Shiny.addCustomMessageHandler("handler1", showConfig );
                function showConfig(message){
                  alert(message);
                }
    '),
    actionButton("showAlert", "show alert")
  )
)

server = shinyServer(function(input,output,session){
  observeEvent(input$showAlert,{
    message = config$default$val
    session$sendCustomMessage("handler1", message)
  })                     
})

shinyApp(ui, server)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • Yes, that seems to work for this small example, but apparently it doesn't for the real example, where there is a JS-function which runs before Shiny is initiated. And in that function I would need to access the yaml-config variable. – SeGa Jun 18 '19 at 12:14
  • 1
    hmm did you try reading from file: https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file? Or as a dirty workaround you could create the javascript file in R (from text) before starting shiny,... – Tonio Liebrand Jun 18 '19 at 16:18