1

我正在formattable()为我的 shinyApp 使用精彩的软件包。这是我遇到的问题的简化示例:

library(formattable)

df <- data.frame(
    ID = LETTERS[1:4], 
    `2018` = c(0.5, 0.9, 0.8, 0.4), 
    n = c(88, 44, 55, 66), 
    `2019` = c(0.9, 0.8, 0.7, 0.4), 
    n = c(78, 84, 54, 25))

names(df)[2] <- '2018'
names(df)[4] <- '2019'

formattable(
  df, align = c("l", "r", "l", "r", "l"),
  list(
    `2018` = function(x) percent(x, digits = 1),
    `2019` = function(x) percent(x, digits = 1)
  )
)

在此处输入图像描述

这很好用,但我希望将列n.1命名为n. 当我将列名重命名为n.1ton并运行相同的代码来创建表时,最后一列下降...似乎只显示任何具有重复名称的列的第一列。

names(df)[5] <- "n"

formattable(
  df, align = c("l", "r", "l", "r", "l"),
  list(
    `2018` = function(x) percent(x, digits = 1),
    `2019` = function(x) percent(x, digits = 1)
  )
)

如何n在列名中同时显示表格?

4

1 回答 1

1

如果只是显示的问题,可以在第二n列的名称后面加一个空格(使用check.names = FALSE),如下:

df <- data.frame(ID     = LETTERS[1:4], 
                 `2018` = c(0.5,0.9,0.8,0.4), 
                 n      = c(88,44,55,66), 
                 `2019` = c(0.9,0.8,0.7,0.4), 
                 `n `   = c(78,84,54,25), 
                 check.names = FALSE)

df

formattable(df,
            align=c("l", "r", "l", "r", "l"),
            list(
              `2018` = function(x) percent(x, digits = 1),
              `2019` = function(x) percent(x, digits = 1)
            )
)

这将为您提供所需的输出:

输出

希望你觉得它有用。

于 2019-05-01T04:52:29.420 回答