S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Using Quarto for a Website

R Programming
Quarto
Author

Sam Hutchins

Published

January 6, 2024

This will be my first post created on this blog using Quarto for the website and blog. I have spent quite a bit of effort converting my existing posts on the original S Lazy-H Website to display here on this Quarto-generated site.

Most of those previous posts have images in the header, but they don’t show in the converted posts. That’s just one thing I still need to look into. That may be a css issue. There is a laundry list of things that Quarto really doesn’t like about the Allaire et al. (2024) format.

  • yaml data at head of post:
    • author: ~ (the tilde causes the Quarto render and preview functions to DIE!)
    • date: “`r Sys.Date()`” ( this works, but each render changes the post date. Not good!)
    • draft: no/yes (this causes problems, as quarto prefers false/true.)
  • Filename extensions wants to be .qmd instead of .rmd (although it will still compile.)
  • Hugo themes for rmarkdown creates a default ‘post’ directory inside content directory; quarto themes use ‘posts’ in the top level (/) project directory.
  • Quarto wants each post named ‘index.qmd’ inside a separate directory, which is also the link name for the post.
  • QUARTO REFUSES TO DISCOVER THE ‘R’ INSTALLATION (this is likely the most annoying bug.)

The last bullet above is the cause (I think) of why several pre-existing posts just will not render, and crash quarto to the command line. I had to delete several posts just to get the website to compile. Also, creating the “QUARTO_R” environment variable seems to have zero effect.

These are a few of the big site-breakers that come to mind. Most existing posts will work, to some extent, but there may be hidden issues with complicated posts rendering incorrectly. There are some issues with PDF files also, but that is still too new to me to get into now.

Next, we will get into the hoops I jumped through to make the pre-existing posts more compatible with Quarto. I ultimately made up a little script to do most of the heavy lifting, but still had to edit some text files to use with the script.

What I had wanted to do was automate the process of creating separate directories for each post, then placing each old post inside that directory; and in the process, rename each post to index.qmd. That took some searching to find a good answer! The script shown here may not be complete as I was testing as I went, so some things may have not made it into the file. As for the filex.txt versions I used, I had to manually edit them in some cases. In fact I re-created it several times to fit the purposes at the time. Early posts had used ‘.Rmd’ instead of ‘.rmd’, so I had two rename lines.

# convert files to .qmd and place each in own directory
# run this is new 'posts' directory
rename -v .rmd .qmd *.rmd # rename 'expression' 'replacement' 'file'
rename -v .Rmd .qmd *.Rmd
ls *.qmd > file1.txt # place all .qmd file names into file1.txt
# remove file extension, only directory names
sed -i 's/.qmd//' file1.txt # first variable
# recreate same 'ls' action, but keep extension for merging
ls *.qmd > file2.txt # second variable
pr -tm -w 150 file1.txt file2.txt > file3.txt # merge both files by columns
while read -r directory filename # reads the lines of file3.txt one by one, 
                        # and sets the first word to the variable "directory" ("date",) 
                        # and the remaining words to the variable "filename" ("path")
do 
    mkdir -p -- "$directory"  # creates a directory named "$directory".
    mv -- "$filename" "$directory" # moves the file from the "$filename" variable to the "$directory folder"
done < file3.txt   # here we set the input of the while loop to the merged file3.txt file
# this renames all the individual files in each directory to index.qmd
find -name '*.qmd' -exec bash -c ' dn=$(dirname "$1"); bn=$(basename "$dn"); c=$(ls "$dn/$bn"_??.qmd 2>/dev/null | wc -l); c=$((c+1)); cnt=$(printf "%02d" $c); mv "$1" "$dn/"index.qmd' -- {} \;
# rename any internal links inside posts to correct directory, old 'post' to new 'posts'
for i in `cat`;do sed -i 's/](\/post/](\/posts/' $i; done < file1.txt 
# fix each post's yaml header 'draft' to new format
for i in `cat`; do sed -i 's/draft: no/draft: false/' $i; done < file1.txt

EDIT: One thing to be aware of is running the above line that renames internal links more than once will add more ’s’es to those links. If that happens, modifying the line and running again will work, such as,

for i in `cat`;do sed -i 's/](\/postsss/](\/posts/' $i; done < file1.txt
for i in `cat`;do sed -i 's/](\/postss/](\/posts/' $i; done < file1.txt

Now, I have the basic outline working, but there remains a LOT of tweaking before I am satisfied with the final results. Anyway, that’s all for this post. Have a great day, and God Bless you and yours! Till next time…

References

Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2024. Rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.
© S Lazy-H 2019 -