使用markdown,knitr和胶水从data.frame中自动格式化好格式的歌词书 [英] Automated nicely formated book of lyrics from data.frame using markdown, knitr and glue

查看:107
本文介绍了使用markdown,knitr和胶水从data.frame中自动格式化好格式的歌词书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是上一个问题的扩展:自动化使用R 在Rmarkdown中生成预格式化的文本.这是任务:

This is the extension of previous question: Automating the generation of preformated text in Rmarkdown using R. Here's the task:

您有一个存储在data.frame中的歌曲歌词集合,其列为:歌曲歌词(每行末尾有许多\n),歌曲标题,专辑,音乐作者,词作者,年等...

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...

现在,您想使用markdown(或bookdown)和glue软件包自动生成一本格式良好的诗集,这将产生此类html:

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

换句话说,有没有办法一行一行地打印歌词行(\ 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.

注意:kable()函数不会将\n替换为<br>,这是我们想要的.
因此,如果使用它生成诗歌的html,则会遇到相同的问题-而不是一首多行的经文,您会得到一条长长的连接线.

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.解决该问题的一种方法是将原始文本字符串分成许多单行字符串(例如,使用str_split(dt$text, '\n'),然后使用for循环分别打印每行.但是应该有一种更好的打印经文的方法,我希望.

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.

推荐答案

换句话说,是否有一种方法可以一行一行地打印歌词行(\ 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 ?

它是串联的,因为这是一个减价规则,每行末尾需要两个空格.

It's concatenated because that's a markdown rule, which requires two spaces at the end of each line.

我的解决方案是在dt$text周围添加原始html标签<pre> <\pre>,以防止它们被markdown语法解析:

My solution is to add raw html tags<pre> <\pre> around your dt$text to protect them from being parsed by markdown syntax:

(但是我注意到<pre>会将文本呈现为代码块,呃)

(However I notice that <pre> will render text as code blocks, uh)

```{r echo=FALSE, results='asis'}
for (i in 1:nrow(dt)){
  album = dt$album[i]
  song = dt$song[i]
  lyrics = dt$text[i]

  cat(glue::glue("# {album}\n\n"))
  cat(glue::glue("## {song}\n\n"))
  cat(glue::glue("<pre>{lyrics}</pre>"))
}
```

输出

源rmd

或者,将\n替换为\n(在\ n之前插入两个空格),而不使用glue:

Or, replace \n with \n(insert two spaces before \n), without glue:

```{r echo=FALSE, results='asis'}

dt$text <- gsub("\n","  \n",dt$text)

for (i in 1:nrow(dt)){
  cat(paste0("# ",dt$album[i],"\n\n"))
  cat(paste0("## ",dt$song[i],"\n\n"))
  cat(paste0("> ",dt$text[i]))
  cat("\n\n")
}
```

这篇关于使用markdown,knitr和胶水从data.frame中自动格式化好格式的歌词书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆