为什么当我在 DT 数据中设置列名时我的列名没有改变并给我一个错误“'escape' 参数中的名称未找到”?

Why are my column names not changing when I set the colnames in DT datable and giving me an error that ' names in the 'escape' argument not found'?

我想更改我的列名称在我的 R Shiny App 数据表中的显示方式,但我不想更改 R 在后台对它们的响应方式。当我尝试使用数据表的 colnames 参数时,出现以下错误:

Warning: Error in convertIdx: Some column names in the 'escape' argument not found in data

从技术上讲,这是有道理的,因为我正在更改列名,但我认为这只是一种美学上的改变(这就是我想要的)。

这是一个重现错误的简单应用程序:

library(shiny)
library(dplyr)
library(DT)
ui <- fluidRow(
    column(12,
           
           DT::dataTableOutput("table")

        
    )
)

server <- function(input, output) {
    
    raw_data <- reactive({tibble(start_date = c("2020-01-01", "2020-01-09", "2020-01-30"),
                       choice = c("A", "B", "C"))
    }) #Note: I'm aware this doesn't need to be reactive here, but in my actual data, the underlying data are reactive, so I want to mirror my real world scenario here.
    
    output$table <- DT::renderDataTable({
        datatable(raw_data(),
                  colnames = c("start_date" = "Date to Start", "choice" = "Letter Options"))
    })
    

    
}

shinyApp(ui = ui, server = server)

columns中的顺序需要相反,是newname = oldname

library(shiny)
library(dplyr)
library(DT)

ui <- fluidRow(
  column(12,
         DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  
  raw_data <- reactive({tibble(start_date = c("2020-01-01", "2020-01-09", "2020-01-30"),
                               choice = c("A", "B", "C"))
  }) 
  
  output$table <- DT::renderDataTable({
    datatable(raw_data(),
              colnames = c("Date to Start" = "start_date", "Letter Options" = "choice"))
  })
}

shinyApp(ui = ui, server = server)