在下面的rmarkdown文件中,我尝试使用折叠行。在这种情况下,只有1行,但在其他情况下,将超过1行。
运行文件时,您将看到一个星号,然后显示数字.02 *。当您注释掉行“%>%crash_rows(columns = 1,latex_hline =” major“,valign =” middle“)”时,星号消失了,因此与塌陷行有关。考虑到我想保留折叠行代码,如何解决此问题,因为在其他情况下,行数超过了1行并且可以正常工作。我尝试使用if语句有条件地进行排除,但那没有用。
---
author:
- ""
classoption: landscape
output:
pdf_document:
number_sections: false
dev: pdf
keep_tex: no
toc: yes
toc_depth: 3
fig.pos: H
fig.width: 11in
fontsize: 9pt
geometry: "left=1cm,right=1cm,top=1cm,bottom=1cm"
fig.lp: 'Fig '
setspace: singlespacing
always_allow_html: yes
header-includes:
- \usepackage{amsmath}
- \usepackage{rotating}
- \usepackage{float}
- \usepackage{longtable}
- \usepackage{tabulary}
- \usepackage{array}
- \usepackage{colortbl}
- \usepackage{graphicx}
- \usepackage{booktabs}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r test}
library(kableExtra)
library(dplyr)
d = data.frame(g = c("A"),v = c(.02))%>%
dplyr::mutate(
v = cell_spec(v,
color = ifelse(is.na(v),"black", ifelse( abs(v) >= 1, "red", "black")),
bold= ifelse(is.na(v),FALSE, ifelse( abs(v) >= .1, TRUE, FALSE)) )
)%>%
dplyr::rename("test"="v" )
kable(
d,
format ="latex",
caption = "test",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>% kable_styling(latex_options = c(
"repeat_header"
),
font_size=8) %>% collapse_rows(columns = 1, latex_hline = "major", valign = "middle")
#%>% {if(nrow(d)>1) collapse_rows(columns = 1, latex_hline = "major", valign = "middle")} #this does not work
```
答案 0 :(得分:0)
这是一种解决方法:
.... {if(nrow(d)>1) collapse_rows(columns = 1, latex_hline = "major", valign = "middle") else . } %>%