I Want to edit table column names in shiny and save it for further analysis.
I have added a piece of code which allows me to edit the colnames in the table. Please see the example below.
With this simple example here. I can edit the column names by double clicking them. But I don't know how to save the edited table for further data analysis in shiny. For instance save the edited table in the edit action button.
Thanks a lot for your help.
library(shiny)
library(DT)
callback <- c(
"var colnames = table.columns().header().to$().map(function(){return this.innerHTML;}).get();",
"Shiny.onInputChange('colnames', colnames);",
"table.on('dblclick.dt', 'thead th', function(e) {",
" var $th = $(this);",
" var index = $th.index();",
" var colname = $th.text(), newcolname = colname;",
" var $input = $('<input type=\"text\">')",
" $input.val(colname);",
" $th.empty().append($input);",
" $input.on('change', function(){",
" newcolname = $input.val();",
" if(newcolname != colname){",
" $(table.column(index).header()).text(newcolname);",
" colnames[index] = newcolname;",
" Shiny.onInputChange('colnames', colnames);",
" }",
" $input.remove();",
" }).on('blur', function(){",
" $(table.column(index).header()).text(newcolname);",
" $input.remove();",
" });",
"});"
)
ui <- fluidPage(
titlePanel("Need your help"),
DTOutput("table"),
DTOutput("editedTable"),
actionButton("save", label = "save")
)
server <- function(input, output){
output$table <- renderDT({
datatable(iris[1:3,], callback = JS(callback))
}, server = FALSE)
}
shinyApp(ui, server)