简体   繁体   English

R Shiny SweetAlert - 根据用户输入显示警报

[英]R Shiny SweetAlert - Display alert depending on user input

I would like to send a SweetAlert to the app user depending on correct password input and file upload.我想根据正确的密码输入和文件上传向应用用户发送 SweetAlert。

My app asks a user to upload a password protected Excel file using the fileInput widget and accepts a password using the passwordInput widget:我的应用程序要求用户使用fileInput小部件上传受密码保护的 Excel 文件,并使用passwordInput小部件接受密码:

  # file select
  output$file_select <- renderUI({
    fileInput("file_select","Import File:",buttonLabel = "Select",
              multiple = FALSE, accept = ".xlsx",placeholder = "No file selected")
  })
  # password input
  output$pword_input <-renderUI({
    passwordInput("pword_input","Password:", "", placeholder = "Enter Excel password")
  })

Reading of the Excel sheet is handled by excel.link and creates a reactive object called raw.data() : Excel 表的读取由excel.link处理,并创建一个名为raw.data()的反应性 object :

  raw.data <- reactive({
    # Read Excel file
    data_file <- input$file_select
    df <- xl.read.file(data_file$datapath,
                       password =  input$pword_input)
   return(df)
})

Documentation for sendSweetAlert only seems to provide examples for sending an alert on action button press. sendSweetAlert的文档似乎只提供了在按下操作按钮时发送警报的示例。 However, I would like the observeEvent to look for correct password input.但是,我希望observeEvent寻找正确的密码输入。

I cannot store the password within the app and so I have currently set the observeEvent that triggers the SweetAlert to look for the creation of the reactive object raw.data() as an indicator that the correct password has been input:我无法将密码存储在应用程序中,因此我当前设置了触发observeEvent的 observeEvent 以查找反应式 object raw.data()的创建,作为输入正确密码的指示:

  observeEvent(raw.data(), {
    sendSweetAlert(
      session = session,
      title = "Password Accepted",
      type = "success"
    )
  })

This functions, to a certain extent.这在一定程度上起作用。 If the correct password is input then the SweetAlert is displayed once the reactive object is created as desired.如果输入了正确的密码,那么一旦根据需要创建了反应式 object,就会显示 SweetAlert。 However, input of incorrect password crashes the app.但是,输入错误的密码会使应用程序崩溃。

I hoped someone could help with the correct way to set this up please.我希望有人可以帮助以正确的方式进行设置。 I imagine my error is in the use of the observeEvent?我想我的错误是在使用observeEvent? However, without storing the password, i'm not sure how else to achieve my aim.但是,如果不存储密码,我不确定如何实现我的目标。

Thanks in advance提前致谢

xl.read.file throws an error if the password is not correct.如果密码不正确, xl.read.file会引发错误。 So you can try something like that (not tested):所以你可以尝试这样的事情(未经测试):

raw.data <- reactive({
    # Read Excel file
    data_file <- input$file_select
    df <- try(xl.read.file(data_file$datapath,
                           password = input$pword_input))
   return(df)
})

observeEvent(raw.data(), {
  if(inherits(raw.data(), "try-error")) {
    sendSweetAlert(
      session = session,
      title = "Wrong password",
      type = "error"
    )
  } else {
    sendSweetAlert(
      session = session,
      title = "Password Accepted",
      type = "success"
    )
  }
})

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

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