问题描述
我想更改我的列名称在我的 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)