如何从R

时间:2019-03-07 06:02:57

标签: r shinydashboard reactive

我正在开发一个闪亮的仪表板。我需要从登录模块中单独获取用户名。我正在使用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()
  })
})

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

但是,我不确定只显示用户,而是显示密码,权限和名称。是否有解决此问题的方法?

1 个答案:

答案 0 :(得分:0)

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

  1. user_data仅给您返回用户名

user_data <- 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只是您要过滤的某个字符