我在RPubs上找到了一个关于数据验证的很好的解释:https://rpubs.com/techanswers88/validate-data-in-r.我试着在R笔记本上重现这些步骤.在我拿到结果之前,所有的步骤都运行得很好.该示例使用Flextable将验证示例数据集时出现的错误制作成表格.当我编织笔记本预览:flextable in Notebook preview时,柔性显示得很好.然而,当我针织成PDF格式时,同样的柔性件看起来很糟糕:Flextable in PDF.所以我go 了kable/kableExtra,因为我更熟悉那里的包,我用这些包来设计我的大多数桌子.Kable/kableExtra通常可以很好地与R笔记本和PDF配合使用.有了Kable/KableExtra,我得到了一张我更喜欢的更紧凑的桌子.但是,该表中的一些验证标准包含管道字符(例如"(DBP==0|SBP==0)&;结果==‘存活’)".管道字符在表格中显示为‘&;#124;’.只要我通过KableExtra的样式选项传递表设置,就会发生这种情况.
在下面的MWE中,我省略了我试图重现的原始示例的图形,而只专注于麻烦的表格. 使用的R版本:4.3.2;dplyr 1.1.4;Flextable 0.9.4;kableExtra 1.4.0;验证1.1.5.
MWe:
---
title: "kableExtra vs flextable"
output:
pdf_document:
toc: true
number_sections: true
html_notebook:
toc: true
number_sections: true
---
```{r}
#Load Libraries
library(dplyr)
library(validate)
library(flextable)
library(kableExtra)
```
```{r}
#Create dataset (from https://rpubs.com/techanswers88/validate-data-in-r)
myData <- data.frame(PatientID = c('P001', 'P002', 'P003' , NA, 'P005', 'P006', 'P007', 'P008',
'P009', 'P010', 'P008', 'P11'),
Age = c(23, 12, 5, 8, 245, NA, 23, 45, 87, 121, 56, 130),
Outcome = c('Died', 'Died', NA, 'Survived', 'Survived', 'Survived', 'Survived',
'Survived', 'Survived', '?', 'Survived', 'Unknown'),
SBP = c(0, 0, 120, 80, 45, 67, 100, 130, 350, 120, 46, 120),
DBP = c(0, 0, 80, 70, 30, 40, 80, 210, NA, 80, 0, 80)
)
#Add an id field in the data.
myData <- myData%>%
dplyr::mutate(id = row_number())
```
```{r}
#Create validation rules
myrules <- validator( "Patient Id Unique" = is_unique(PatientID),
"Patient ID" = is.na(PatientID),
"Age in range" = Age >= 0 & Age <= 120,
"Outcome validity " = Outcome %in% c('Survived', 'Died'),
"SBP in range" = SBP >= 0 & SBP <= 300,
"DBP in range" = in_range (DBP, min = 0 , max = 200),
"Survived with No BP" = (DBP == 0 | SBP ==0) & Outcome == 'Survived' )
```
```{r}
#create validation results with the confront function
output <- confront(myData, myrules, key ="id")
```
```{r}
#Show errors in table
#Convert our output into a dataframe first
dout <- as.data.frame(output)
```
```{r}
#Errors for each record id
dErrors <- dout%>%
dplyr::filter(! value == TRUE)%>%
dplyr::select(id, name, expression)%>%
dplyr::arrange(id)
#Display validation results in flextable
#This part works well in a R Notebook or HTML, but not in PDF.
ft <- flextable(dErrors)%>%
theme_booktabs()%>%
merge_v(j = ~id)%>%
set_header_labels(name = "Error",
id = "Record ID",
expression = "Validation expression")
ft
```
```{r}
#Try to reproduce the flextable with kable/kableExtra
#This produces a more compact table but unfortunately without the pipe character
dout %>%
filter(!value==TRUE) %>%
select(id, name, expression) %>%
arrange(id) %>%
kable(booktabs = TRUE, escape = TRUE) %>%
kable_classic(full_width = FALSE) %>%
column_spec(3, width = "10cm") %>%
collapse_rows(columns = 1, valign = "top")
```
我发现如果我只通过kable而不是kableExtra发送结果
```{r}
dout %>%
filter(!value==TRUE) %>%
select(id, name, expression) %>%
arrange(id) %>%
kable(booktabs = TRUE, escape = TRUE)
```
则管道符号将显示在表中.但是,只要我try 执行任何样式设置(例如CLUSPLE_ROWS()或KABLE_STYLLING),管道符号就会更改为‘&;#124;’. 将"Format="LaTeX"传递给kable并不能解决问题,也不能解决问题.正如前面所说的,在我添加任何样式之前,管道字符将显示在PDF中的表格中. 我在网上或kableExtra的手册中都找不到解决方案.我已经try 更新R和包,但没有结果.
我试着"修复"弹性桌子,看看我能不能做到这一点,让桌子看起来更好看,但没有成功.我已try 将弹性工作台的宽度更改为ft <- fit_to_width(ft, max_width = 15, unit = "cm")
.然而,这不会对PDF中的可伸缩宽度做任何事情,所有文本都会被打碎在一起.一些在线建议建议在YAML头文件中使用‘LATEX_ENGINE:xelatex’,但这也没有任何效果.
我更喜欢使用kableExtra,因为我更熟悉那个包. 在使用kableExtra时如何显示管道字符有什么建议吗?
先谢谢你.