Skip to content

Instantly share code, notes, and snippets.

@petermeissner
Last active August 29, 2015 14:13
Show Gist options
  • Save petermeissner/6dc6cf6de97b0c9a6dc7 to your computer and use it in GitHub Desktop.
Save petermeissner/6dc6cf6de97b0c9a6dc7 to your computer and use it in GitHub Desktop.
Rmd snippet that translates itself into Rpres
[...]
```{r, include=FALSE}
RMDtoRpres <- function(fname){
require(stringr)
text <- readLines(fname, encoding="UTF-8")
text <- paste(text, collapse="\n")
author <- str_replace_all(str_extract(text,'author: ".*?\n'),'["\n]',"")
date <- str_replace_all(str_extract(text,"date: '.*?\n"),'["\n]',"")
title <- str_replace(str_replace_all(str_extract(text,'title: ".*?\n'),'["\n]',""),"title: ","")
subtitle <- str_replace(str_replace_all(str_extract(text,'subtitle: .*?\n'),'["\n]',""),"subtitle: ","")
text <- str_replace(text, "---\n.*?---\n",
paste0(str_replace(title, ".Rmd", ""),
"\n=============\n", author, "\n",date, "\n\n", subtitle, "\n\n"))
rule <- "\n## .+?\n"
while(str_detect(text, rule)){
replacement <- paste0( str_replace(str_extract(text, rule), "## ",""), "=============\n")
text <- str_replace(text, rule, replacement)
}
writeLines(text, str_replace(fname, "\\.Rmd", ".Rpres"), useBytes=TRUE)
}
fname <- "session1.Rmd"
RMDtoRpres(fname)
```
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment