用于不带特殊字符的条件单元格格式的 xtable

xtable for conditional cell formatting not with special characters

我正在将 xtable for conditional cell formatting significant p-values of table 的解决方案应用于我的数据集;但是,我不能按照建议的答案使用 sanitize.text.function = function(x) x

例子

数据集

数据集包含使用 _ 字符的文本列,如下例所示:

dta <- data.frame(
    varA = c("this_and_that", "something_else", "def"),
    varB = c("other_thing", "something_different", "abc")
)

xtable

当传递给 时,它使用语法:

print.xtable(x = xtable(dta), 
             booktabs = TRUE,
             comment = FALSE,
             floating = TRUE,
             type = "latex",
             include.rownames = FALSE,
             NA.string = "NA",
             # Font and linespace size
             size = "\fontsize{5pt}{5pt}\selectfont")

代码生成可用的乳胶片段:

\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
  \toprule
varA & varB \ 
  \midrule
this\_and\_that & other\_thing \ 
  something\_else & something\_different \ 
  def & abc \ 
   \bottomrule
\end{tabular}
\endgroup
\end{table}

问题

我想将 \textbf{} 应用到包含单词 something 的列。按照链接问题中讨论的答案,这可以通过 ifelsesanitize.text.function = function(x) x 完成,如下例所示:

print.xtable(
    x = xtable(dta %>%
                   mutate_all(funs(
                       ifelse(grepl("something", .), paste0("\textbf{", ., "}"), .)
                   ))),
    booktabs = TRUE,
    comment = FALSE,
    floating = TRUE,
    type = "latex",
    include.rownames = FALSE,
    NA.string = "NA",
    # Font and linespace size
    size = "\fontsize{5pt}{5pt}\selectfont",
    sanitize.text.function = function(x) {
        x
    }
)

这将生成关于 \textbf{} 的正确语法,但 _ 不会被转义,因为至:sanitize.text.function = function(x) {x}

\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
  \toprule
varA & varB \ 
  \midrule
this_and_that & other_thing \ 
  \textbf{something_else} & \textbf{something_different} \ 
  def & abc \ 
   \bottomrule
\end{tabular}
\endgroup
\end{table}

已删除 sanitize.text.function

代码:

print.xtable(
    x = xtable(dta %>%
                   mutate_all(funs(
                       ifelse(grepl("something", .), paste0("\textbf{", ., "}"), .)
                   ))),
    booktabs = TRUE,
    comment = FALSE,
    floating = TRUE,
    type = "latex",
    include.rownames = FALSE,
    NA.string = "NA",
    # Font and linespace size
    size = "\fontsize{5pt}{5pt}\selectfont"
)

将产生可怕的混乱,因为默认清理行为将尝试逃避 \textbf

\begin{table}[ht]
\centering
\begingroup\fontsize{5pt}{5pt}\selectfont
\begin{tabular}{ll}
  \toprule
varA & varB \ 
  \midrule
this\_and\_that & other\_thing \ 
  $\backslash$textbf\{something\_else\} & $\backslash$textbf\{something\_different\} \ 
  def & abc \ 
   \bottomrule
\end{tabular}
\endgroup
\end{table}

替代方法/侧点

可能更好的方法是使用清理功能:

print.xtable(
    x = xtable(dta),
    booktabs = TRUE,
    comment = FALSE,
    floating = TRUE,
    type = "latex",
    include.rownames = FALSE,
    NA.string = "NA",
    # Font and linespace size
    size = "\fontsize{5pt}{5pt}\selectfont",
    sanitize.text.function = function(x) {
        ifelse(grepl("something", x), paste0("\textbf{", x, "}"), x)
    }
)

总结

除了 greping 我的 table 中的所有特殊字符并通过将处理 both 个元素 textbf{}_ 正确吗?


工作解决方案

使用:

  sanitize.text.function = function(x) {
    gsub(fixed = TRUE, pattern = "_", replacement = "\_", 
         x = ifelse(grepl(pattern = "something", x = x), paste0("\textbf{", x, "}"), x)
         )
  }

将工作,因为它手动转义 _,理想情况下,我想找到一个解决方案,我不必手动对每个字符进行空格。

Hmisc 中有一个函数可以完成以下工作:latexTranslate。 运行 它先应用 LATEX 格式命令。

library("Hmisc")
library("xtable")
dta <- data.frame(
    varA = c("this_and_that", "something_else", "def"),
    varB = c("other_thing", "something_different", "abc")
)
print.xtable(
    x = xtable(dta),
    file=paste0(getwd(),"/table/sanitize_test.tex"),
    booktabs = TRUE,
    comment = FALSE,
    floating = TRUE,
    type = "latex",
    include.rownames = FALSE,
    NA.string = "NA",
    # Font and linespace size
    size = "\fontsize{5pt}{5pt}\selectfont",
    sanitize.text.function = function(x) {
      x <- latexTranslate(x)
      x <- ifelse(grepl("something", x), paste0("\textbf{", x, "}"), x)
      return(x)
    }
)