Assuming I have 2 action buttons as a starting page 'Client' & 'Employee' as shown below, and for each option, there is a different web application. enter image description here

当用户点击"客户端"按钮时,我需要运行以下代码:

library(shiny)

ui <- 
navbarPage(
"The Client Web",
tabPanel("Section1 "),
tabPanel(" Section1 2")
)

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

}

shinyApp(ui = ui, server = server)

当用户点击"Employee"按钮时,我需要运行以下代码:

 library(shiny)

 ui <- 
 navbarPage(
 "The Employee Web",
 tabPanel("Component 1"),
 tabPanel("Component 2"),
  tabPanel("Component 3")
  )

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

  }

 shinyApp(ui = ui, server = server)

我需要在一个应用程序中同时使用这两个web应用程序,具体取决于用户类型"客户"或"员工".提前谢谢!

推荐答案

使用JavaScript

我会使用Javascript代码隐藏/显示任何一个页面.

你必须创建带有de按钮和两个NavBar页面的应用程序.

library(shiny)

ui <- fluidPage(
  # Buttons
  actionButton('clientsBtn', 'Clients'),
  actionButton('employeeBtn', 'Employee'),
  
  # Employee pag
  div(
    class = 'employees-page',
    navbarPage(
      "The Employee Web",
      tabPanel("Component 1"),
      tabPanel("Component 2"),
      tabPanel("Component 3")
    )
  ),
  
  # Clients page
  div(
    class = 'clients-page',
    navbarPage(
      "The Client Web",
      tabPanel("Section1 "),
      tabPanel(" Section1 2")
    )
  ),

  # Javascript to control de page logic
includeScript('script.js')
)

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

shinyApp(ui = ui, server = server)

playbook .js文件只是一个具有该扩展名的文本文件.

// hide by default the clients page
$('.clients-page').hide(); 

$('#clientsBtn').on('click', () => { 
  $('.employees-page').hide(); 
  $('.clients-page').show();
})

$('#employeeBtn').on('click', ()=>{ 
  $('.employees-page').show(); 
  $('.clients-page').hide(); 
})

enter image description here

单独使用shiny 的页面.路由

正如我promise 的,这里是使用{shiny.router}来实现你想要的方法.


library(shiny)
library(shiny.router)

# The root page is separated from clients and employee pages
# and contains two buttons/links that takes you the destination
# you wish

root_page <- tagList(
  tags$a(
  div(class='btn btn-default', id='clientsBtn', 'Clients'),
  href=route_link('clients')
  ),
  
  tags$a(
    div(class='btn btn-default', id='employeeBtn', 'Employee'), 
    href=route_link('employees')
    )
)

# The employee and clients page should include a button/link
# to take you back to the root page. I place the button in the
# first tabpanel of each page, but for better ux is good idea
# if you place it in all of them. Consider change its style and
# position using css configuration.

employee_page <- tagList(
  navbarPage(
    "The Employee Web",
    tabPanel(
      "Component 1",
      tags$a(
        div(class='btn btn-default', id='home1', 'Home'), 
        href=route_link('/')
      )
      ),
    tabPanel("Component 2"),
    tabPanel("Component 3")
  )
)


clients_page <- tagList(
  navbarPage(
    "The Client Web",
    tabPanel(
      "Section1 ",
      tags$a(
        div(class='btn btn-default', id='home1', 'Home'), 
        href=route_link('/')
      )
      ),
    tabPanel(" Section1 2")
  )
)

router <- make_router(
  route("/", root_page),
  route("employees", employee_page),
  route("clients", clients_page)
)

ui <- fluidPage(
  router$ui
)

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

shinyApp(ui, server)

enter image description here

R相关问答推荐

使用R中的小鼠()进行插补后观察次数显着变化

R包terra在投影时如何决定模板格栅属性?

R图中的字体大小和字体样式(带有R底图)

分组时间连续值

使用ggplot将平滑线添加到条形图

根据多个条件增加y轴高度以适应geom_text标签

使用sf或terra的LINESTRAING的累积长度

从BRM预测价值

线性模型斜率在减少原始数据时提供NA

在R函数中使用加号

R中有约束的优化问题:如何用复数和对数效益函数解决问题?

列名具有特殊字符时的循环回归

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

当每个变量值只能 Select 一次时,如何从数据框中 Select 两个变量的组合?

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

填充图例什么时候会有点?

如何在Quarto中使用美人鱼图表中的标记来加粗文本

使用函数从R中的列中删除标高

计算多变量的加权和