在闪亮的应用程序中取消选择列或编辑数据表时,下载按钮消失 [英] download button disappear when deselecting column or editing data table in shiny app
问题描述
我构建了一个闪亮的应用程序,用于下载自定义和可编辑的数据表。这里以 iris
数据集为例。
I built a shiny app for downloading customized and editable data table. Here I use iris
dataset as an example.
According to this post, I add a button to download the whole dataset as csv.
但是,出现了一个问题。当我尝试取消选中某些列或编辑表时,下载按钮便消失了。而且它再也不会出现。
However, one issue came up. When I tried to uncheck some column OR edit table, the download button simply disappear. And it never show up again.
我花了数小时试图弄清楚,但没有成功。
有人知道为什么会这样吗?非常感谢。
I spend hours trying to figure it out but was unsuccessful. Does anyone know why that happens? Thanks a lot in advance.
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)
推荐答案
非常有趣的情况。
每次编辑单元格或选择/取消选择列时,这都会更改 df $ dat
,然后重新呈现表。但是随后表中包含的元素#download1
在DOM中不再存在。
Each time you edit a cell or select/unselect a column, this changes df$dat
, and then the table is rerendered. But then the element #download1
which was included in the table does not exist anymore in the DOM.
找到一种方法来选择/取消选择某些列并编辑某些单元格而无需重新呈现表。这里是一个:
We have to find a way to select/unselect some columns and to edit some cells without rerendering the table. Here is one:
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)
这篇关于在闪亮的应用程序中取消选择列或编辑数据表时,下载按钮消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!