基于用户登录的反应式过滤 - Shinyautr

问题描述 投票:0回答:1

我有一个 QA Shiny Dashboard 应用程序,它有一个主数据集,必须根据用户登录详细信息过滤后续表/映射中使用的行。

例如:如果用户使用特定用户名([电子邮件受保护])登录,则反应函数会选择包含此用户名的行,并将这些行传递给闪亮的应用程序进行渲染。

基本前提示例:

username <- "[email protected]"

new_dataset <- filter(dataset, column %like% username

output$table <- renderTable ({
  new_dataset
)}

我正在使用闪亮的作者包来创建它,但我很难将内置 Shinyautr 函数创建的用户登录详细信息传递给反应函数。代码如下:

users <- data.frame(user= c("user1", "user2", "user3"),
                         password = c("pass1", "pass2", "pass3"),
                         stringsAsFactors = FALSE)

ui <- dashboardPage(

  dashboardHeader(
    title = "QA App",

    tags$li(class = "dropdown", style = "padding: 8px;", shinyauthr::logoutUI("logout"))
  ),

  dashboardSidebar(
    collapsed = TRUE, sidebarMenuOutput("sidebar")
  ),

  dashboardBody(

    shinyjs::useShinyjs(),

    # shinyauthr login ui module here
    shinyauthr::loginUI("login"),

    tabItems(
      tabItem("tab", uiOutput("tab1_ui"))
    )
  )
)

server <- function(input, output) {

credentials <- callModule(shinyauthr::login, "login", 
                          data = users,
                          user_col = user,
                          pwd_col = password,
                          sodium_hashed = FALSE,
                          log_out = reactive(logout_init()))


# logout status managed by shinyauthr module and stored here
logout_init <- callModule(shinyauthr::logout, "logout", reactive(credentials()$user_auth))

output$sidebar <- renderMenu({
  req(credentials()$user_auth)
  sidebarMenu(

    menuItem("Tab", tabName = "tab", icon = icon("arrows-alt-v"))

  )
})

output$tab1_ui <- renderUI({

  req(credentials()$user_auth)
  fluidPage(

    mainPanel(
             tableOutput("table"),
      )
     )
  })
}

shinyApp(ui = ui, server = server)

我希望创建一个反应式函数来渲染这个 new_dataset 并将其传递到 UI 中的表输出。问题是如何以字符串形式访问用户登录凭据以用于过滤?

r shiny shinydashboard shinyauthr
1个回答
0
投票

正如在这些情况下经常发生的那样,很多争论最终得到了答案,但我会在这里发布我的解决方案,以防有人遇到类似的问题。

R2Evans 是正确的,因为可以使用凭据()$info 调用它。问题是尝试使用 eventReactive,而不是简单的响应式。

反应性用户详细信息通过以下方式收集:

credentials <- callModule(shinyauthr::login, "login", 
                          data = user_base,
                          user_col = user,
                          pwd_col = password,
                          sodium_hashed = FALSE,
                          log_out = reactive(logout_init()))

user_info <- reactive({credentials()$info})

filteredData <- reactive({
  filter(table, column %like% as.character(user_info()$user))
})

output$table <- renderTable(filteredData())
© www.soinside.com 2019 - 2024. All rights reserved.