download button disappear after editing data table in shiny app

蹲街弑〆低调 提交于 2019-12-11 14:29:54

问题


I have included editable table in my shiny app I developed in my organization. Use this simple example to illustrate the issue. This is an extension of this question

In this app, after I edit any cell, the download button just disappear. Does anyone know why that happens? Thanks a lot in advance.

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

# UI
ui = fluidPage(
    selectInput("nrows",
                "select n entries",
                choices = 100:150,
                selected = 100,
                multiple = FALSE),
    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 = reactiveValues()
    observe ({

        dat$dat = iris[1:input$nrows, ]

    })
    # render DT
    output$tbl = renderDT({
        datatable(dat$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$dat <<- editData(dat$dat, cellinfo, "tbl")
    })

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

}

shinyApp(ui, server)

来源:https://stackoverflow.com/questions/58194795/download-button-disappear-after-editing-data-table-in-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!