R Shiny: user authentication for a single app.R

前端 未结 7 1497
孤城傲影
孤城傲影 2020-12-25 14:15

I am developing a R Shiny App and want to add user name and logins. I checked the RStudio demo but that is only using the ShinyServer Pro and I am using the mongolite packag

7条回答
  •  梦谈多话
    2020-12-25 15:05

    Well, you can do it via from code by using renderUI and changing the UI on the fly. Here is an example of how to do it:

    library(shiny)
    library(ggplot2)
    
    u <- shinyUI(fluidPage(
      titlePanel("Shiny Password"),
    
      sidebarLayout(position = "left",
                    sidebarPanel( h3("sidebar panel"),
                                  uiOutput("in.pss"),
                                  uiOutput("in.clr"),
                                  uiOutput("in.titl"),
                                  uiOutput("in.cnt"),
                                  uiOutput("in.seed")
    
                    ),
                    mainPanel(h3("main panel"),
                              textOutput('echo'),
                              plotOutput('stdplot')
                    )
      )
    ))
    
    pok <- F
    
    s <- shinyServer(function(input, output) 
    {
      output$in.pss   <- renderUI({ input$pss; if (pok) return(NULL) else return(textInput("pss","Password:","")) })
      output$in.clr   <- renderUI({ input$pss; if (pok) return(selectInput("clr","Color:",c("red","blue"))) else return(NULL) })
      output$in.titl  <- renderUI({ input$pss; if (pok) return(textInput("titl","Title:","Data")) else return(NULL) })
      output$in.cnt   <- renderUI({ input$pss; if (pok) return(sliderInput("cnt","Count:",100,1000,500,5)) else return(NULL) })
      output$in.seed  <- renderUI({ input$pss; if (pok) return(numericInput("seed","Seed:",1234,1,10000,1)) else return(NULL) })
      histdata <- reactive(
        {
          input$pss;
          validate(need(input$cnt,"Need count"),need(input$seed,"Need seed"))
          set.seed(input$seed)
          df <- data.frame(x=rnorm(input$cnt))
        }
      )
      observe({
         if (!pok) {
           password <- input$pss
           if (!is.null(password) && password == "pass") {
             pok <<- TRUE
           }
         }
       }
      )
      output$echo = renderText(
        {
          if (pok) {
            s <- sprintf("the %s is %s and has %d rows and uses the %d seed",
               input$ent,input$clr,nrow(histdata()),input$seed)
          } else {
            s <- ""
          }
          return(s)
        }
      )
      output$stdplot = renderPlot(
        {
          input$pss
          if (pok) {
            return(qplot(data = histdata(),x,fill = I(input$clr),binwidth = 0.2,main=input$titl))
          } else {
            return(NULL)
          }
        }
      )
    }
    )
    shinyApp(ui=u,server=s)
    

    Yields

    this at login:

    And this once you have entered the hardcoded password "pass".

    Of course programming this way is a bit awkward, but you could use tabs and hide them perhaps using a similar logic.

    Or if you are using shinyServer you could probably put a filter in front of the site. But this is how I would approach it in Shiny.

提交回复
热议问题