【问题标题】:How to retrieve a parameter from callModule() in R如何从 R 中的 callModule() 中检索参数
【发布时间】:2019-07-28 22:34:51
【问题描述】:

我正在开发一个闪亮的仪表板。我需要从登录模块中单独获取用户名。我正在使用 callModule() 执行仪表板的身份验证。

ui.R 代码如下所示:

library(shiny)
library(shinyauthr)
library(shinyjs)
shinyUI(
    fluidPage(
  # must turn shinyjs on
  shinyjs::useShinyjs(),
  # add logout button UI 
  div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
  # add login panel UI function
  shinyauthr::loginUI(id = "login"),
  # setup table output to show user info after login
  tableOutput("user_table")
)
)

server.R 代码如下所示:

library(shiny)
library(shinyauthr)
library(shinyjs)
library(DBI)
library(RMySQL)
library(dplyr)

# dataframe that holds usernames, passwords and other user data

user_base <- data.frame(
  user = c("user1", "user2"),
  password = c("pass1", "pass2"), 
  permissions = c("admin", "standard"),
  name = c("User One", "User Two"),
  stringsAsFactors = FALSE)

shinyServer(function(input, output, session) {

  # call the logout module with reactive trigger to hide/show
  logout_init <- callModule(shinyauthr::logout, 
                            id = "logout", 
                            active = reactive(credentials()$user_auth))


  # call login module supplying user and password cols
  # and reactive trigger
  credentials <- callModule(shinyauthr::login, 
                            id = "login", 
                            data = user_base,
                            user_col = user,
                            pwd_col = password,
                            log_out = reactive(logout_init()))

  print(class(credentials))
  # pulls out the user information returned from login module
  user_data <- reactive({credentials()$info})
  #user_name <- reactive({credentials()$user_col})
  print(user_data)

  output$user_table <- renderTable({
    # use req to only render results when credentials()$user_auth is TRUE
    req(credentials()$user_auth)
    user_data()
  })
})

当我尝试上面的代码时,我得到以下输出

但是,我不确定是否单独显示用户,而不是显示密码、权限和名称。有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: r shinydashboard reactive


    【解决方案1】:

    您可以轻松地操作要显示的数据: 这里有几个:

    1. user_data 只给你回用户名

    user_data &lt;- reactive({credentials()$info[1]}) 因为credentials()$info只是一个字符数组,所以只能选择第一个:用户名

    1. renderTable 只显示用户名: 你可以这样做:

      output$user_table <- renderTable({ req(credentials()$user_auth) user_data()[1] })

    output$user_table <- renderTable({ # use req to only render results when credentials()$user_auth is TRUE req(credentials()$user_auth) userData <-user_data() userData[1] })

    与第一个逻辑相同:最后,user_data 只是您要过滤的某个字符

    【讨论】:

      猜你喜欢
      • 2011-07-23
      • 1970-01-01
      • 2013-04-06
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多