问题
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