Skip to contents

The purpose of this function

The conditional_publishing_output function is designed to simplify the process of creating multiple outputs from a single R Markdown document. This is useful when you want to create two different outputs which are mostly identical in content, but have a couple of minor differences in formatting or content.

The most common use case for this is when you want to have a formatted bit of content in a document you’ll be sharing with others, but this formatting is not compatible with publishing in govspeak format. For example, you may want to have a formatted header in your HTML output, but don’t want this HTML formatting to appear in the version used to create your govspeak output.

Using conditional_publishing_output

The conditional_publishing_output function takes two arguments:

  • output - the content you want to conditionally include in your output. This can be any valid R Markdown content, such as text, code, or images.

  • publication_type - the type of output you want to create. This is based on the name of the document in the YAML header of your Rmd file. The most common options you may use are:

  • “html” for HTML/govspeak output

  • “docx” for Word output

  • “latex” for PDF output

Here’s an example of how you might use the conditional_publishing_output function in an R Markdown document:

conditional_publishing_output(
  output = "# This is a header",
  publication_type = "html"
)

In this example, the header will only appear in the HTML output of the document. If you were to create a Word or PDF output, this header would not appear in the output.

You can also use multiple conditional outputs in a single chunk as an if-else type statement. This allows you to include the same content but with different formatting across two different output types. Here’s an example:

conditional_publishing_output(
  output = "# This is a header",
  publication_type = "html"
)

conditional_publishing_output(
  output = "This is a header",
  publication_type = "docx"
)

In this example, the header will appear as a formatted header in the HTML output, but as plain text in the Word output.

Use within other functions

This function also forms the basis of other functions in the govspeakr package, such as govspeakr::add_blue_header(). This function uses conditional_publishing_output to generate a blue header in HTML output, but does not show this header in other output formats. This is designed to be used in situations where you want to share your HTML content prior to release and want it to emulate the appearance of gov.uk pages. The will be added automatically by Whitehall publisher when the govspeak content is published, so it’s not necessary for this to be present in your govspeak output.