0

我正在通过比较电子邮件中发送的代码和用户提交的代码在我的应用程序中构建身份验证部分。我已经尝试通过使用 react()、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 <- 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"))
                    )) 
                ),
            )
            )
4

1 回答 1

0

我想我有固定的做你想做的事。请检查。我做了以下更改

  • 替换了你的submitButtonto actionButtonasAcc和 usingobserveEvent来调用它。

  • 身份验证现在也由observeEvent单击第二个按钮时触发

  • Excel 扩展无法正常工作,write.csv因此更改了扩展。

服务器.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 <- 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"))
                            )) 
                        )
)
)
于 2018-09-07T15:16:51.067 回答