Home > OS >  change the name of the column of the dataset within the shiny
change the name of the column of the dataset within the shiny

Time:05-23

I would like to have the ability to change the column name of the dataset within the shiny app through "colnames(data) <- c()". Is there any way to do that? Here I have a reproducible shiny with a test dataset. could we use "textAreaInput()" to change the names of the column or there is a better way? Thanks for all your help.

ui.R

library(ggplot2)
library(shiny)

ui<-fluidPage(
  titlePanel("Basic DataTable"),
  
  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
           selectInput("man",
                       "Manufacturer:",
                       c("All",
                         unique(as.character(mpg$manufacturer))))
    ),
    column(4,
           selectInput("trans",
                       "Transmission:",
                       c("All",
                         unique(as.character(mpg$trans))))
    ),
    column(4,
           selectInput("cyl",
                       "Cylinders:",
                       c("All",
                         unique(as.character(mpg$cyl))))
    ),
    column(4,
           textAreaInput("modify_code", "change the column names: ", 
                         "colnames(data) <- c()", width="600px")
           
           ),
  ),
  DT::dataTableOutput("table")
)

server.R

server<-function(input, output) {
  
  # Filter data based on selections
  output$table <- DT::renderDataTable(DT::datatable({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    data
  }))
  
}


shinyApp(ui, server)

CodePudding user response:

If your heart is set on using textAreaInput, you could get your names from input$modify_code, split it into a vector, new_col_names <- unlist(strsplit(input$modify_code," ")), assuming each new column name is a single word (or use a separator). Then pass that to datatable(data, colnames = new_col_names) in the server side. The number of words you enter has to match the number of columns you're trying to rename. Is there a reason why you want the user to change the column names? You could rename at the server side when building your tables.

EDITED sever side

server<-function(input, output) {
  
  # Filter data based on selections
  output$table <- DT::renderDT({
    data <- mpg
    if (input$man != "All") {
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All") {
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All") {
      data <- data[data$trans == input$trans,]
    }
    
    ifelse(isolate(input$modify_code) == "",
           col_names <- names(data),
           col_names <- unlist(strsplit(isolate(input$modify_code),"|",fixed=T)))
    
    DT::datatable(data, colnames = col_names)
  })
  
}

No changes in the UI side. You need to enter exactly 11 names separated by | a bar. Like this one|two|three|four|five|six|seven|eight|nine|ten|eleven.

What you enter in the textAreaInput is on hold until you change one of the dropdowns, otherwise it will try to change the column names as you type. You may want to put a submit button instead. This is just quick and dirty to get it working. It works, just tested it.

EDITED: Forgot to put the second isolate...

  • Related