如何解决Kableextra:更新至R 4.0.3并重新安装后,我无法在每个单元格中正确运行带有多行换行文本的旧脚本
最亲爱的
这类似于我在平台上运行了几个月的脚本。
我在列中有一些长文本(称为“说明”),并且希望将其换行(每行最多20个字符)。为此,我有一个函数,可以在需要时在文本中插入一个换行符“ \ n”。 然后,我在该文本上使用kableextra的“换行符”功能,以确保正确理解换行符(确实如此)。
然后通过乳胶处理最终结果以生成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)),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 package(v0.3.0.9001)于2020-10-13创建
解决方法
问题似乎出在您的包装函数而不是kableExtra
上。如果检查df2
data.frame,您会发现它包含许多\makecell
带有奇怪对齐方式的调用。
这些makecell通过kableExtra::linebreak
函数插入。要获得所需的行为,请使用以下命令更改包装函数:
linebreak(description,align='l')
此行为更改是在a recent commit中引入了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)
这将产生带有此表的文档:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。