簡體   English   中英

如何在 R 中具有多個標題行的 flextable 中設置標題標簽?

[英]How do I set header labels in a flextable with multiple header rows in R?

我在 R 中使用 flextable-function 來創建一個漂亮的平面列聯表。 我的平面列聯表有兩行列標題。 我試圖用改變這些set_header_labels在flextable包,但失敗了-功能。

下面的步驟代表了我所經歷的過程。 前五個步驟很順利。 在最后一步,我想將列標題名稱從“A”更改為“aa”,並將“B”更改為“bb”,但未能達到我想要的結果。 請注意,一開始將“A”更改為“aa”等並不能解決我的問題。 我真的需要在最后而不是在開始時做出我想要的改變!

  1. 創建一個tibble。
t <- tibble(ID = 1:10, 
header1 = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
header2 = c("No", "No", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No"),
q1 = c(1, 3, 2, 2, 3, 1, 2, 1, 1, 3))
  1. 創建一個平面列聯表。

ft <- (t %>% ftable(row.vars = c("q1"), col.vars = c("header1", "header2")))

  1. 加載函數以將平面列聯表轉換為靈活表。 資料來源: 是否有一種(簡單的)方法可以將平面列聯表(ftable)轉換為 flextable
ftable_to_flextable <- function( x ){

  row.vars = attr( x, "row.vars" )
  col.vars = attr( x, "col.vars" )
  rows <- rev(expand.grid( rev(row.vars), stringsAsFactors = FALSE ))
  cols <- rev(expand.grid( rev(col.vars), stringsAsFactors = FALSE ))

  xmat <- as.matrix(x)
  cols$col_keys = dimnames(xmat)[[2]]
  xdata <- cbind(
    data.frame(rows, stringsAsFactors = FALSE),
    data.frame(xmat, stringsAsFactors = FALSE)
  )
  names(xdata) <- c(names(row.vars), cols$col_keys)

  ft <- regulartable(xdata)
  ft <- set_header_df(ft, cols)
  ft <- theme_booktabs(ft)
  ft <- merge_v(ft, j = names(row.vars))
  ft
}
  1. 創建一個彈性表。

flext <- ftable_to_flextable(ft)

  1. 合並兩個標題標題。
flext <- merge_at(flext, i = 1, j = 2:3, part = "header")
flext <- merge_at(flext, i = 1, j = 4:5, part = "header")

運行flext現在返回:

在此處輸入圖片說明

  1. 現在,我想要更改平面列聯表的第一個標題行中的兩個標簽。

set_header_labels(flext, A = "aa", B = "bb")

我收到以下錯誤消息:

Error in `[<-`(`*tmp*`, i, j, value = value) : subscript out of bounds

這應該可以完成您的代碼的工作

# flext <- flextable::compose(flext, i = 1, j = 2, part = "header", value = as_paragraph("aa"))
# flext <- flextable::compose(flext, i = 1, j = 4, part = "header", value = as_paragraph("bb"))

如果有幫助,這就是我將如何使用通用as_flextable實現它:

library(flextable)

dat <- data.frame(
  ID = 1:10,
  header1 = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
  header2 = c("No", "No", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No"),
  q1 = c(1, 3, 2, 2, 3, 1, 2, 1, 1, 3), 
  stringsAsFactors = FALSE
)

dat$header1 <- factor(dat$header1, levels = c("A", "B"), labels = c("aa", "bb"))
dat$header2 <- factor(dat$header2, levels = c("No", "Yes"), labels = c("nope", "ok"))
ft <- ftable(dat,
  row.vars = c("q1"),
  col.vars = c("header1", "header2")
)


as_flextable.ftable <- function(x, ...) {
  row.vars <- attr(x, "row.vars")
  col.vars <- attr(x, "col.vars")
  rows <- rev(expand.grid(rev(row.vars), stringsAsFactors = FALSE))
  cols <- rev(expand.grid(rev(col.vars), stringsAsFactors = FALSE))

  xmat <- as.matrix(x)
  cols$col_keys <- dimnames(xmat)[[2]]
  xdata <- cbind(
    data.frame(rows, stringsAsFactors = FALSE),
    data.frame(xmat, stringsAsFactors = FALSE)
  )
  names(xdata) <- c(names(row.vars), cols$col_keys)

  ft <- flextable(xdata)
  ft <- set_header_df(ft, cols)
  ft <- theme_booktabs(ft)
  ft <- merge_h(ft, i = seq_len(ncol(rows)), part = "header")
  ft
}

as_flextable(ft)

靈活的

這可能需要額外的工作,但不需要那么多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM