繁体   English   中英

如果基于反应中的值的Shiny语句

[英]If Statement in Shiny based on values in reactive

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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