This is the extension of previous question: Automating the generation of preformated text in Rmarkdown using R. Here's the task:
You have a collection of song lyrics stored in data.frame
, the columns of which are: song lyrics (with many \n
at the end of each line), song title, song album, music author, words author, year, etc...
Now you want to produce automatically a nicely formatted book of this poetry using markdown
(or bookdown
) and glue
packages, which should generate an html of this kind:
<h1> Album name </h1>
<h2> Song name </h2>
<blockquote>
<cite>
Music: author_music <br>
Words: author_words
</cite>
<pre>
lyrics-line1
...
lyrics-lineN
</pre>
</blockquote>
The challenge is that, if you use the code from Automating the generation of preformated text in Rmarkdown using R, it will print all lyrics as one long line: lyrics-line\n...\nlyrics-lineN
In other words, is there a way to print lyrics lines (line\n ... \nlyrics-lineN) line by line, rather than one concatenated line ?
This is code can be used to get the lyrics data to play with :
library(data.table); library(magrittr); library(knitr); library(stringr);library(dplyr)
dt <- fread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/prince_raw_data.csv")
dt <- dt[301:303,2:6] #take three songs only
dt %>% names
dt %>% kable() # just to view the lyrics in your console.
NB: that kable()
function does not replace \n
with <br>
, which we would like to.
So if you use it to generate an html of your poetry, you'll have the same problem - instead of a verse with many lines, you'll get one long concatenated line.
PS. One way to circumvent the problem is to split the original text string into many one-line strings (eg. by using str_split(dt$text, '\n')
and then print each line separately using a for
loop. But there should be a better way of printing verses, I hope.