rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.86k stars 969 forks source link

Combining Output Formats #2568

Open franceskoi opened 1 month ago

franceskoi commented 1 month ago

Is it possible that functions from the both bookdown and officedown are taken into account at the output? For instance, when I write this code (Version 1 of the .Rmd document):

---
title: "Analysis"

output: 
  officedown::rdocx_document:
    reference_docx: "reference.docx"
  bookdown::word_document2:
    reference_docx: "reference.docx"  
bibliography: references.bib
---

{r setup, include=FALSE}
knitr::opts_chunk$set(
    echo = TRUE,
    message = FALSE,
    warning = FALSE,
    out.width = NULL,
  out.height = NULL
)

# Header1

## Header2

### Header3

Lorem Ipsum Lorem Ipsum Lorem Ipsum

<br>

Lorem Ipsum:

-   Lorem Ipsum (Image: \@ref(fig:img1))

<br>

{r img1, echo=FALSE, message=FALSE, warning=FALSE, fig.height =2, fig.cap="Image 1", fig.id = "img1", fig.cap.style = "Image Caption"}
library(tidyverse)
library(readxl)
library(officer)   
library(flextable)
library(officedown)
library(ggplot2)
library(grid)
library(jpeg)

img <- readJPEG("rmarkdown_logo.jpg")

grid.raster(img)

<br>

#### Lorem Ipsum

Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum

<!---BLOCK_LANDSCAPE_START--->

{r w_height, echo=FALSE, message=FALSE, warning=FALSE, tab.cap="women height", tab.id = "rezultatiCM816", tab.cap.style = "Table Caption"}
height <- women$height
height

<!---BLOCK_LANDSCAPE_STOP--->

it ignores bookdown (it doesn't do automatic numeration of the chapters and it doesn't do me cross-referencing), but it makes me a landscape page from the officedown, but when I write this code (Version 2 of the .Rmd document):

---
title: "Analysis"

output: 
  bookdown::word_document2:
    reference_docx: "reference.docx" 
  officedown::rdocx_document:
    reference_docx: "reference.docx" 
bibliography: references.bib
---

{r setup, include=FALSE}
knitr::opts_chunk$set(
    echo = TRUE,
    message = FALSE,
    warning = FALSE,
    out.width = NULL,
  out.height = NULL
)

# Header1

## Header2

### Header3

Lorem Ipsum Lorem Ipsum Lorem Ipsum

<br>

Lorem Ipsum:

-   Lorem Ipsum (Image: \@ref(fig:img1))

<br>

{r img1, echo=FALSE, message=FALSE, warning=FALSE, fig.height =2, fig.cap="Image 1", fig.id = "img1", fig.cap.style = "Image Caption"}
library(tidyverse)
library(readxl)
library(officer)   
library(flextable)
library(officedown)
library(ggplot2)
library(grid)
library(jpeg)

img <- readJPEG("rmarkdown_logo.jpg")

grid.raster(img)

<br>

#### Lorem Ipsum

Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum

<!---BLOCK_LANDSCAPE_START--->

{r w_height, echo=FALSE, message=FALSE, warning=FALSE, tab.cap="women height", tab.id = "rezultatiCM816", tab.cap.style = "Table Caption"}
height <- women$height
height

<!---BLOCK_LANDSCAPE_STOP--->

it ignores officedown (it doesn't show me a landscape page), but it puts an automatic numeration for the chapters and cross-refferencing from bookdown. So, it takes only what is first written. Is there a way to combine these two, so there can be shown an automatic numeration of the chapters and cross-refferencing from bookdown, as well as a landscape page from officedown?

I am sending the outputs for the Version 1 and Version 2 of the output Word documents, as well as the reference Word doc. Version1.docx Version2.docx reference.docx

cderv commented 3 weeks ago

I would advise to revise the rmarkdown basics about formats. rmarkdown::render() will only render one output format, that you can provide as an argument to the function, or using YAML and the first one will be used as default.

If you want to combine feature from several output formats, you need to look into base_format document. Especially the combination of bookdown and officedown makes sense, and you should look at the doc.

So basically

output:
  officedown::rdocx_document:
    base_format: bookdown::word_document2

You should look into more about the officeverse to see how you can achieve the different feature you need.

Hope it helps

franceskoi commented 2 weeks ago

Thank you, I will try it!