How to produce Word docs from R or Python with Quarto


There are a number of methods to create a Word document from programs languages, consisting of R Markdown and the officer package with R and the python-docx library in Python. But among the newest and more interesting is Book, a free, open source technical publishing system from RStudio (now Posit) that’s native to Python and Julia along with R.One of the big benefits of Book is that, unlike a Word-specific package, the exact same Book file with small tweaks can be utilized to produce dozens of output formats in addition to Word, consisting of PowerPoint, HTML, and PDF. (Find out more: “What is Quarto? RStudio rolls out next-generation R Markdown”.) In addition, you can automate the creation of Word reports and include results of your analysis and visualization code.Here’s how

to use Book to produce Word documents.Step 1: Set up Book Due to the fact that Book

isn’t a language-specific library, you install it like any other stand-alone software application. You can find binary downloads for Windows, macOS, and Linux on Quarto’s “Start” page.

If you’re an R user and you have an up-to-date version of RStudio, Quarto ought to be consisted of by default. You do not need to install Quarto separately.If you want to utilize Book in Visual Studio Code, set up the Book extension in addition to the Book software. To render Quarto files that consist of Python code, my system likewise advised me to set up Jupyter Notebook by running python3-m pip set up jupyter. You can develop and render Quarto files with any plain text editor

and your terminal, just as you can with R or Python scripts, given that they are plain text and not binary files. However, you ‘d miss out on all of the built-in tools of an IDE, such as code conclusion ideas and a render button.Step 2: Produce a Quarto document file Once you have actually got Book installed, you can create a new Book file

in your IDE the typical way, either File > New File > Book File (not Quarto Discussion )in RStudio, or File > New File in VS Code and pick”Quarto”as the language. In RStudio, you’ll have a choice of a couple of Book document output formats

. Select Word, and you can then either auto-generate a Word sample file or a blank doc. It can be useful up until you recognize with Quarto syntax to see what the sample looks like. Screen shot by Sharon Machlis Sample Quarto file created by RStudio when choosing Word output.

The default YAML header in RStudio consists of a title, output format (in this case docx for Word), and editor (visual WYSIWYG or source).

If you’re starting with a blank document in VS Code, you can include the basic YAML header at the top:

— title: “Your file title”
format: docx
— As far as I understand, there is no WYSIWYG Book editor in VS Code, so there is no reason to specify an editor.

Then begin creating your content.Step 3: Add text with Markdown syntax Quarto utilizes Pandoc’s variation of Markdown syntax for writing text. That consists of single underscores around text you want in italics, double asterisks for text you wish to bold, blank lines between paragraphs, two or more spaces at the end of a line to produce a line break, and hash symbols at the start of a line to symbolize header font style size. A single hash suggests the biggest font size, h1; 2 is the second biggest, h2; and so on.Step 4(optional ): Style your document from a reference.docx Some CSS-based file styling developed for Quarto HTML output formats won’t work when exporting to Word. Nevertheless, you can develop a different referral style Word file with font designs, sizes, and such for your document.The code listed below should be run in your terminal (not R or Python console)

to develop a default Word styling document , in this example called my_doc_style. docx( you can call it anything ): book pandoc-o my-doc-style. docx — print-default-data-file reference.docx This develops a routine Word.docx file, not a Microsoft Word.dotx template. You can open your reference.docx and customize its styles just like any Word document by opening the Styles panel from the Word ribbon.To usage the design template in a Quarto doc, add it to the document’s YAML header with syntax like this: format: docx: reference-doc: my-doc-style. docx There are other modifications offered for Quarto Word files, such as adding a table of contents or section numbering, which you can see in the Quarto

documents for Word. Step 5: Add results of R or Python code to your Word doc Among the best features of creating a Word doc from R or Python is the ability to run code and add outcomes to your document– consisting of graphics.You do this by adding code pieces to your Book file, which are set off

by three backticks, like this for R:” ‘ # R code here “‘ or this for Python:”‘python # Python code here”‘You can set options for a code piece such as whether to show the code (echo), run the

code (eval), reveal code caution messages, and so on. Portion choices begin with # |(frequently referred to as a”hash pipeline “)for R, Python, or Julia.The portion choices listed below would reveal outcomes of R code in a piece but not display the code in

Word doc:”‘ r #|echo: false #|eval: real # R code here”‘Other options include #|fig-cap: My caption for a figure caption, #|caution: false to not display any warning messages when code runs

, and #|cache: true to cache outcomes

of a compute-intensive chunk where information wo n’t change.You can execute code within the figure caption alternative by
using! expr with syntax such
as #|fig-cap:!
expr paste (“Data pulled on” Sys.Date ())Action 6: Render the file You can render a Quarto document in RStudio or VS Code by utilizing the Render button, the keyboard faster way Ctrl/Cmd+Shift+K or do so with the terminal command quarto render my_quarto_document. qmd– to docx for a document

named my_quarto_document. R users can likewise use the book R package’s command quarto_render(” my_quarto_document”)Note: Sometimes, the initial Word file preview that appeared from RStudio in early variations didn’t always display my chart. That seems to be fixed. However, if that happens to you, attempt replicating the preliminary. docx file as a new, editable Word document, because that fixed the

issue for me.Step 7(optional): Automate numerous variations with criteria Having the ability to create Word

files with results of your code is useful not only for single-time files. It likewise lets you simplify regular data reporting and updates with code that pulls brand-new information from an external source, runs new estimations, and generates current graphs with a single render call.But Book also has the ability to add criteria to a report, which are like variables specified externally during rendering. That lets you utilize a report

as a template and create the very same report for various criteria like cities or regions. For example, if you needed to run one report for each of 10 cities, city could be specified as a criterion in your file’s YAML header, such as– title:”My Quarto File “params: city: New York– That sets a parameter named city with a default worth of New York.

You can then access the worth of the city specification in your R code with params$city, such as “‘ #|echo: incorrect feline(” This report has to do with”, params$ city )”‘To develop multiple reports in R using the very same Quarto document but various values for the parameter, I typically produce a function to render my file and then use the purrr bundle’s walk ()function to run my function on a list of items. For instance, if my parameterized Quarto file is called params_test. qmd with one parameter called city, this might be my render function in R: render_my_doc

Leave a Reply

Your email address will not be published. Required fields are marked *