如何根据用户隐藏/显示Flexdashboard页面?

rjee0c15  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(159)

我创建了一个带有两个选项卡的Flexdashboard。我使用shinymanager包进行身份验证。在shinymanager凭据中,我创建了两个用户:"admin"和"manager"。现在我希望admin用户可以访问这两个选项卡,而manager只能访问选项卡A。
下面是我的代码:

---
title: "Flexdashboard"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{css}
.panel-auth {
  position: fixed;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #FFF;
  opacity: 1;
  z-index: 99997;
  overflow-x: hidden;
  overflow-y: scroll;
}
library(flexdashboard)
library(shiny)
library(shinymanager)

credentials <- data.frame(
  user = c("admin", "manager"),
  password = c("1234", "1234"),
  stringsAsFactors = FALSE
)
auth_ui(id = "auth")
auth <- callModule(
    module = auth_server,
    id = "auth",
    check_credentials = check_credentials(credentials))

user_role <- "manager"
r_show_hide <- reactive({
  if(user_role %in% auth$user) {
      "hidden"
  } else {
      "show" 
  }
})

TAB A

Chart

TAB B {.r isolate(r_show_hide())}

Chart

093gszye

093gszye1#

我认为您的方法在设计上是行不通的,因为渲染发生在生成任何用户输入之前。
话虽如此,我还是使用一些JavaScript来隐藏相应的导航栏项:

---
title: "Flexdashboard"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{css}
.panel-auth {
  position: fixed;
  top:0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #FFF;
  opacity: 1;
  z-index: 99997;
  overflow-x: hidden;
  overflow-y: scroll;
}
library(flexdashboard)
library(shiny)
library(shinymanager)

credentials <- data.frame(
   user = c("admin", "manager"),
   password = c("1234", "1234"),
   stringsAsFactors = FALSE
)
Shiny.addCustomMessageHandler('hide-tabs', function(tabs) {
   /* for whatever reason Shiny does not box single values 
      despite the output of `shiny:::toJSON(c("TAB A"))`.
      Thus, make sure that tabs is always an array
   */
   if (!Array.isArray(tabs)) {
      tabs = [tabs]; 
   }
   const $sel = $('.nav-item > a').filter(function() {
      return(tabs.includes($(this).text()));
   });
   $sel.addClass('hidden');
})
auth_ui(id = "auth")
auth <- callModule(
   module = auth_server,
   id = "auth",
   check_credentials = check_credentials(credentials))

observeEvent(auth$user, {
   if (auth$user == "manager") {
      session$sendCustomMessage("hide-tabs", c("TAB B"))
   }
})

TAB A

Chart on TAB A

TAB B

Chart on TAB B

相关问题