If Statement in Shiny 基于 reactive 中的值
If Statement in Shiny based on values in reactive
我正在通过比较电子邮件中发送的代码和用户提交的代码在我的应用程序中构建身份验证部分。我尝试通过在 reactive()、isolate()、renderTable() 中使用来添加 if 语句。我得到的值应该在反应部分错误中,或者应用程序根本没有响应。
以下是我在 Server.R 中的内容,应用程序完全没有响应且没有错误。
shinyServer(function(input, output, session) {
myData <- reactive({
req(input$Id)
#connect to the database, get the data, res
#send an email with random number, rnd
list(res,rnd)
})
output$tbTable <- renderTable({req(input$Auth)
if (input$AuthCode==myData()$rnd) {
myData()$res
}
else{
as.data.frame(c("Authentication Failed"))
}
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Id, " filname.xlsx", sep = "")
},
content = function(file) {
write.csv(myData(), file, row.names = FALSE)
}
)#this part need to depend on the if-statement as well
}
)
UI.R
ui <- shinyUI(fluidPage(title = "aaa",
titlePanel("aaa"),
sidebarLayout(
sidebarPanel(
textInput("Id", "Enter Acct Name below"),
submitButton(text="Submit"),
tags$hr(),
numericInput("AuthCode",label="Authentication",value=""),
actionButton("Auth",label="Submit"),
tags$hr(),
tags$hr(),
downloadButton("downloadData", "Download Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", tableOutput("tbTable"))
))
),
)
)
我想我已经准备好做你想做的事了。请检查。我做了以下更改
将您的 submitButton
替换为 actionButton
作为 Acc
并使用 observeEvent
调用它。
当单击第二个按钮时,observeEvent
现在也会触发身份验证
Excel 扩展在 write.csv
中不起作用,因此更改了扩展。
server.R
shinyServer(function(input, output, session) {
# the first button will trigger this
observeEvent(input$Acc,{
# myData <- reactive({
req(input$Id)
#connect to the database, get the data, res
#send an email with random number, rnd
#list(res,rnd)
myData <<- list(res = 123,rnd = 345) #passing test value and storing it as global variable for accessing in other functions
# })
cat("mydata done")
})
# the second button will trigger this
observeEvent(input$Auth,{
output$tbTable <- renderTable({
#req(input$Auth)
if (input$AuthCode==myData$rnd) {
myData$res
}
else{
#as.data.frame(c("Authentication Failed"))
shiny::showModal(modalDialog(
title = "Important message",
"Authentication Failed!"
))
}
})
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Id, " filname.csv", sep = "") #renamed it to csv because write.csv writes to csv not excel
},
content = function(file) {
write.csv(myData, file, row.names = FALSE)
}
)#this part need to depend on the if-statement as well
}
)
ui.R
ui <- shinyUI(fluidPage(title = "aaa",
titlePanel("aaa"),
sidebarLayout(
sidebarPanel(
textInput("Id", "Enter Acct Name below"),
actionButton("Acc",label="Click to send code"),
tags$hr(),
numericInput("AuthCode",label="Authentication",value=000),
actionButton("Auth",label="Submit"),
tags$hr(),
tags$hr(),
downloadButton("downloadData", "Download Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", tableOutput("tbTable"))
))
)
)
)
我正在通过比较电子邮件中发送的代码和用户提交的代码在我的应用程序中构建身份验证部分。我尝试通过在 reactive()、isolate()、renderTable() 中使用来添加 if 语句。我得到的值应该在反应部分错误中,或者应用程序根本没有响应。 以下是我在 Server.R 中的内容,应用程序完全没有响应且没有错误。
shinyServer(function(input, output, session) {
myData <- reactive({
req(input$Id)
#connect to the database, get the data, res
#send an email with random number, rnd
list(res,rnd)
})
output$tbTable <- renderTable({req(input$Auth)
if (input$AuthCode==myData()$rnd) {
myData()$res
}
else{
as.data.frame(c("Authentication Failed"))
}
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Id, " filname.xlsx", sep = "")
},
content = function(file) {
write.csv(myData(), file, row.names = FALSE)
}
)#this part need to depend on the if-statement as well
}
)
UI.R
ui <- shinyUI(fluidPage(title = "aaa",
titlePanel("aaa"),
sidebarLayout(
sidebarPanel(
textInput("Id", "Enter Acct Name below"),
submitButton(text="Submit"),
tags$hr(),
numericInput("AuthCode",label="Authentication",value=""),
actionButton("Auth",label="Submit"),
tags$hr(),
tags$hr(),
downloadButton("downloadData", "Download Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", tableOutput("tbTable"))
))
),
)
)
我想我已经准备好做你想做的事了。请检查。我做了以下更改
将您的
submitButton
替换为actionButton
作为Acc
并使用observeEvent
调用它。当单击第二个按钮时,
observeEvent
现在也会触发身份验证Excel 扩展在
write.csv
中不起作用,因此更改了扩展。
server.R
shinyServer(function(input, output, session) {
# the first button will trigger this
observeEvent(input$Acc,{
# myData <- reactive({
req(input$Id)
#connect to the database, get the data, res
#send an email with random number, rnd
#list(res,rnd)
myData <<- list(res = 123,rnd = 345) #passing test value and storing it as global variable for accessing in other functions
# })
cat("mydata done")
})
# the second button will trigger this
observeEvent(input$Auth,{
output$tbTable <- renderTable({
#req(input$Auth)
if (input$AuthCode==myData$rnd) {
myData$res
}
else{
#as.data.frame(c("Authentication Failed"))
shiny::showModal(modalDialog(
title = "Important message",
"Authentication Failed!"
))
}
})
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$Id, " filname.csv", sep = "") #renamed it to csv because write.csv writes to csv not excel
},
content = function(file) {
write.csv(myData, file, row.names = FALSE)
}
)#this part need to depend on the if-statement as well
}
)
ui.R
ui <- shinyUI(fluidPage(title = "aaa",
titlePanel("aaa"),
sidebarLayout(
sidebarPanel(
textInput("Id", "Enter Acct Name below"),
actionButton("Acc",label="Click to send code"),
tags$hr(),
numericInput("AuthCode",label="Authentication",value=000),
actionButton("Auth",label="Submit"),
tags$hr(),
tags$hr(),
downloadButton("downloadData", "Download Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", tableOutput("tbTable"))
))
)
)
)