微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用sweetalertR保存前确认消息

如何解决使用sweetalertR保存前确认消息

我使用sweetalertR 包在UI 中创建确认消息(使用这个包的原因:它看起来非常好,而且消息是高度可定制的)。 但是,我需要在服务器函数中实现一些代码,以便只有在确认消息已被批准时才会将文件保存到磁盘。目前,它已在单击保存按钮时保存文件

问题:sweetalert 函数似乎没有 inputID 参数!

library(shiny) #1.5.0
library(shinydashboard) #0.7.1
library(rhandsontable) #0.3.7
# remotes::install_github("timelyportfolio/sweetalertR")
library(sweetalertR) #0.2.0
library("xlsx") #0.6.5

shinyApp(
  ui = fluidPage(
    
    Box(width = 12,# Save button
        actionButton(inputId = "saveBtn","Save"),br(),# Editable table
        rHandsontableOutput("submit_data_edit_table"),# Confirmation button,see: http://www.buildingwidgets.com/blog/2015/6/29/week-25-sweetalertr
        sweetalert(selector = "#saveBtn",text = 'Changes will be saved in a new excel file',title = 'Confirm changes',type = "warning",allowOutsideClick = TRUE,showCancelButton = TRUE,cancelButtonText = 'Cancel',confirmButtonText = 'Confirm',confirmButtonColor = "darkred",cloSEOnConfirm = FALSE,evalFunction = 'function(){swal("Saved","Restart the application","success")}'
        )
        
    )
    
  ),server = function(input,output,session) {
    
    # Create a table that can be modified
    output$submit_data_edit_table = renderRHandsontable({
      if (!is.null(input$submit_data_edit_table)) {
        DF = hot_to_r(input$submit_data_edit_table)
      } else {
        DF = iris
      }
      
      rhandsontable(DF,height = 750
      ) %>%
        hot_table(highlightCol = TRUE,highlightRow = TRUE)
    })
    
    # Save file based on button press
    observe({
      
      # Save button
      input$saveBtn
      
      submit_data_edit_table = isolate(input$submit_data_edit_table)
      if (!is.null(submit_data_edit_table)) {
        
        # Save table as Excel file
        write.xlsx(hot_to_r(input$submit_data_edit_table),file = "newData.xlsx",sheetName = "Tot",col.names = TRUE,row.names = FALSE,append = FALSE)
        
      }
      
    })
    
  }
)

解决方法

我找到了一个解决方案:改用shinyalert。它提供相同的功能和设计,但文档更完善。

我在服务器函数中包含了以下代码:

  # Create global variable for confirmation message response
  global <- reactiveValues(response = FALSE)
  
  # Update file after table change
  observeEvent(input$saveBtn,{
    
    # Trigger confirmation dialog
    shinyalert(title = "Confirm changes",text = "Changes will be saved in a new excel file",type = "warning",closeOnClickOutside = TRUE,showCancelButton = TRUE,cancelButtonText = 'Cancel',showConfirmButton = TRUE,confirmButtonText = 'Confirm',confirmButtonCol = "darkred",timer = 15000,# 15 seconds
               callbackR = function(x) { 
                 global$response <- x
                 shinyalert(title = "Saved",text = "Restart the application",type = "success")
               }
    )
    
    print(global$response)
    
    observe({
      req(input$shinyalert)
      
      if (!global$response == "FALSE") {
        
        submit_data_edit_table = isolate(input$submit_data_edit_table)
        
        if (!is.null(submit_data_edit_table)) {
          
          # Save as Excel file
          write.xlsx(hot_to_r(input$submit_data_edit_table),file = "newData.xlsx",sheetName = "Tot",col.names = TRUE,row.names = FALSE,append = FALSE)
        }
    
        # Reset value
        global$response <- "FALSE"
    
      } # End of confirmation button if
      
    }) # End of observe
    
  }) # End of observeEvent

在用户界面中,您只需要设置shinyalert:

# Set up shinyalert to create confirmation messages
  useShinyalert(),

别忘了先加载包!

library(shinyalert)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。