1

I have 2 different Shinnyapps in 2 different directories. And let's assume Shinyapp1's UI and server and Shinnyapp2's UI and server as given. How can I create 1 Shinyapp by combining these 2 different apps without rewriting the code? or if you know any GitHub example for this? Many thanks in advance.

 library(shiny)

ui <- navbarPage("Combine 2 Shinyapps",
           tabPanel("Shinyapp1"),
           tabPanel("Shinyapp2")
)

server <- function(input, output, session) { 
}
shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Seyma Kalay
  • 2,037
  • 10
  • 22
  • 1
    Can use `iframe` maybe https://stackoverflow.com/questions/33020558/embed-iframe-inside-shiny-app/33021018#33021018 – Pork Chop Oct 07 '21 at 08:12
  • Possible duplicate: https://stackoverflow.com/questions/36291489/how-do-you-integrate-2-different-shiny-apps-in-one also some ideas here: https://stackoverflow.com/questions/59592687/how-to-separate-out-tabs-into-their-own-script-in-a-shiny-app – MrFlick Oct 07 '21 at 08:17

1 Answers1

1

Here is an example using iframes (an approach I already mentioned in your duplicated earlier question).

In the global part im writing two dummy app.R files which are started in a seperate R process via callr::r_bg:

library(shiny)
library(callr)

makeDir <- function(dir){
  if(!dir.exists(dir)){
    dir.create(dir, recursive = TRUE)
  }}

lapply(c("app1", "app2"), makeDir)

writeLines(text = 'ui <- fluidPage(p("App 1 content"), 
sliderInput("obs", "Number of observations:",
    min = 0, max = 1000, value = 500
  ))
server <- function(input, output, session) {}
shinyApp(ui, server)', con = "app1/app.R")

writeLines(text = 'ui <- fluidPage(p("App 2 content"),
sliderInput("obs", "Number of observations:",
    min = 0, max = 100, value = 50
  ))
server <- function(input, output, session) {}
shinyApp(ui, server)', con = "app2/app.R")

r_bg(function(){
  shiny::runApp(
  appDir = "app1",
  port = 3939,
  launch.browser = FALSE,
  host = "127.0.0.1"
)}, supervise = TRUE)

r_bg(function(){
  shiny::runApp(
  appDir = "app2",
  port = 4040,
  launch.browser = FALSE,
  host = "127.0.0.1"
)}, supervise = TRUE)


ui <- navbarPage("Combine 2 Shinyapps",
                 tabPanel("Shinyapp1", tags$iframe(src="http://127.0.0.1:3939/", height="900vh", width="100%", frameborder="0", scrolling="yes")),
                 tabPanel("Shinyapp2", tags$iframe(src="http://127.0.0.1:4040/", height="900vh", width="100%", frameborder="0", scrolling="yes"))
)

server <- function(input, output, session) {
  
}

mainApp <- shinyApp(ui, server)

runApp(mainApp)

Another (better) approach would be to wrap both apps in separate modules with their own name space. This is less computationally intensive (a single R process instead of 3) but it would require to rearrange the code (which is not desired by OP: without rewriting the code).

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • 1
    I second the remark about modules that can be a good solution for reusing code and make multiple apps/ combine them – Bart Oct 07 '21 at 09:41