/
run_shiny.R
82 lines (67 loc) · 2.4 KB
/
run_shiny.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#' Runs Shiny app for an AQUANES site
#' @param siteName site name for shiny app (default: "haridwar")
#' @param use_live_data should live data be used (default: FALSE)
#' @param mySQL_conf file path to mySQL config file (.my.cnf). Only used if
#' parameter use_live_data is TRUE and there is no .my.cnf in the app folder for
#' the selected site (default: NULL)
#' @param launch.browser If true, the system's default web browser will be
#' launched automatically after the app is started (default: TRUE)
#' @param ... further arguments passed to shiny::runApp()
#' @importFrom shiny runApp
#' @export
run_app <- function(siteName = "haridwar",
use_live_data = FALSE,
mySQL_conf = NULL,
launch.browser = TRUE,
...) {
use_live_data <- toupper(use_live_data)
shinyDir <- system.file("shiny", package = "aquanes.report")
appDir <- file.path(shinyDir, siteName)
if (!siteName %in% dir(shinyDir)) {
msg <- sprintf(
"Could not find shiny app directory for %s.\n
Please select for parameter 'siteName' one of:\n'%s'",
siteName,
paste(dir(shinyDir), collapse = ", ")
)
stop(msg, call. = FALSE)
}
if (siteName == "haridwar") {
mySQL_conf_path <- file.path(appDir, ".my.cnf")
if (use_live_data) {
if (!is.null(mySQL_conf)) {
file.copy(
from = mySQL_conf,
to = mySQL_conf_path
)
}
if (!file.exists(mySQL_conf_path)) {
msg <- sprintf("No '.my.cnf' file located under: %s\n.
Please once specify the path to a valid MySQL config file with parameter
'mySQL_conf'", appDir)
stop(
msg,
call. = FALSE
)
}
}
}
global_path <- file.path(appDir, "global.R")
if (file.exists(global_path) == FALSE) {
stop(
sprintf("Could not find a 'global.R' in: %s", appDir),
call. = FALSE
)
}
### adapt "global.R" to use live data or not
global_string <- readLines(global_path)
replace_line <- grep(pattern = "use_live_data\\s*<-", global_string)
global_string[replace_line] <- sprintf("use_live_data <- %s", use_live_data)
writeLines(global_path, text = global_string)
shiny::runApp(
appDir,
display.mode = "normal",
launch.browser = launch.browser,
...
)
}