r - Kableextra:更新到 R 4.0.3 并重新安装后,我无法正确运行每个单元格中包含多行换行文本的旧脚本
问题描述
最亲爱的,
这类似于我在平台上运行了几个月的一个脚本。
我在一列中有一些长文本(称为“描述”),我想将它换行(每行不超过 20 个字符)。为了做到这一点,我有一个函数可以在需要时在文本中插入一个换行符“\n”。然后我在该文本上使用 kableextra 的“换行符”功能,以确保正确理解换行符(确实如此)。
然后通过latex处理最终结果以生成pdf。问题是描述列内的文本虽然按我的意愿破坏,但不再左对齐。我确信我过去没有这个(在将 R 更新到 4.0.3 并重新安装 kableextra 之前)。
任何知道如何解决此问题的人都会感激不尽!
谢谢
library(tidyverse)
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
library(formattable)
##Functions to wrap and format text in the table
wrapper2 <- function(x, width) {
lapply(strwrap(x, width = width, simplify = FALSE), paste, collapse="\n")
}
wrapper_text <- function(x, width) {
res <- wrapper2(x, width) %>% unlist
return(res)
}
df <- structure(list(description = c("Total Goods", "85 Electrical machinery and equipment",
"84 Machinery and mechanical appliances", "63 Other made-up textile articles",
"94 Furniture; lamps and lighting fittings", "90 Optical, medical or surgical instruments",
"62 Apparel and clothing, not knitted or crocheted", "29 Organic chemicals",
"39 Plastics", "95 Toys, games and sports requisites", "87 Motor vehicles"
), y = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8)), row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
df2 <- df %>%
mutate(description=wrapper_text(description, 20)) %>%
mutate(description=linebreak(description))
mytable <- df2 %>%
kable("latex", booktabs = T, escape = F,align=c("l", "r")) %>%
kable_styling( )
writeLines(
c(
"\\documentclass{article}",
"\\usepackage{graphicx}",
"\\usepackage{makecell}",
"\\usepackage{float}",
"\\usepackage{booktabs}",
"\\begin{document}",
"\\pagestyle{empty}",
mytable,
"\\end{document}"
),
"test_table.tex"
)
tools::texi2pdf("test_table.tex", clean = TRUE)
print(sessionInfo())
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 10 (buster)
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so
#>
#> locale:
#> [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
#> [5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
#> [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] formattable_0.2.0.1 kableExtra_1.2.1 forcats_0.5.0
#> [4] stringr_1.4.0 dplyr_1.0.2 purrr_0.3.4
#> [7] readr_1.4.0 tidyr_1.1.2 tibble_3.0.3
#> [10] ggplot2_3.3.2 tidyverse_1.3.0
#>
#> loaded via a namespace (and not attached):
#> [1] styler_1.3.2 tidyselect_1.1.0 xfun_0.18 haven_2.3.1
#> [5] colorspace_1.4-1 vctrs_0.3.4 generics_0.0.2 viridisLite_0.3.0
#> [9] htmltools_0.5.0 yaml_2.2.1 blob_1.2.1 rlang_0.4.8
#> [13] pillar_1.4.6 glue_1.4.2 withr_2.3.0 DBI_1.1.0
#> [17] dbplyr_1.4.4 modelr_0.1.8 readxl_1.3.1 lifecycle_0.2.0
#> [21] munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0 rvest_0.3.6
#> [25] htmlwidgets_1.5.2 evaluate_0.14 knitr_1.30 fansi_0.4.1
#> [29] highr_0.8 broom_0.7.1 Rcpp_1.0.5 scales_1.1.1
#> [33] backports_1.1.10 webshot_0.5.2 jsonlite_1.7.1 fs_1.5.0
#> [37] hms_0.5.3 digest_0.6.25 stringi_1.5.3 grid_4.0.3
#> [41] cli_2.0.2 tools_4.0.3 magrittr_1.5 crayon_1.3.4
#> [45] pkgconfig_2.0.3 ellipsis_0.3.1 xml2_1.3.2 reprex_0.3.0.9001
#> [49] lubridate_1.7.9 assertthat_0.2.1 rmarkdown_2.4 httr_1.4.2
#> [53] rstudioapi_0.11 R6_2.4.1 compiler_4.0.3
由reprex 包于 2020 年 10 月 13 日创建(v0.3.0.9001)
解决方案
问题似乎来自您的包装函数而不是kableExtra
. 如果您检查您的df2
data.frame,您会发现它包含许多\makecell
带有奇怪对齐的调用。
这些 makecell 由kableExtra::linebreak
函数插入。要获得您想要的行为,请使用以下内容更改您的包装函数:
linebreak(description, align='l')
这种行为变化是在最近提交到kableExtra
存储库时引入的。
作为参考,我认为kableExtra
在这种情况下使用更惯用(和更容易)的方法是避免自己包装文本,并使用column_spec
函数指定列宽。
请注意,我专门使用了框架,而不是像您那样df
经历:df2
library(kableExtra)
mytable <- df %>%
kbl("latex", booktabs=TRUE, escape=FALSE, align="lr") %>%
kable_styling() %>%
column_spec(1, width="2in")
writeLines(c(
"\\documentclass{article}",
"\\usepackage{graphicx}",
"\\usepackage{makecell}",
"\\usepackage{float}",
"\\usepackage{booktabs}",
"\\begin{document}",
"\\pagestyle{empty}",
mytable,
"\\end{document}"),
"test_table.tex"
)
tools::texi2pdf("test_table.tex", clean = TRUE)
这将生成一个包含此表的文档:
推荐阅读
- batch-file - 使用“rundll 键盘,禁用”运行批处理文件是否需要恢复出厂设置才能修复?
- esp8266 - ESP8266-12F WiFi 软 AP config.authmode 失败
- r - 包足球动画
- php - 带有文本框自定义验证的 Wordpress Ninja 表单
- pine-script - 从图表参数中删除空值
- java - 如何使用 Spring Boot Framework 在发布请求正文节点中接受 PascalCase 而不是 camelCase
- cordova - Ionic 3 Firebase 和代码推送插件替换
- android - 蓝牙何时从 Android 手机广播?
- javascript - 检测重叠矩形的有效数据结构/技术是什么?
- python - 使用 SQLAlchemy Session 作为上下文管理器时收到“AttributeError:__enter__”