【问题标题】:If Statement in Shiny based on values in reactive如果基于反应值的 Shiny 语句
【发布时间】:2019-02-12 21:34:06
【问题描述】:

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

【问题讨论】:

  • 你为什么用这个表达as.data.frame(c("Authentication Failed"))
  • 我不太清楚 Shiny 中的数据结构。作为 res,输出是一个数据帧,所以我将此字符串转换为数据帧以避免不匹配。如果是问题,请欣赏有关不利影响的任何意见。
  • 如果有一个模态框说身份验证失败,你还好吗?
  • 是的,当然。
  • 为什么会有第一个提交按钮,账户和验证码不能是一个按钮吗?

标签: r shiny shiny-server shiny-reactivity


【解决方案1】:

我想我已经决定做你想做的事了。请检查。我做了以下改动

  • 将您的 submitButton 替换为 actionButtonAcc 并使用 observeEvent 调用它。

  • 现在,当点击第二个按钮时,身份验证也由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.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"))
                            )) 
                        )
)
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2020-12-20
    • 2015-12-21
    • 1970-01-01
    • 2018-02-15
    • 2020-03-14
    • 2014-06-16
    相关资源
    最近更新 更多