r - 在 R Shiny 中使用 SweetAlert2

标签 r datatables shiny sweetalert sweetalert2

我正在尝试使用 SweetAlert2 弹出窗口而不是传统的 JS 弹出窗口。期待一个例子来实现,因为我是 JS 概念的新手。访问- https://sweetalert2.github.io/ 在实际场景中,我想在双击数据表行时使用这些 SweetAlert2 而不是 JS 警报。

使用 JS 弹出代码:

library(shiny)
library(shinydashboard)
library(DT)

ui <- shinyUI(

dashboardPage (
    dashboardHeader(title="Report"),
    dashboardSidebar(sidebarMenu(menuItem("Table",tabName="Table"))),
    dashboardBody(      
        tabItems(
        tabItem(tabName = "Table",
            DT::dataTableOutput("DataTable")    
        )
   ))

))

server <- shinyServer(function(input, output) {
     output$DataTable <- DT::renderDataTable({
          datatable(iris,rownames=FALSE,selection = 'single',
                    options = list(
                              searching = FALSE,ordering=FALSE,
                              dom = 'Bfrtip',
                              buttons = c('copy','excel', 'pdf', 'print', 'colvis'),
                              columnDefs = list(list(visible=FALSE, targets=c(2))
          ),
     rowCallback = JS(
          "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
          "var full_text = aData[2]",
          # Tooltip for the rows
          "$('td:eq(1)', nRow).attr('title', full_text);",
          # Showing a hand as a cursor
          "$('td:eq(1)', nRow).css('cursor','pointer');",
          "$('td:eq(1)', nRow).css('font-weight','bold');",
          "}")
      ),
      #On double Click show Alert Message
     callback = JS('
          table.on("dblclick.dt","tr", function() {
          var data=table.row(this).data();
          alert("You clicked on "+data[4]+"\'s row");})
          '))
})
})

shinyApp(ui,server)

最佳答案

我不熟悉那个 JS 库,无论如何这就是我认为应该工作的。

ui.R

首先你必须将js脚本添加到你的页面(注意路径,它应该是CDN/www/文件夹中的文件:

tags$head(HTML("<script type='text/javascript' src='path/to/sweetAlert2.js'></script>"))

服务器.R

然后在你的回调中你可以调用库函数:

callback = JS('
    table.on("dblclick.dt","tr", function() {
        var data=table.row(this).data();
        swal({
            title: "Click!",
            text: "You clicked on "+data[4]+"\'s row",
            type: "success",
            confirmButtonText: "Cool"
        })
    )')

希望对你有帮助

关于r - 在 R Shiny 中使用 SweetAlert2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203927/

相关文章:

javascript - jquery 数据表中第 0 行请求未知参数 1

r - 使用 Shiny 的包上传数据、更改数据框和下载结果

r - 如何在 R 中发送推文?

r - 在 r 中的轴上使用表达式(绘图)

javascript - 独特的 DataTable 列标题工具提示

database - R Shiny 的数据库管理

R Shiny 显示 checkboxGroupInput 水平

r - 查找一对文档之间的重叠

r - 如何解决打开 ".rattle"文件时出现的以下错误? "Error in .RGtkCall("S_gtk_file_chooser_dialog_new_with_backend

javascript - 数据表中数组变量的粒度操作