在预订/降价中将Flextables渲染到PDF不适合页面
当我尝试将Markdown文档渲染到PDF时(使用PDF_Document或BookDown中的PDF_Document2),我无法根据需要显示包含的Flextable(根据我所引用的文档改编)以显示所需的显示。这是特别令人沮丧的,因为在Rstudio Visual Markdown编辑器中,表根据需要显示。我尝试使用autofit()和set_table_properties(layout =“ autofit”),无用。使用下面的Markdown脚本,我获得的表看起来像
和
.认为这可能是由于牢房,边界和主题的并发症所致,但是即使我将其剥离到裸露的弹性时,我也会遇到相同的合适问题。渲染到HTML一切看起来都很好。我发现的所有帖子和文档都表明它应该按书面形式工作,不确定我缺少什么。
---
title: "Test Flextables"
author: ""
date: ""
output:
bookdown::pdf_document2:
toc: false
fig_caption: yes
latex_engine: xelatex
number_sections: false
keep_tex: true
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(flextable)
library(bookdown)
library(knitr)
TEST_TABLE <- structure(list(`Winter Feeding Strategy` = c("Shallow snow terrestrial lichen",
"Shallow snow terrestrial lichen", "Shallow snow terrestrial lichen",
"Shallow snow terrestrial lichen", "Shallow snow terrestrial lichen",
"Deep snow arboreal lichen"), `Ecotype Name` = c("BC Northern Alberta Mountain",
"BC Northern Alberta Mountain", "BC Northern Alberta Mountain",
"BC Northern Alberta Mountain", "BC Northern Alberta Mountain",
"BC Mountain"), Location = c("Northern BC", "West central BC",
"North central BC", "East central BC", "West central Alberta",
"Southeastern BC"), `Nationally Significant Population by National Ecological Area (SARA)` = c("Northern Mountain",
"Southern Mountain", "Southern Mountain", "Southern Mountain",
"Southern Mountain", "Southern Mountain"), `COSEWIC Designatable Unit (2011)` = c("Northern Mountain",
"Northern Mountain", "Northern Mountain", "Central Mountain",
"Northern Mountain", "Southern Mountain"), `Southern Mountain Caribou Groupings` = c("Not Applicable",
"Central Group", "Central Group", "Central Group", "Central Group",
"Southern Group")), row.names = c(NA, -6L), spec = structure(list(
cols = list(`Winter Feeding Strategy` = structure(list(), class = c("collector_character",
"collector")), `Ecotype Name` = structure(list(), class = c("collector_character",
"collector")), Location = structure(list(), class = c("collector_character",
"collector")), `Nationally Significant Population by National Ecological Area (SARA)` = structure(list(), class = c("collector_character",
"collector")), `COSEWIC Designatable Unit (2011)` = structure(list(), class = c("collector_character",
"collector")), `Southern Mountain Caribou Groupings` = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
```
## Test Table No Fitting
```{r TEST1, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
TEST_FLEX
```
## Test Table autofit()
```{r TEST2, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
autofit(TEST_FLEX)
```
## Test Table set_table_properties
```{r TEST3, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
set_table_properties(TEST_FLEX, layout="autofit")
```
When I attempt to render a markdown document to pdf (using either pdf_document, or pdf_document2 from bookdown) I cannot get the included flextable (adapted from document I am referencing) to display as desired. This has been particularly frustrating as in Rstudio visual markdown editor the table displays as desired. I have attempted to use autofit() and set_table_properties(layout="autofit") to no no avail. Using the markdown script below I get tables that look like
and
when my desired output which is how it appears in visual editor is
I thought it could be due to complications with the merging of cells, borders and the theme, but even when I strip it down to a bare flextable I run into the same fitting issues. Rendering to html everything looks fine. All the posts and documentation I have found indicate it should work as written, not sure what I am missing.
---
title: "Test Flextables"
author: ""
date: ""
output:
bookdown::pdf_document2:
toc: false
fig_caption: yes
latex_engine: xelatex
number_sections: false
keep_tex: true
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(flextable)
library(bookdown)
library(knitr)
TEST_TABLE <- structure(list(`Winter Feeding Strategy` = c("Shallow snow terrestrial lichen",
"Shallow snow terrestrial lichen", "Shallow snow terrestrial lichen",
"Shallow snow terrestrial lichen", "Shallow snow terrestrial lichen",
"Deep snow arboreal lichen"), `Ecotype Name` = c("BC Northern Alberta Mountain",
"BC Northern Alberta Mountain", "BC Northern Alberta Mountain",
"BC Northern Alberta Mountain", "BC Northern Alberta Mountain",
"BC Mountain"), Location = c("Northern BC", "West central BC",
"North central BC", "East central BC", "West central Alberta",
"Southeastern BC"), `Nationally Significant Population by National Ecological Area (SARA)` = c("Northern Mountain",
"Southern Mountain", "Southern Mountain", "Southern Mountain",
"Southern Mountain", "Southern Mountain"), `COSEWIC Designatable Unit (2011)` = c("Northern Mountain",
"Northern Mountain", "Northern Mountain", "Central Mountain",
"Northern Mountain", "Southern Mountain"), `Southern Mountain Caribou Groupings` = c("Not Applicable",
"Central Group", "Central Group", "Central Group", "Central Group",
"Southern Group")), row.names = c(NA, -6L), spec = structure(list(
cols = list(`Winter Feeding Strategy` = structure(list(), class = c("collector_character",
"collector")), `Ecotype Name` = structure(list(), class = c("collector_character",
"collector")), Location = structure(list(), class = c("collector_character",
"collector")), `Nationally Significant Population by National Ecological Area (SARA)` = structure(list(), class = c("collector_character",
"collector")), `COSEWIC Designatable Unit (2011)` = structure(list(), class = c("collector_character",
"collector")), `Southern Mountain Caribou Groupings` = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
```
## Test Table No Fitting
```{r TEST1, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
TEST_FLEX
```
## Test Table autofit()
```{r TEST2, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
autofit(TEST_FLEX)
```
## Test Table set_table_properties
```{r TEST3, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>%
merge_v(1:6)%>%
theme_zebra() %>%
border_inner_h()
set_table_properties(TEST_FLEX, layout="autofit")
```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,这是一个错误。它应该在github上固定(并在cran上很快可用)。
Sorry, this was a bug. It should be fixed on Github (and soon available on CRAN).
如果您愿意容忍基于Huxtable的解决方案(我的软件包),那么这是否接近您想要的?
where
Quick_pdf(tth,width =“ 7in”,height =“ 4in”)
看起来如下:您可以使用huxtable命令进一步自定义输出,EG设置背景颜色对于您的标题,或与
map_background_color(tth,by_rows(...))添加条纹
。If you're willing to tolerate a huxtable-based solution (my package), is this close to what you want?
where
quick_pdf(tth, width = "7in", height = "4in")
looks like the below:You can customize output further with huxtable commands, e.g. setting background colors for your header, or adding a stripe with
map_background_color(tth, by_rows(...))
.