问题
Here is an example shiny app that allows user to download editable table. User can click on the csv
button on the upper left corner to download the table.
However, I found after I edit any cell in the table (by double click on any cell and modify contents), when I click on the csv
button, enter file name and save, I got warning message like below:
dataTables warning: table id=DataTables_Table_3 - invalid json response.
For more information about this error, please see http://datatables.net/tn/1
Although I was still able to save the table as csv file, the warning message is very annoying.
This only happens after I add the argument server=FALSE
in the renderDT
function. The reason I need server=FALSE
is without this, the app only download the first page of the folder (missing all the rest of data after saving).
Another problem is after I edit a cell, if I check/uncheck some columns, the editted cell goes back to its original value.
Does anyone know how to fix these issues?
Thanks a lot.
The example shiny app is below:
library(shiny)
library(DT)
library(dplyr)
shinyApp(
# UI
ui = fluidPage(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(server=FALSE, {
datatable(df$dat,
editable = "cell",
extensions = "Buttons",
options = list(
dom = "Bfrtip", buttons = list("csv")))
})
observeEvent(input[["tbl_cell_edit"]], {
cellinfo <- input[["tbl_cell_edit"]]
df$dat <- editData(df$dat, input[["tbl_cell_edit"]], "tbl")
})
}
)
回答1:
That's because you're using the proxy with server = FALSE
. You should not. Do
observeEvent(input[["tbl_cell_edit"]], {
cellinfo <- input[["tbl_cell_edit"]]
df$dat <- editData(df$dat, input[["tbl_cell_edit"]])
})
and not
observeEvent(input[["tbl_cell_edit"]], {
cellinfo <- input[["tbl_cell_edit"]]
df$dat <- editData(df$dat, input[["tbl_cell_edit"]], "tbl")
})
If you want to download the whole table with server = TRUE
, see this discussion.
来源:https://stackoverflow.com/questions/58176637/download-edited-data-table-gives-warning-in-shiny-app