1

Rmarkdown 文件中的 R 代码:

col1 <- c("dummydata","dummydata","dummydata")
col2 <- c("dummydata","dummydata","dummydata")  
col3 <- c("dummydata","dummydata","dummydata")
col4 <- c("dummydata","dummydata","dummydata")
col5 <- c("dummydata","dummydata","dummydata")
col6 <- c("dummydata","dummydata","dummydata")
col7 <- c("dummydata","dummydata","dummydata")
col8 <- c("dummydata","dummydata","dummydata")

df1 <- data.frame(col1,col2,col3,col4,col5,col6,col7,col8)

kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))

输出:FlexDashBoard Kable - 数据框作为表格

问题:我只想要组合列上的标题,即 Group1、Group2 等。我想删除单个列上的标题,即 col1、col2 等。

有没有办法使用数据框、html/javascript、Rmarkdown 或任何 R 包来做到这一点?

4

1 回答 1

1

如果您经常遇到此类问题,您应该熟悉正则表达式和 gsub 函数。

这是一个解决方案,第一行是您的最后一行代码,由“x <-”增强

x <- kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))

gsub("</th></tr><tr>.*</thead>","</thead>",x)

gsub 的工作方式如下:查找第一个参数的匹配项,将其替换为第二个参数,并对作为第三个参数提供的变量执行所有操作。第一个参数中的点星 (.*) 表示任何类型和数量的字符都可以在第三个参数中的结束之前跟随。该算法是贪婪的,它试图找到最长的匹配字符串。由于此输入参数中只有一个,因此在这里可以正常工作。

于 2017-06-26T04:17:56.103 回答