Hello everyone,
I'm new into the R community and pleased to be part of it.
Unfortinately, I'm actually stuck on a little problem: I'm unable to load the value of a global var before it is used in the UI. Here is my code:
# global.R
globalVar = 0
# ui.R
library(shiny)
library(RMySQL)
db = dbConnect(dbDriver("MySQL"),
dbname = "db_name",
host = "127.0.0.1",
port = 3306,
user = "db_user",
password = "db_password")
# Returns 1 in our case.
getSomething = function() {
dbGetQuery(db, sprintf("SELECT column FROM Table WHERE Id = %s", globalVar));
}
gender <- getSomething() # = 1
shinyUI(fluidPage(theme = "style.css",
titlePanel(title="", windowTitle="Test"),
wellPanel(
h4("Test"), br(),
selectInput("gender","Gender",choices = c("Male" = 1, "Female" = 2), selected=gender)
)
))
# server.R
library(urltools)
shinyServer(function(input, output, session) {
observe({url <- sprintf(
"%s//%s:%s%s%s",
session$clientData$url_protocol,
session$clientData$url_hostname,
session$clientData$url_port,
session$clientData$url_pathname,
session$clientData$url_search)
urlParams <- param_get(urls = url, parameter_names = c("id")))
globalVar <<- paramsUrl$id
})
})
So if I load the app for the first time, globalVar = 0 for the UI but R Studio tells me that it is equal to the value passed in the id url param.
Do you think it is possible to do what I want to do?
Thank you very much for your help!