Site icon R-bloggers

Automate Your Blog Workflow with a Custom R Function: Creating QMD Files

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

Introduction

As a blogger who uses R for content creation, I’ve found it incredibly useful to automate some of the repetitive tasks. One such task is creating Quarto Markdown (QMD) files for new blog posts. To simplify this, I’ve added a custom R function that not only creates the necessary file structure. Let’s take a look at this function and how you can integrate it into your own workflow.

< section id="the-function" class="level1">

The Function

Here’s the function I’ve been using:

create_qmd_file <- function(filename = "index.qmd", title = "", date = Sys.Date(), 
                            categories = c("code", "rtip")) {
  # Define the base path
  base_path <- paste0(getwd(),"/posts")
  
  # Convert date to string and create the full directory path
  date_str <- as.character(date)
  full_path <- file.path(base_path, date_str)
  
  # Create the directory if it doesn't exist
  if (!dir.exists(full_path)) {
    dir.create(full_path, recursive = TRUE)
    message("Directory created: ", full_path)
  }
  
  # Define the full file path
  file_path <- file.path(full_path, filename)
  
  # Create the content to be written in the file
  content <- paste0(
    "---\n",
    'title: "', title, '"\n',
    'author: "Steven P. Sanderson II, MPH"\n',
    'date: "', date_str, '"\n',
    "categories: [", paste(categories, collapse = ", "), "]\n",
    "toc: TRUE\n",
    "---\n\n",
    '<script src="https://giscus.app/client.js"\n',
    '        data-repo="Yours_Here"\n',
    '        data-repo-id="Yours_Here"\n',
    '        data-category="Comments"\n',
    '        data-category-id="Yours_Here"\n',
    '        data-mapping="url"\n',
    '        data-strict="0"\n',
    '        data-reactions-enabled="1"\n',
    '        data-emit-metadata="0"\n',
    '        data-input-position="top"\n',
    '        data-theme="dark"\n',
    '        data-lang="en"\n',
    '        data-loading="lazy"\n',
    '        crossorigin="anonymous"\n',
    '        async>\n',
    '</script>\n'
  )
  
  # Write the content to the file
  writeLines(content, file_path)
  message("QMD file created: ", file_path)
}
< section id="how-it-works" class="level2">

How It Works

  1. Setting the Base Path: The function starts by defining the base path where blog posts will be stored, appending "/posts" to the current working directory. This centralizes all posts in one location.

  2. Creating Directories: It then converts the date to a string and uses it to create a directory path. If this directory doesn’t exist, the function creates it. This helps in organizing posts by date.

  3. File Path Definition: The function then defines the full path for the QMD file, defaulting the filename to “index.qmd” if none is provided.

  4. Content Creation: The main content for the QMD file is generated next. This includes a YAML front matter section with metadata like title, author, date, and categories. The function also adds a script for Giscus, which handles the comments section.

  5. File Writing: Finally, the function writes the generated content to the specified file path and informs you that the file has been created.

< section id="automating-with-.rprofile" class="level2">

Automating with .Rprofile

To make this function available every time you start your project, you can use the .Rprofile file. This file is sourced whenever you start a new R session, making it perfect for setting up your environment.

Here’s the relevant .Rprofile setup:

source(paste0(getwd(),"/create_qmd_file.R"))

By sourcing the create_qmd_file.R script, the function is loaded automatically, so you don’t have to manually source it each time.

< section id="give-it-a-try" class="level1">

Give It a Try!

This function has saved me a lot of time, and I encourage you to try something similar if you’re managing content or projects with R. You can customize the function to suit your specific needs, whether that’s adjusting the metadata, changing the file structure, or adding other automated features. It’s a great way to streamline your workflow and focus more on the creative aspects of your work.

If you have any questions or run into any issues, feel free to reach out. Happy coding!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version