在闪亮的应用程序中取消选择列或编辑数据 table 时,下载按钮消失

download button disappear when deselecting column or editing data table in shiny app

我构建了一个闪亮的应用程序来下载自定义和 editable 数据 table。这里我以 iris 数据集为例。

根据这个 post,我添加了一个按钮来将整个数据集下载为 csv。

但是,出现了一个问题。当我试图取消选中某些列或编辑 table 时,下载按钮就消失了。它再也不会出现了。

我花了好几个小时想弄明白,但没有成功。 有谁知道为什么会这样?非常感谢。

library(shiny)
library(DT)
library(dplyr)


    # UI
    ui = fluidPage(
                   downloadButton("download1","Download iris as csv"),
                   DT::dataTableOutput('tbl'),
                   checkboxGroupInput('datacols', 
                                      label='Select Columns:',
                                      choices= c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
                                      selected = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
                                      inline=TRUE )

                   )

    # SERVER
    server = function(input, output) {



        df = reactiveValues()

        observe ({

            df$dat = iris %>% select(one_of(input$datacols))
        })
        # render DT
        output$tbl = renderDT({
                datatable(df$dat,
                editable = "cell",
                callback = JS("$('div.dwnld').append($('#download1'));"),
                extensions = "Buttons",
                options = list(
                    dom = 'B<"dwnld">frtip',
                    buttons = list(
                        "copy" ) ) )

        })


        observeEvent(input[["tbl_cell_edit"]], {
            cellinfo <- input[["tbl_cell_edit"]]
            df$dat  <- editData(df$dat,  input[["tbl_cell_edit"]] )
        })

        output$download1 <- downloadHandler(
            filename = function() {
                paste("data-", Sys.Date(), ".csv", sep="")
            },
            content = function(file) {
                write.csv(df$dat, file)
            }
        )

    }

shinyApp(ui, server)

非常有趣的案例。

每次编辑单元格或 select/unselect 列时,都会更改 df$dat,然后 table 会重新呈现。但是 table 中包含的元素 #download1 在 DOM.

中不再存在

我们必须找到一种方法来 select/unselect 某些列并编辑某些单元格而不重新呈现 table。这是一个:

library(shiny)
library(DT)
library(dplyr)

# UI
ui = fluidPage(
  downloadButton("download1", "Download iris as csv"),
  DTOutput('tbl'),
  checkboxGroupInput(
    'datacols', 
    label='Select Columns:',
    choices= c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species'),
    selected = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species'),
    inline=TRUE)

)

# SERVER
server = function(input, output) {

  dat <- iris

  # render DT
  output$tbl = renderDT({
    datatable(dat,
              editable = "cell",
              callback = JS(
                "$('div.dwnld').append($('#download1'));",
                "var checkboxes = $('input[name=datacols]');",
                "checkboxes.each(function(index,value){",
                "  var column = table.column(index+1);",
                "  $(this).on('click', function(){",
                "    if($(this).prop('checked')){",
                "      column.visible(true);",
                "    }else{",
                "      column.visible(false);",
                "    }",
                "  });",
                "});"
              ),
              extensions = "Buttons",
              options = list(
                dom = 'B<"dwnld">frtip',
                buttons = list("copy")
              ) 
    )

  })

  observeEvent(input[["tbl_cell_edit"]], {
    cellinfo <- input[["tbl_cell_edit"]]
    dat <<- editData(dat, cellinfo, "tbl")
  })

  output$download1 <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(dat %>% select(one_of(input$datacols)), file)
    }
  )

}

shinyApp(ui, server)