我有三个RShiny文件:
ui.R
,我在其中定义了两个标签。第一个是带有图形的sliderInput
,另一个是空的。我也在这里定义登录模块。
shinyUI(bootstrapPage(
tagList(
tabsetPanel(
tabPanel("A Lot of Info in Tab1",
column(2, sliderInput("obs", "Number of observations:",
min = 10000, max = 90000,
value = 50000, step = 10000)),
column(10, plotOutput("distPlot"))
),
tabPanel("A Lot of Info in Tab2")
)
),
## Login module;
div(class = "login",
uiOutput("uiLogin"),
textOutput("pass")
)))
server.R
,我在其中定义登录名和密码为“test”。另外,我在这里加载文件Login.R
,其定义如下。
library(shiny)
library(datasets)
Logged = FALSE;
PASSWORD <- data.frame(Brukernavn = "test", Passord = "test")
shinyServer(function(input, output) {
source("www/Login.R", local = TRUE)
observe({
if (USER$Logged == TRUE) {
output$distPlot <- renderPlot({
dist <- NULL
dist <- rnorm(input$obs)
hist(dist, breaks = 100, main = "")
})
}
})
})
和Login.R
定义了成功登录所需的所有规则集。
#### Log in module ###
USER <- reactiveValues(Logged = Logged)
passwdInput <- function(inputId, label) {
tagList(
tags$label(label),
tags$input(id = inputId, type="password", value="")
)
}
output$uiLogin <- renderUI({
if (USER$Logged == FALSE) {
wellPanel(
textInput("userName", "User Name:"),
passwdInput("passwd", "Pass word:"),
br(),
actionButton("Login", "Log in")
)
}
})
output$pass <- renderText({
if (USER$Logged == FALSE) {
if (!is.null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
Id.username <- which(PASSWORD$Brukernavn == Username)
Id.password <- which(PASSWORD$Passord == Password)
if (length(Id.username) > 0 & length(Id.password) > 0) {
if (Id.username == Id.password) {
USER$Logged <- TRUE
}
} else {
"User name or password failed!"
}
}
}
}
})
主要问题是,在我没有登录时如何隐藏标签? 至于现在,虽然用户没有登录,但他看不到结果,但可以看到标签中的所有信息,移动滑块等。这段代码只是一个小例子,实际上,我有一个更复杂的情况。