Hello everyone,
I would like to ask a question about the runApp function. I have developed my Shiny project which is located in a directory like this:
Z:/Documents/dev/myproject/
Therefore, myproject
subdirectory, is the folder containing my app.R
and of course, ui.R
and server.R
of my project. To launch my application from a terminal, according to the documentation, I am supposed to run the following in the current running R session:
runApp("Z:/Documents/dev/myproject")
Yet, I noticed that to make my program work, actually I need to do a source()
on the app.R
file before calling runApp()
:
source("Z:/Documents/dev/myproject/app.R")
runApp("Z:/Documents/dev/myproject")
The reason, is that my GUI (HTML part I mean) is quite big. Therefore to make the code both easier to read and maintain, I split it into multiple files for different parts such as menu, sidebar and main, etc.
So what I do is,
-
First, I read the content of these HTML files separately using
readLines()
, -
second, I call the
HTML()
function in myui.R
, in order to incorporate different HTML components into their corresponding locations.
I have a lot of specific CSS
, JavaScript
and HTML
so I almost don't use Shiny's predefined widgets and that's why I use raw HTML code by calling HTML()
.
The following is how my app.R
looks like
library("tidyverse")
library("shiny")
library("htmltools")
library("RPostgres")
appBaseDir <- str_replace_all(Sys.getenv("APP_BASE_DIR"), "\"|'", "")
menuBar <- readLines(str_c(appBaseDir, "www/html/menubar.html"))
sideBar <- readLines(str_c(appBaseDir, "www/html/sideBar.html"))
main <- readLines(str_c(appBaseDir, "www/html/main.html"))
source(str_c(appBaseDir, "ui.R"))
source(str_c(appBaseDir, "server.R"))
Now the problem is, when I call runApp("Z:/Documents/dev/myproject")
, it seems to me that ui.R
is evaluated before the content of app.R
is fully evaluated. Consequently, the HTML prerequisites (menuBar, sideBar, main, etc.) will not have been evaluated by the time ui.R
is executed, resulting in an error message indicating that the corresponding vectors were not found.
Therefore,
runApp("Z:/Documents/dev/myproject")
Fails as the prerequisites are not present when ui.R
is being evaluated but
source("Z:/Documents/dev/myproject/app.R")
runApp("Z:/Documents/dev/myproject")
Runs perfectly well as everything seems to be there upon the application launch. I checked the runApp
documentation but I didn't find anything about any specific order of evaluation between ui.R
and app.R
.
Therefore, my question is: Am I on the right track by running a source()
on app.R
before calling runApp()
as indicated above?
Thanks in advance.