I'm using datatable function from DT package in R Shiny and I want that the user of my app can edit the column names (the variable names). Is there any option to do that?
For now I'm using a text input "old_var_name", a text input "new_var_name" and an actionbutton "update_variable_name". But at this point, I'm only able to change on variable name at the time. I want the user to be able to change as much as variable names he wants.
Server:
tab <- eventReactive(input$import,{
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tabledata <- read.xlsx(inFile$datapath,startRow=1,sheet = 1)
})
name_temp <- eventReactive(input$var_name,{
if (input$old_name == ""){
colnames(tab())
} else {
c(colnames(tab())[1:(which(colnames(tab()) == input$old_name)-1)],input$new_name,
colnames(tab())[(which(colnames(tab()) == input$old_name)+1):length(colnames(tab()))])
}
})
final_rename <- reactive({
d <- tab()
colnames(d) <- name_temp()
d
})
output$tabledata <- DT::renderDataTable({
if (input$var_name == 0) {
DT::datatable(tab(),editable = T)
} else {
DT::datatable(final_rename(),editable = T)
}
})
UI:
tabPanel("Table",h1("Table",align="center") ,
actionButton(inputId = "import", label = "Import data"),br(),br(),
splitLayout(textInput(inputId = "old_name", label = "Old variable name"),
textInput(inputId = "new_name", label = "New variable Name")),
actionButton(inputId = "var_name", label = "Update Variable name"),br(),br(),
DT::dataTableOutput("tabledata"))
Is there any suggestion to achieve that or any option with datatable that I can use and then the user will be able to change all variable names he wants?