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

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

我正在使用 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)
r shiny rstudio shinyjs shinyauthr
1个回答
0
投票

编辑后我能够满足您的第一个要求:仅在登录后显示用户界面,请参阅下面的代码。我在上传 csv 时遇到问题。有样本文件吗?

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.