3

我正在应用来自 xtable 的解决方案,用于将表格的重要 p 值格式化为我的数据集的条件单元格;但是,我不能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{}包含 word something的列。ifelse按照链接问题中讨论的答案,这可以通过sanitize.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
    }
)

这将生成关于 the 的正确语法,\\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}

替代方法/侧点

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

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)
    }
)

概括

除了 grep 表格中的所有特殊字符并通过可以正确处理两个元素的自定义清理功能手动转义它们之外,还有其他解决此问题的方法吗?textbf{}_


工作解决方案

使用:

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

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

4

1 回答 1

3

有一个功能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)
    }
)   

在此处输入图像描述

于 2017-12-09T17:07:32.580 回答