我知道在Shiny Server Pro中有一个密码控制功能.

1) 仅在正确输入密码后启动应用程序

谢谢

推荐答案

EDIT 2019:我们现在可以使用软件包shinymanager来实现这一点:invactivity脚本将在2分钟的不活动后使登录页面超时,这样您就不会浪费资源:

library(shiny)
library(shinymanager)

inactivity <- "function idleTimer() {
var t = setTimeout(logout, 120000);
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer;     // catches mouse clicks
window.onscroll = resetTimer;    // catches scrolling
window.onkeypress = resetTimer;  //catches keyboard actions

function logout() {
window.close();  //close the window
}

function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 120000);  // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();"


# data.frame with credentials info
credentials <- data.frame(
  user = c("1", "fanny", "victor", "benoit"),
  password = c("1", "azerty", "12345", "azerty"),
  # comment = c("alsace", "auvergne", "bretagne"), %>% 
  stringsAsFactors = FALSE
)

ui <- secure_app(head_auth = tags$script(inactivity),
                 fluidPage(
                   # classic app
                   headerPanel('Iris k-means clustering'),
                   sidebarPanel(
                     selectInput('xcol', 'X Variable', names(iris)),
                     selectInput('ycol', 'Y Variable', names(iris),
                                 selected=names(iris)[[2]]),
                     numericInput('clusters', 'Cluster count', 3,
                                  min = 1, max = 9)
                   ),
                   mainPanel(
                     plotOutput('plot1'),
                     verbatimTextOutput("res_auth")
                   )
                   
                 ))

server <- function(input, output, session) {
  
  result_auth <- secure_server(check_credentials = check_credentials(credentials))
  
  output$res_auth <- renderPrint({
    reactiveValuesToList(result_auth)
  })
  
  # classic app
  selectedData <- reactive({
    iris[, c(input$xcol, input$ycol)]
  })
  
  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })
  
  output$plot1 <- renderPlot({
    palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
              "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
    
    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })
  
}


shinyApp(ui = ui, server = server)

enter image description here

Original Post:

  1. 创建两个页面,如果用户输入正确的用户名和密码,您可以使用renderUIhtmlOutput来输出页面
  2. 你可以像我一样用用户名和密码tags来设置框的位置,如果你想也用tags$style来着色

然后,您可以进一步查看实际页面,并指定应根据不同的用户创建什么.你也可以查看JavaScript Popup Boxes

EDIT 2018:再看看这里的例子https://shiny.rstudio.com/gallery/authentication-and-database.html

Example of front page

rm(list = ls())
library(shiny)

Logged = FALSE;
my_username <- "test"
my_password <- "test"

ui1 <- function(){
  tagList(
    div(id = "login",
        wellPanel(textInput("userName", "Username"),
                  passwordInput("passwd", "Password"),
                  br(),actionButton("Login", "Log in"))),
    tags$style(type="text/css", "#login {font-size:10px;   text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -100px;margin-left: -150px;}")
  )}

ui2 <- function(){tagList(tabPanel("Test"))}

ui = (htmlOutput("page"))
server = (function(input, output,session) {
  
  USER <- reactiveValues(Logged = Logged)
  
  observe({ 
    if (USER$Logged == FALSE) {
      if (!is.null(input$Login)) {
        if (input$Login > 0) {
          Username <- isolate(input$userName)
          Password <- isolate(input$passwd)
          Id.username <- which(my_username == Username)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
            } 
          }
        } 
      }
    }    
  })
  observe({
    if (USER$Logged == FALSE) {
      
      output$page <- renderUI({
        div(class="outer",do.call(bootstrapPage,c("",ui1())))
      })
    }
    if (USER$Logged == TRUE) 
    {
      output$page <- renderUI({
        div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = "Contratulations you got in!",ui2())))
      })
      print(ui)
    }
  })
})

runApp(list(ui = ui, server = server))

R相关问答推荐

在Julia中调用R函数

从载体创建 pyramid

高质量地将R格式的图表从Word中输出

如果行和列名以相同的开头,将矩阵值设置为0

如何删除R中除某些特定名称外的所有字符串?

如何按排序顺序打印一个框架中所有精确的唯一值?

如何计算前一行的值,直到达到标准?

如果行和大于值,则过滤

过滤器数据.基于两列的帧行和R中的外部向量

隐藏e_mark_line的工具提示

在R中,如何在每个堆叠的条上放置误差条,特别是当使用facet_grid时?

用值序列对行进行子集化,并标识序列开始的列

bslib::card_header中的shine::downloadButton,图标而不是文本

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

在多页PDF中以特定布局排列的绘图列表不起作用

将向量元素重新排序为R中的第二个

在R中使用列表(作为tibble列)进行向量化?

如何在PrePlot()中将多个元素设置为斜体

如何在GALT包的函数&geom_x样条线中调整线宽

为什么不能使用lApply在包装函数中调用子集