How to dynamically rename multiple columns in R Shiny based on drop down inputs?
<p>I have a shiny app like shown below :</p>

<pre><code>require(shiny)
require(dplyr)
server &lt;-  function(input, output) {

  dataa &lt;- reactive({
    table1 &lt;- mtcars
    return(table1)
  })

  output$contents &lt;- renderDataTable({
    dataa()      
  })

  output$mpg &lt;- renderUI({
    selectizeInput(
      'MPG', 'MPG: ', choices = c("",as.list(colnames(dataa()))),
      options = list(
        placeholder = 'Please select',
        onInitialize = I('function() { this.setValue(""); }')
      )
    )
  })   

  output$cyl &lt;- renderUI({
    selectizeInput(
      'CYL', 'CYL: ', choices = c("",as.list(colnames(dataa()))),
      options = list(
        placeholder = 'Please select',
        onInitialize = I('function() { this.setValue(""); }')
      )
    )
  })  


  DataRename &lt;- reactive({
    Data &lt;- dataa()
    DataNew1&lt;-Data
    MPG &lt;- input$MPG
    CYL &lt;- input$CYL
    if(!is.null(MPG)){
      StatRenameEmp1 &lt;- paste0("DataNew1 &lt;- dplyr::rename(DataNew1,Mileage=",MPG,")")
      eval(parse(text=StatRenameEmp1))
    } else{
      DataNew1&lt;-Data
    }
    if(!is.null(CYL)){
      StatRenameEmp1 &lt;- paste0("DataNew1 &lt;- dplyr::rename(DataNew1,Cylinders=",CYL,")")
      eval(parse(text=StatRenameEmp1))
    }else{
      DataNew1&lt;-Data
    }

    return(DataNew1)
  })

  output$rename &lt;- renderDataTable({
    DataRename()      
  })


}

ui &lt;- shinyUI({
  navbarPage("Dynamic Rename",
             tabPanel("Data",
                      fluidPage(
                        titlePanel("mtcars"),

                        dataTableOutput('contents'))
                      ),
             tabPanel("Variables",
                      fluidPage(
                        sidebarLayout(
                          sidebarPanel(
                            uiOutput("mpg"),
                            uiOutput("cyl")
                          ),
                          mainPanel(
                            dataTableOutput("rename")
                          )
                        )
                      )
   )


  )



})

shinyApp(ui = ui, server = server)
</code></pre>

<p>The aim is to rename the columns of the dataframe (mtcars) where I have some standard names given to the final data and the user is supposed to select the corresponding variable from the input dataset. Eg., I want to rename a certain column selected by the user (<code>mpg</code> in this case) as "Mileage", <code>cyl</code> to "Cylinder" and so on.</p>

<p>My problem is I am not able to rename multiple columns at a go. Only the first column is being renamed not the remaining.</p>

<p>Second, I may not select any column, in that case the column name need not be changed, i.e., if MPG is not selected then it should remain as <code>mpg</code></p>

<p>How do I get all the columns renamed according to a name set by me, based on the user inputs?</p>

<p>The renaming works good if I select all dropdowns.</p>