简体   繁体   English

在 R 闪亮应用程序中的数据表中的字段名称上方添加附加标签

[英]Adding a additional labels above field names in datatable in R shiny app

I am attempting to add multiple additional labels above field names in a grouped datatable in R, which will then be rendered as a datatable in a Shiny app.我试图在 R 中的分组数据表中的字段名称上方添加多个附加标签,然后将其呈现为 Shiny 应用程序中的数据表。

For example, using iris data, I would like each species to be grouped, then have their Sepal Lengths be summarized (max is longest, min is shortest, etc.), along with their Sepal Widths, Petal Lengths, and Petal Widths.例如,使用鸢尾花数据,我希望对每个物种进行分组,然后汇总它们的萼片长度(最大值是最长的,最小值是最短的等),以及它们的萼片宽度、花瓣长度和花瓣宽度。 In this example, the additional labels added would be "Sepal Length", "Sepal Width", "Petal Length", and "Petal Width" above their respective Max/Avg/Min sub columns.在此示例中,添加的附加标签将是“Sepal Length”、“Sepal Width”、“Petal Length”和“Petal Width”,位于其各自的 Max/Avg/Min 子列上方。 The image here is a Microsoft Excel recreation of what I am looking for.这里的图像是我正在寻找的 Microsoft Excel 娱乐

This is a link to a possible solution , but I have had no luck trying to replicate this for multiple labels. 这是一个可能的解决方案的链接,但我没有尝试为多个标签复制它。 Here is my code attempting to recreate the solution in the above link, using the iris data.这是我尝试使用虹膜数据重新创建上述链接中的解决方案的代码。

df3<-data.frame(iris)

grouped_df3<- df3 %>%
  group_by(Species) %>%
  summarize("#"=n(),
            Max_Sepal_W=max(Sepal.Width, na.rm = TRUE),
            Avg_Sepal_W=mean(Sepal.Width, na.rm = TRUE),
            Min_Sepal_W=min(Sepal.Width, na.rm = TRUE),
            Max_Sepal_L=max(Sepal.Length, na.rm = TRUE),
            Avg_Sepal_L=mean(Sepal.Length, na.rm = TRUE),
            Min_Sepal_L=min(Sepal.Length, na.rm = TRUE) )


class(grouped_df3) <- c( "Question", class(grouped_df3) )

print.Question <- function( x, ... ) {
  if( ! is.null( attr(x, "Question") ) ) {
    cat("Question:", attr(x, "Question"), "\n")
  }
  print.data.frame(x)
}
attr(grouped_df3, "Question") <- "Age"
attributes(grouped_df3)
head(grouped_df3)

# found here: https://stackoverflow.com/questions/9274548/add-a-row-above-row-headers-in-r
# does not work with datatables, or with multiple labels

Please let me know if additional info is needed.如果需要其他信息,请告诉我。 Thank you in advance for any help!预先感谢您的任何帮助!

This answer and this answer were very helpful in providing an approach using DT which could be incorporated into your shiny app.这个答案和这个答案对于提供一种使用DT的方法非常有帮助,该方法可以合并到您的闪亮应用程序中。

library(htmltools)
library(DT)

sketch <- withTags(
  table(
    class = "display",
    thead(
      tr(
        th(colspan = 1, "", style = "border-right: solid 2px;"),
        th(colspan = 1, "", style = "border-right: solid 2px;"),
        th(colspan = 3, "Sepal Width", style = "border-right: solid 2px;"),
        th(colspan = 3, "Sepal Length")
      ),
      tr(
        th("Species", style = "border-right: solid 2px;"),
        th("#", style = "border-right: solid 2px;"),
        th("Longest"),
        th("Avg"),
        th("Shortest", style = "border-right: solid 2px;"),
        th("Widest"),
        th("Avg"),
        th("Narrowest"),
      )
    )
  )
)

datatable(grouped_df3, rownames = FALSE, container = sketch, 
          options = list(
            columnDefs = list(
              list(targets = "_all", className = "dt-center")
            )
          )) %>%
  formatStyle(c(1,2,5), `border-right` = "solid 2px")

Output输出

具有复杂标题的闪亮应用程序的数据表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM