Is it possible to write a reusable function based on your R scripts? In the single function, it might be possible to obtain the image from the database, store it to disk, convert the image to base-64 and include it in the R Markdown document (making it self-contained), and remove the image from disk.
If this sounds reasonable, I could provide some code for each of the parts.