How to create a login page in shiny?

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

The goal is to have a shiny module ui1.R that loads a second module ui2.R when the button confirm is clicked. I think the issue is that is.null(input$confirm) is always invalidated. What I need instead, is to invalidate the expression only once confirm is clicked. The question is very close to Starting Shiny app after password input, but I am trying to modularize the solution.

ui.R

library(shiny) library(shinyjs) htmlOutput("page") 

server.R

rm(list = ls()) library(shiny) library(dplyr) library(shinyjs) Logged <-  FALSE  shinyServer(function(input, output) {    source('ui1.R') #login page   source('ui2.R')     observeEvent(!is.null(input$confirm), {     Logged <<- T   })    observe({     if (Logged == FALSE) {       output$page <- renderUI({          ui1Output('ui1Output')        })       output$lsuId <- renderText({ input$lsuId })     }     if (Logged == TRUE)      {       output$page <- renderUI({ ui2 })     }   })   callModule(ui1,'ui1')  }) 

ui1.R

library(shinyjs)  ui1Output <-  function(id, label = "ui1") {   ns <- NS(id)   shinyUI(fluidPage(     useShinyjs(),     titlePanel("Form"),     div(textInput(ns("lsuId"), "This has to be filled", ""),       actionButton(ns("confirm"), "Submit", class = "btn-primary")     )   )) }  ui1 <- function(input, output, session) {   observe({     LSUID <- reactive({ input$lsuId })     shinyjs::toggleState(id = "confirm", condition = LSUID())   }) } 

ui2.R

ui2<-  shinyUI(fluidPage(  div("well done!") )) 

global.R

source('ui1.R') #login page source('ui2.R') 

回答1:

I think you have two issues here:

First your confirm button value is stored here: input$"ui1Output-confirm" and not here: input$confirm

I would suggest to replace:

observeEvent(is.null(input$confirm), {   Logged <<- F }) 

By:

observeEvent(input$"ui1Output-confirm", {     Logged <<- T }) 

Then your observe function does not contain any reference to the button so it is not executed when the user clicks on it. I don't have a proper solution for this but a simple hack would be to add:

tmp <- input$"ui1Output-confirm" 

at the begining of the observe section:

observe({     tmp <- input$"ui1Output-confirm"      if (Logged == FALSE) {         output$page <- renderUI({              ui1Output('ui1Output')          })         output$lsuId <- renderText({ input$lsuId })     }     if (Logged == TRUE)      {          output$page <- renderUI({ ui2 })     } }) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!