为什么我在DT datable中设置列名时,列名没有改变,并给我一个错误“names in the 'escape' argument not found”?

zzlelutf  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(124)

我想改变我的列名在R Shiny App的datable中的显示方式,但我不想改变R在后台对它们的响应方式。当我尝试使用datatable的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)
alen0pnh

alen0pnh1#

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)

相关问题