简体   繁体   中英

If Statement in Shiny based on values in reactive

I am building an authentication part in my app by comparing the code sent in email and the code users submit. I have tried adding if-statement by using either in reactive(),isolate(), renderTable(). I either get the value should be in the reactive part error, or the app does not respond at all. Below is what I have in Server.R , the app does not respond at all with no error.

            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"))
                    )) 
                ),
            )
            )

I think I've got the fixed to do what you want. Please check. I've done the following changes

  • Replaced your submitButton to actionButton as Acc and using observeEvent to call that.

  • The authentication is also now triggered by an observeEvent when the second button is clicked

  • Excel extension wouldn't work in write.csv so changed the extension.

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"))
                            )) 
                        )
)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM