Shiny-登录后看不到我的html页面?

Posted

技术标签:

【中文标题】Shiny-登录后看不到我的html页面?【英文标题】:Shiny-Cannot see my html page after login? 【发布时间】:2020-03-18 01:33:46 【问题描述】:

我正在使用 Shiny 并尝试显示我的代码包含一个 csv 文件输入对话框,一个带有两个选项卡的选项卡集,使用侧面板/主面板或任何其他方式。但是我的代码没有运行包含这些元素的代码。我能够成功登录,但不知道如何显示 html 代码并在登录后接受输入。

这是我的代码。

library(shiny)
library(shinyauthr)
library(shinyjs)
library(survival)
library(shinyWidgets)

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

ui <- fluidPage(
shinyjs::useShinyjs(),
div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
shinyauthr::loginUI(id = "login"),
setBackgroundColor(color = c("#F7FBFF", "#2171B5"), gradient = "linear", direction = "bottom"),
htmlOutput(
setBackgroundColor(color = c("#000000", "#000000"), gradient = "linear", direction = "bottom")
),
ui2()
)
ui2<-function()

fileInput("file1", "Choose CSV File",
          multiple = FALSE,
          accept = c("text/csv",
                     "text/comma-separated-values,text/plain",
                     ".csv"))
tabsetPanel(type = "tabs",
          tabPanel("Summary of data", tableOutput("maindf")),
          tabPanel("Summary of data", tableOutput("user_table"))
  )


server <- 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 data frame, 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()))

  # pulls out the user information returned from login module
  user_data <- reactive(credentials()$info)

  output$user_table <- renderTable(

    # use req to only render results when credentials()$user_auth is TRUE
    req(credentials()$user_auth)
    user_data()
  )
  output$maindf<-renderTable(
  req(input$file1)
  testdb <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)
  req(credentials()$user_auth)
  ###### how to show main data frame????
  testdb
  )


shinyApp(ui = ui, server = server)

【问题讨论】:

【参考方案1】:

在您编辑后,我能够满足您的第一个要求:仅在登录后显示 uis,请参见下面的代码。我在上传 csv 时遇到了问题。你有示例文件吗?

P.S:这是shinyauthr package 的一个有趣的工作示例。

library(shiny)
library(shinyauthr)
library(shinyjs)
library(survival)
library(shinyWidgets)

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


ui2<-function()

  fileInput("file1", "Choose CSV File",
            multiple = FALSE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv"))
  tabsetPanel(type = "tabs",
              tabPanel("Summary of data", tableOutput("maindf")),
              tabPanel("Summary of data", tableOutput("user_table"))
  )


ui <- fluidPage(
  shinyjs::useShinyjs(),
  div(class = "pull-right", shinyauthr::logoutUI(id = "logout")),
  shinyauthr::loginUI(id = "login"),
  setBackgroundColor(color = c("#F7FBFF", "#2171B5"), gradient = "linear", direction = "bottom"),
  htmlOutput(
    setBackgroundColor(color = c("#000000", "#000000"), gradient = "linear", direction = "bottom")
  ),
  #ui2(),
  uiOutput("ui2")
)


server <- 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 data frame, 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()))

  # pulls out the user information returned from login module
  user_data <- reactive(credentials()$info)


  output$ui2 <- renderUI(

    req(credentials()$user_auth)

    fluidRow(

      fileInput("file1", "Choose CSV File",
              multiple = FALSE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),
      tabsetPanel(type = "tabs",
                  tabPanel("Summary of data", tableOutput("maindf")),
                  tabPanel("Summary of data", tableOutput("user_table"))
                 )
    )

  )


  output$user_table <- renderTable(

    # use req to only render results when credentials()$user_auth is TRUE
    req(credentials()$user_auth)
    user_data()
  )

  output$maindf<-renderTable(
    req(input$file1)
    testdb <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)
    req(credentials()$user_auth)
    ###### how to show main data frame????
    testdb
  )


shinyApp(ui = ui, server = server)

【讨论】:

亲爱的@alessio,我想做的是只有在成功登录后才能查看文件输入和相关代码。但是当我调用它时,它显示在登录屏幕下方或不显示它。我试图读取 CSV 文件的行是在登录后输入 CSV 文件并在数据框中显示数据后将触发的实际代码。希望我已经清楚地解释了我的问题。如果您有任何其他问题,请告诉我。

以上是关于Shiny-登录后看不到我的html页面?的主要内容,如果未能解决你的问题,请参考以下文章

使用 FB 登录构建了一个自定义 html 页面。我想在使用 facebook 登录按钮后将它们重定向到主页

登录成功后重定向到新页面

HTML问题解决-退出登录后,页面只有部分刷新到登录界面

带有 PHP 和 SESSION 的登录页面

在闪亮服务器上托管 html 页面

使用 Shiny 链接到 RMarkdown 上的本地 html 文件