【r<-博客|rmd】技术与艺术结合|生成rmd文章模板

rmdmd文章时每次都需要写头文件信息或一些初始的配置,我们可以通过预先设定模板来简化这一过程。

构建

首先构建博文类型的模板。

诗歌:

---
title: "诗之名"
author: "王诗翔"
date: "`r Sys.Date())`"
slug: to-do-a-dream
categories:
  - 小诗
tags:
  - 诗
---

<center>

</center>

技术文章:

---
title: "Put your title here"
author: 王诗翔
date: "`r Sys.Date())`"
slug: "give a english slug for your post"
categories: 
    - R
tags:
    - dplyr
---

生成

我尝试用gif图片来动态显示生成文档的过程。

《【r<-博客|rmd】技术与艺术结合|生成rmd文章模板》 初始化技术博文
《【r<-博客|rmd】技术与艺术结合|生成rmd文章模板》 初始化诗歌博文

预览

最后看下生成的rmd文档的效果吧~

《【r<-博客|rmd】技术与艺术结合|生成rmd文章模板》 预览操作

如果你有渴望,手中的工具都能创造心中的一份诗意。

最后可以点击看看这首诗喔~

最后提供下代码:

new_post <- function(post_name=NULL, type = c('post', 'poem'),
                     template_path = getwd()){
    if(is.null(post_name)){
        stop("A post name must be given!")
    }
    
    type <- match.arg(type)
    if (type == "post"){
        template_name <- "post_template.Rmd"
        post_path <- paste0(getwd(),"/content/post/2018/") # modify path for you categories
    }
    if (type == "poem"){
        template_name <- "poem_template.Rmd"
        post_path <- paste0(getwd(),"/content/poem/")
    }
    
    
    input_file   <- paste(template_path,template_name, sep="/")
    current_time <- Sys.Date()
    out_file     <- paste0(post_path, current_time, "-",post_name,".Rmd")
    fl_content   <- readLines(input_file)
    writeLines(fl_content, out_file)
    print("New Rmarkdown post creat successfully!")
}
    原文作者:王诗翔
    原文地址: https://www.jianshu.com/p/b99b7b47e472
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞