Hi PJ,
I will try to reproduce a small example from a very large one (sorry in advance if too long).
I currently have the following architecture for my code (the only one I could have thought about which does what I want --> BUT I realized it takes far too much time as I add products so I will have to find a more optimal solution absolutely!)
UI BODY SIDE:
dashboardBody(
tabItems(
tabItem(tabName = "tab_selection",
sidebarLayout(
sidebarPanel(width = 3,
radioButtons(...),
shiny::conditionalPanel(condition="", radioButtons(...)),
shiny::conditionalPanel(condition="",radioButtons(...))),
mainPanel(
DT::dataTableOutput("table"),
actionButton("selection")
))),
tabItem(tabName = "product1",source("project/product1.R")$value),
tabItem(tabName = "product2",source("project/product2.R")$value),
tabItem(tabName = "product3",source("project/product3.R")$value
)))
To sum up, the user arrives to the home page when going on the app. The homepage has a sidebarPanel with some radio buttons which allow him to filter a table that appears on the mainPanel on the right.
The user can choose a product from this table and click on a button that appears below the table.
Each product has a particular productX.ui.R file (box with several tabs with different content) and a particular productX.server.R file.
As you can see from the code above, every productX.ui.R file is executed (which I suppose is one of the reasons why the app is so slow???)
SIDEBAR SIDE:
sidebarMenu(
id = "tabs",
shinyjs::useShinyjs(),
menuItem("Product Selection", tabName = "tab_selection", ...),
shinyjs::hidden(div(id="product1",menuItem("P1 Name",tabName = "product1", ...))),
shinyjs::hidden(div(id="product2",menuItem("P2 Name",tabName = "product2",...))),
shinyjs::hidden(div(id="product3",menuItem("P3 Name",tabName = "product3", ...)))
)
We have many menuItems:
- one for the homepage
- one for each product
All menuItems for the products are initially hidden using shinys::hidden()
SERVER SIDE:
server <- function(input, output,session) {
inputdata <- reactive({...})
output$table <- DT::renderDataTable(
inputdata(),
...)
observeEvent(input$selection, {
ID = ...
shinyjs::show(id= ID,anim = TRUE)
if (ID == "product1") {source("project/product1_server.R",local=TRUE)$value
}else if(ID == "product2") {source("project/product2_server.R",local=TRUE)$value
}else if(ID == "product3") {source("project/product3_server.R",local=TRUE)$value
updateTabItems(session, "tabs", ID)
}})
So that when the user chooses a product, the menuItem of this product appears in the sidebar and the user is redirected directly to this menuItem. The particular server file for this product is ran so that I do not have to run all the server files as it will take ages.
I am trying to find a way to optimize this as my App is far too slow (specially at initiation).
Using Profvis is showing me the following:
Do you (or anyone) have a suggestion so that the App does not have the run all the productX_server.R files???
Regards,
Maxime