Thursday, September 8, 2022
HomeITThe way to create Phrase docs from R or Python with Quarto

The way to create Phrase docs from R or Python with Quarto


There are a number of methods to create a Phrase doc from programming languages, together with R Markdown and the officer package deal with R and the python-docx library in Python. However one of many latest and extra intriguing is Quarto, a free, open supply technical publishing system from RStudio (now Posit) that’s native to Python and Julia in addition to R.

One of many massive benefits of Quarto is that, in contrast to a Phrase-specific package deal, the identical Quarto file with minor tweaks can be utilized to generate dozens of output codecs along with Phrase, together with PowerPoint, HTML, and PDF.  (Discover out extra: “What’s Quarto? RStudio rolls out next-generation R Markdown”.) As well as, you may automate the creation of Phrase studies and embody outcomes of your evaluation and visualization code.

Right here’s find out how to use Quarto to create Phrase paperwork.

Step 1: Set up Quarto

As a result of Quarto isn’t a language-specific library, you put in it like every other stand-alone software program. You’ll find binary downloads for Home windows, macOS, and Linux on Quarto’s “Get Began” web page.

Should you’re an R person and you’ve got an up-to-date model of RStudio, Quarto must be included by default. You don’t want to put in Quarto individually.

If you wish to use Quarto in Visible Studio Code, set up the Quarto extension along with the Quarto utility software program. To render Quarto paperwork that embody Python code, my system additionally instructed me to put in Jupyter Pocket book by working python3 -m pip set up jupyter.

You’ll be able to create and render Quarto information with any plain textual content editor and your terminal, simply as you may with R or Python scripts, since they’re plain textual content and never binary information. Nonetheless, you’d miss out on the entire built-in instruments of an IDE, comparable to code completion strategies and a render button.

Step 2: Create a Quarto doc file

When you’ve obtained Quarto put in, you may create a brand new Quarto file in your IDE the standard method, both File > New File > Quarto Doc (not Quarto Presentation) in RStudio, or File > New File in VS Code and select “Quarto” because the language.

In RStudio, you’ll have a selection of some Quarto doc output codecs. Choose Phrase, and you may then both auto-generate a Phrase pattern doc or a clean doc. It may be useful till you’re conversant in Quarto syntax to see what the pattern seems like.

Document text including the YAML header --- title: 'My Quarto Word Doc Sample' format: docx editor: Display shot by Sharon Machlis

Pattern Quarto doc generated by RStudio when deciding on Phrase output.

The default YAML header in RStudio features a title, output format (on this case docx for Phrase), and editor (visible WYSIWYG or supply).

Should you’re beginning with a clean doc in VS Code, you may add the essential YAML header on the prime:

---
title: "Your doc title"
format: docx
---

So far as I do know, there isn’t a WYSIWYG Quarto editor in VS Code, so there isn’t a cause to specify an editor.

Then begin creating your content material.

Step 3: Add textual content with Markdown syntax

Quarto makes use of Pandoc’s model of Markdown syntax for writing textual content. That features single underscores round textual content you need in italics, double asterisks for textual content you wish to daring, clean strains between paragraphs, two or extra areas on the finish of a line to create a line break, and hash symbols at the beginning of a line to suggest header font measurement. A single hash signifies the most important font measurement, h1; two is the second largest, h2; and so forth.

Step 4 (non-compulsory): Type your doc from a reference .docx

Some CSS-based doc styling designed for Quarto HTML output codecs received’t work when exporting to Phrase. Nonetheless, you may create a separate reference model Phrase doc with font kinds, sizes, and such on your doc.

The code under must be run in your terminal (not R or Python console) to create a default Phrase styling doc, on this instance known as my_doc_style.docx (you may name it something):

quarto pandoc -o my-doc-style.docx   
--print-default-data-file reference.docx

This creates an everyday Phrase .docx file, not a Microsoft Phrase .dotx template. You’ll be able to open your reference .docx and customise its kinds as with all Phrase doc by opening the Types panel from the Phrase ribbon.

To use the template in a Quarto doc, add it to the doc’s YAML header with syntax like this:

format: 
  docx: 
    reference-doc: my-doc-style.docx

There are different customizations obtainable for Quarto Phrase paperwork, comparable to including a desk of contents or part numbering, which you’ll be able to see within the Quarto documentation for Phrase

Step 5: Add outcomes of R or Python code to your Phrase doc

Among the best issues about producing a Phrase doc from R or Python is the flexibility to run code and add outcomes to your doc—together with graphics.

You do that by including code chunks to your Quarto file, that are set off by three backticks, like this for R:

```{r}
# R code right here
```

or this for Python:

```{python}
# Python code right here
```

You’ll be able to set choices for a code chunk comparable to whether or not to show the code (echo), run the code (eval), present code warning messages, and so forth. Chunk choices begin off with #| (sometimes called a “hash pipe”) for R, Python, or Julia.

The chunk choices under would present outcomes of R code in a bit however not show the code within the Phrase doc:

```{r}
#| echo: false
#| eval: true
# R code right here
```

Different choices embody #| fig-cap: My caption for a determine caption, #| warning: false to not show any warning messages when code runs, and #| cache: true to cache outcomes of a compute-intensive chunk the place information received’t change.

You’ll be able to execute code throughout the determine caption possibility through the use of !expr with syntax comparable to 

#| fig-cap: !expr paste("Information pulled on"Sys.Date())

Step 6: Render the doc

You’ll be able to render a Quarto doc in RStudio or VS Code through the use of the Render button, the keyboard shortcut Ctrl/Cmd + Shift + Ok or achieve this with the terminal command 

quarto render my_quarto_document.qmd --to docx

for a doc named my_quarto_document.

R customers can even use the quarto R package deal’s command

quarto_render("my_quarto_document")

Be aware: Often, the preliminary Phrase doc preview that popped up from RStudio in early variations didn’t all the time show my graph. That appears to be fastened. Nonetheless, if that occurs to you, attempt duplicating the preliminary .docx file as a brand new, editable Phrase doc, since that fastened the difficulty for me.

Step 7 (non-compulsory): Automate a number of variations with parameters

With the ability to create Phrase information with outcomes of your code is helpful not just for single-time paperwork. It additionally allows you to streamline common information reporting and updates with code that pulls new information from an exterior supply, runs new calculations, and generates up-to-date graphs with a single render name.

However Quarto additionally has the flexibility so as to add parameters to a report, that are like variables outlined externally throughout rendering. That allows you to use a report as a template and create the identical report for various parameters like cities or areas. For instance, for those who wanted to run one report for every of 10 cities, metropolis may very well be outlined as a parameter in your doc’s YAML header, comparable to

---
title: "My Quarto Doc"
params:
metropolis: New York
---

That units a parameter named metropolis with a default worth of New York. You’ll be able to then entry the worth of the town parameter in your R code with params$metropolis, comparable to 

```{r}
#| echo: false
cat("This report is about", params$metropolis)
```

To create a number of studies in R utilizing the identical Quarto doc however completely different values for the parameter, I usually create a perform to render my doc after which use the purrr package deal’s stroll() perform to run my perform on an inventory of things. For instance, if my parameterized Quarto doc is called params_test.qmd with one parameter named metropolis, this may very well be my render perform in R:

render_my_doc <- perform(the_city = "New York", the_doc = "params_test.qmd") {
quarto::quarto_render(enter = the_doc, execute_params = checklist("metropolis" = the_city),
output_file = paste0("test_", the_city, ".docx"), output_format = "docx")
}

And that is how I’d use my perform to generate three separate paperwork for New York, Chicago, and Los Angeles:

library(purrr)
stroll(checklist("New York", "Chicago", "Los Angeles"), render_my_doc)

Python syntax is a bit completely different and relies on the papermill library. For instance, defining a parameter is finished in a Python code chunk that may appear like

```{python}
#| tags: [parameters]
metropolis = 'Boston'
```

You’ll be able to learn extra about parameterizing Python paperwork within the Quarto Parameters documentation

Should you’re thinking about R and extra tips on R, head to the Do Extra With R web page!

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments