Can We Modulrize an app which has modules in it?

I have an app which uses three different modules it works fine. I want to modularize that app too. so that I can use it inside another app. Can I do that if so how?

this is what the app looks like.

# load_libraries ----------------------------------------------------------

library(shiny)
library(RSQLite)
library(shinydashboard)
library(plotly)
library(data.table)
library(lubridate)
library(dbplyr)
library(tidyverse)
library(DBI)
library(crosstalk)
library(RSQLite)
library(DT)


# source_code -------------------------------------------------------------

source('www/Modules/select_data.R')
source('www/Modules/dashboard_module.R')
source('www/Modules/emp_data.R')

# front_end ---------------------------------------------------------------

main_ui <- dashboardPage(dashboardHeader(),
                         dashboardSidebar(sidebarMenu(
                             menuItem('MainData', icon = icon('database'),
                                      tabName = 'sql_data_tab'),
                             menuItem(
                                 'Dashboard',
                                 icon = icon('dashboard'),
                                 tabName = 'dashboard_tab'
                             ),
                             menuItem(
                                 'Employee',
                                 icon = icon('user'),
                                 tabName = 'employee_tab'
                             )
                         )),
                         dashboardBody(tabItems(
                             tabItem(
                                 tabName = 'dashboard_tab',
                                 mod_dashboard_ui('dashboardpage')
                             ),
                             tabItem(tabName = 'sql_data_tab',
                                   
                                     
                                     mod_select_data_ui('sql')
                            ),
                            tabItem(tabName = 'employee_tab',
                                    mod_emp_ui('employee')
                            )
                         )))


# server ------------------------------------------------------------------

main_server <- function(input, output, session) {
    # connect_to_DB -----------------------------------------------------------
    
    
    sqlite <- reactive({
        dbConnect(SQLite(),
                  'www/main_data.sqlite')
    })
    
    
    # retreive_data -----------------------------------------------------------
    
    fulldata<-callModule(mod_select_data_serve,'sql',sqlite)
    
    

    # show_dashboard ----------------------------------------------------------

    
    callModule(mod_dashboard_serve,'dashboardpage',sqlite,fulldata)
    

    # show_employee -----------------------------------------------------------

    callModule(mod_emp_serve,'employee',sqlite,fulldata)
    

# dbdisconnect ------------------------------------------------------------

onSessionEnded(function(){
    dbDisconnect(sqlite())
})    
    
    
}
# runapp ------------------------------------------------------------------

shinyApp(main_ui, main_server)

I tried to modulerize the same code and called it from another app but it doesn't work. This is how I modulerized it.

# load_libraries ----------------------------------------------------------

library(shiny)
library(RSQLite)
library(shinydashboard)
library(plotly)
library(data.table)
library(lubridate)
library(dbplyr)
library(shinyjs)
library(tidyverse)
library(DBI)
library(crosstalk)
library(RSQLite)
library(DT)


# source_code -------------------------------------------------------------

source('www/Modules/select_data.R')
source('www/Modules/dashboard_module.R')
source('www/Modules/emp_data.R')
source('www/Modules/CRUD_db.R')
source('www/Modules/mod_SQL_functions.R')

# front_end ---------------------------------------------------------------

main_ui <- function(id){

# namespace ---------------------------------------------------------------

ns<-NS(id)


# Ui_function -------------------------------------------------------------
    dashboardPage(dashboardHeader(),
                  dashboardSidebar(sidebarMenu(
                      menuItem(ns('MainData'), icon = icon('database'),
                               tabName = ns('sql_data_tab')),
                      menuItem(
                          ns('Dashboard'),
                          icon = icon('dashboard'),
                          tabName = ns('dashboard_tab')
                      ),
                      menuItem(
                          ns('Employee'),
                          icon = icon('user'),
                          tabName = ns('employee_tab')
                      ),
                      menuItem(
                          ns('Password'),
                          icon = icon('users'),
                          tabName = ns('user_tab')
                      )
                  )),
                  dashboardBody(tabItems(
                      tabItem(
                          tabName = ns('dashboard_tab'),
                          mod_dashboard_ui(ns('dashboardpage'))
                      ),
                      tabItem(tabName = ns('sql_data_tab'),
                              
                              
                              mod_select_data_ui(ns('sql'))
                      ),
                      tabItem(tabName = ns('employee_tab'),
                              mod_emp_ui(ns('employee'))
                      ),
                      tabItem(tabName = ns('user_tab'),
                              mod_crud_ui(ns('user'))
                      )
                  )))
    
}
    

# server ------------------------------------------------------------------

main_server <- function(input, output, session) {
    

    # namespace ---------------------------------------------------------------

    ns<-session$ns
    
    # connect_to_DB -----------------------------------------------------------
    
    
    sqlite <- reactive({
        dbConnect(SQLite(),
                  'www/main_data.sqlite')
    })
    
    
    # retreive_data -----------------------------------------------------------
    
    fulldata<-callModule(mod_select_data_serve,ns('sql'),sqlite)
    
    
    
    # show_dashboard ----------------------------------------------------------
    
    
    callModule(mod_dashboard_serve,ns('dashboardpage'),sqlite,fulldata)
    
    
    # show_employee -----------------------------------------------------------
    
    callModule(mod_emp_serve,ns('employee'),sqlite,fulldata)
    
    
    # show_passwords ----------------------------------------------------------
    
    callModule(mod_crud_serve,ns('user'),sqlite())
    
    # dbdisconnect ------------------------------------------------------------
    
    onSessionEnded(function(){
        dbDisconnect(sqlite())
    })    
    
    
}
# runapp ------------------------------------------------------------------

shinyApp(main_ui, main_server)

but it looks something like this

But it should look something like this

Please help me out I am unable to find anything on the internet that can help me write a module inside another module. How to work around it.

It turns out to be I shouldn't use ns in callmodule function names

like instead of

innerResult <- callModule(inner, ns("inner1"))

I should use

innerResult <- callModule(inner, "inner1")

But the UI function needs to be wrapped around in ns.

I rewrote the code and it worked. Here is where I found it. at the very bottom

https://shiny.rstudio.com/articles/modules.html

And this is the new code written

# load_libraries ----------------------------------------------------------

library(shiny)
library(RSQLite)
library(shinydashboard)
library(plotly)
library(data.table)
library(lubridate)
library(dbplyr)
library(shinyjs)
library(tidyverse)
library(DBI)
library(crosstalk)
library(RSQLite)
library(DT)


# source_code -------------------------------------------------------------

source('www/Modules/select_data.R')
source('www/Modules/dashboard_module.R')
source('www/Modules/emp_data.R')
source('www/Modules/CRUD_db.R')
source('www/Modules/mod_SQL_functions.R')

# front_end ---------------------------------------------------------------

main_ui <- function(id){

# namespace ---------------------------------------------------------------

ns<-NS(id)


# Ui_function -------------------------------------------------------------
    dashboardPage(dashboardHeader(),
                  dashboardSidebar(sidebarMenu(
                      menuItem('MainData', icon = icon('database'),
                               tabName = 'sql_data_tab'),
                      menuItem(
                          'Dashboard',
                          icon = icon('dashboard'),
                          tabName = 'dashboard_tab'
                      ),
                      menuItem(
                          'Employee',
                          icon = icon('user'),
                          tabName = 'employee_tab'
                      ),
                      menuItem(
                          'Password',
                          icon = icon('users'),
                          tabName = 'user_tab'
                      )
                  )),
                  dashboardBody(tabItems(
                      tabItem(
                          tabName = 'dashboard_tab',
                          mod_dashboard_ui(ns('dashboardpage'))
                      ),
                      tabItem(tabName = 'sql_data_tab',
                              
                              
                              mod_select_data_ui(ns('sql'))
                      ),
                      tabItem(tabName = 'employee_tab',
                              mod_emp_ui(ns('employee'))
                      ),
                      tabItem(tabName = 'user_tab',
                              mod_crud_ui(ns('user'))
                      )
                  )))
    
}
    

# server ------------------------------------------------------------------

main_server <- function(input, output, session) {
    

    # namespace ---------------------------------------------------------------

    ns<-session$ns
    
    # connect_to_DB -----------------------------------------------------------
    
    
    sqlite <- reactive({
        dbConnect(SQLite(),
                  'www/main_data.sqlite')
    })
    
    
    # retreive_data -----------------------------------------------------------
    
    fulldata<-callModule(mod_select_data_serve,'sql',sqlite)
    
    
    
    # show_dashboard ----------------------------------------------------------
    
    
    callModule(mod_dashboard_serve,'dashboardpage',sqlite,fulldata)
    
    
    # show_employee -----------------------------------------------------------
    
    callModule(mod_emp_serve,'employee',sqlite,fulldata)
    
    
    # show_passwords ----------------------------------------------------------
    
    callModule(mod_crud_serve,'user',sqlite())
    
    # dbdisconnect ------------------------------------------------------------
    
    onSessionEnded(function(){
        dbDisconnect(sqlite())
    })    
    
    
}
# runapp ------------------------------------------------------------------

shinyApp(main_ui, main_server)

It might help you so do check it.

HI,
I read you post, I am also searching the same but not exactly like this.
If you found the solution Would you suggest me ?

I have made the shiny application and I want to call one other shiny application(Facto shiny already made for PCA) into my existed shiny code.
I have been trying to do that with the source() but I am getting error Can't call runApp() from within runApp(). If your application code contains runApp(), please remove it.
Of course, the other shiny(fact shiny) app has UI.R and server. R of its own.

Please please let me know if i can do to make it possible ?

there are excellent resources in the Rstudio website for learning modules you must learn the basics.

then you will have to wrap your entire UI into a function and wrap all the id's with a namespace of id. and by calling the module means you are actually calling another function so you don't need a runApp into it.

If you have any problem I can help but only when you share code with me. But I would suggest to at least watch a video or 2 and read the basic document in rstudio forum then you would be able to do it on your own. else do message me I will more than happy to help. I know it's frustrating when you don't find a solution.

:grinning:

1 Like

Thanks for your response.
I will inform you if I need your help :slight_smile:

1 Like