在预订/降价中将Flextables渲染到PDF不适合页面

发布于 2025-02-01 03:06:01 字数 4014 浏览 5 评论 0原文

当我尝试将Markdown文档渲染到PDF时(使用PDF_Document或BookDown中的PDF_Document2),我无法根据需要显示包含的Flextable(根据我所引用的文档改编)以显示所需的显示。这是特别令人沮丧的,因为在Rstudio Visual Markdown编辑器中,表根据需要显示。我尝试使用autofit()和set_table_properties(layout =“ autofit”),无用。使用下面的Markdown脚本,我获得的表看起来像 ”

“在此处输入映像说明”

当我所需的输出(在Visual Editor中)是

.认为这可能是由于牢房,边界和主题的并发症所致,但是即使我将其剥离到裸露的弹性时,我也会遇到相同的合适问题。渲染到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 enter image description here

and

enter image description here

when my desired output which is how it appears in visual editor is enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2025-02-08 03:06:01

抱歉,这是一个错误。它应该在github上固定(并在cran上很快可用)。

---
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
```

Sorry, this was a bug. It should be fixed on Github (and soon available on CRAN).

---
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
```

enter image description here

方觉久 2025-02-08 03:06:01

如果您愿意容忍基于Huxtable的解决方案(我的软件包),那么这是否接近您想要的?

library(huxtable)
tth <- as_hux(TEST_TABLE) |> 
         merge_repeated_rows(everywhere, 1:6) |> 
         set_tb_borders() |> 
         set_width("6in") |> 
         set_col_width(c(.6/5, .6/5, .6/5, .4,.6/5, .6/5)) 

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?

library(huxtable)
tth <- as_hux(TEST_TABLE) |> 
         merge_repeated_rows(everywhere, 1:6) |> 
         set_tb_borders() |> 
         set_width("6in") |> 
         set_col_width(c(.6/5, .6/5, .6/5, .4,.6/5, .6/5)) 

where quick_pdf(tth, width = "7in", height = "4in") looks like the below:

PDF output from huxtable

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(...)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文