You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PDF的kableExtra表格中显示竖线(pipe)字符?

问题:kableExtra处理含竖线的表格时PDF输出异常

问题背景

我复现了一篇数据验证教程的步骤,使用flextable展示验证错误时,在R Notebook或HTML预览中显示正常,但编织为PDF时效果极差。于是转向kable/kableExtra生成紧凑表格,却发现验证规则中的竖线(|)字符(例如表达式(DBP == 0 | SBP ==0) & Outcome == 'Survived'),在经过kableExtra样式处理后,PDF中会显示为|。

使用环境版本

  • R 4.3.2
  • dplyr 1.1.4
  • Flextable 0.9.4
  • kableExtra 1.4.0
  • validate 1.1.5

最小可复现示例(MWE)

---
title: "kableExtra vs flextable"
output:
   pdf_document:
     toc: true
     number_sections: true
   html_notebook:
     toc: true
     number_sections: true
---
# Load Libraries
library(dplyr)
library(validate)
library(flextable)
library(kableExtra)
# Create dataset
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())
# 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' )
# Create validation results with the confront function
output <- confront(myData, myrules, key  ="id")
# Show errors in table
# Convert our output into a dataframe first
dout <- as.data.frame(output)
# 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
# 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处理时,竖线符号能正常显示在PDF表格中:
    dout %>% 
      filter(!value==TRUE) %>% 
      select(id, name, expression) %>% 
      arrange(id) %>% 
      kable(booktabs = TRUE, escape = TRUE)
    
  • 但只要添加任何kableExtra样式(如collapse_rows()或kable_styling()),竖线符号就会变为&#124;
  • 尝试过向kable传入format = 'latex'或escape = TRUE,均无法解决问题;更新R及相关包也无效

其他尝试(未解决)

尝试修复flextable的PDF显示问题:

  • 使用ft <- fit_to_width(ft, max_width = 15, unit = "cm")调整宽度,对PDF中的表格无影响,文本仍拥挤
  • 在YAML头部指定latex_engine: xelatex,也无效果

需求

我更倾向于使用kableExtra,请问如何在使用kableExtra时让竖线(|)字符在PDF中正常显示?


内容的提问来源于stack exchange,提问作者Ouburg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:34:55