-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kdgosik
committed
Jan 5, 2018
1 parent
c63b827
commit 5459439
Showing
12 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Package: NotebookToSite | ||
Type: Package | ||
Title: What the Package Does (Title Case) | ||
Version: 0.1.0 | ||
Author: Who wrote it | ||
Maintainer: The package maintainer <yourself@somewhere.net> | ||
Description: More about what it does (maybe more than one line) | ||
Use four spaces when indenting paragraphs within the Description. | ||
License: What license is it under? | ||
Encoding: UTF-8 | ||
LazyData: true | ||
RoxygenNote: 6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exportPattern("^[[:alpha:]]+") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#' create_site | ||
#' | ||
#' The function create_site is meant to make reporducible workflows easier and more standardized. It | ||
#' many folders commonly used in projects suchs as docs, src and references. It also creates an | ||
#' Rmarkdown file for a notebook as well as uses knitr to spin it into a static website. This html | ||
#' file can be easily shared with collaborators or posted on github pages. It is already placed in | ||
#' the docs folder, which the user can enable a github pages website to be created from that folder | ||
#' under the repository director. | ||
#' | ||
#' @seealso knitr | ||
#' @export | ||
#' @param github_username string of your github username to create a button to direct to your page. | ||
#' @keywords knitr | ||
#' @keywords purl | ||
#' @import utils | ||
#' @import rmarkdown | ||
#' @importFrom knitr purl | ||
#' @importFrom knitr spin | ||
#' @author Kirk Gosik <kdgosik@gmail.com> | ||
|
||
|
||
|
||
create_site <- function(github_username = "kdgosik") { | ||
|
||
create_these <- c("src", "references", "docs", "results", | ||
"src/images", "src/html", "src/chunks") | ||
for( d in create_these ) dir.create(d, showWarnings = FALSE) | ||
|
||
try({ | ||
## reproducible workflow image backgroud found at the ebook The Practice of Reproducible Research | ||
download.file(url = "github.com/kdgosik/NotebookToSite/inst/images/cboettig.png", | ||
destfile = "src/images/cboettig.png") | ||
}) | ||
|
||
## writing a simple html button file that references my github page | ||
html_lines <- c("<!DOCTYPE html>", | ||
"<html>", | ||
paste0('<a href="https://github.com/', github_username, '"><button>My Github</button></a>'), | ||
"</html>") | ||
writeLines(html_lines, con = "src/html/github_button.html") | ||
|
||
## custom css included in the Notebook file to set backgroud of downloaded image above | ||
customcss_lines <- c("body{", | ||
" background-image: url('images/cboettig.png');", | ||
"min-height: 500px;", | ||
" /* Set background image to fixed (don't scroll along with the page) */", | ||
" background-attachment: fixed;", | ||
"background-position: right top;", | ||
"/* Set the background image to no repeat */", | ||
"background-repeat: no-repeat;", | ||
"/* Scale the background image to be as large as possible */", | ||
"background-size: cover;", | ||
"}") | ||
writeLines(customcss_lines, "src/custom.css") | ||
|
||
# writes the site yaml file | ||
site_lines <- c('name: "Reproducible Workflows"', | ||
'output_dir: "../docs"', | ||
'navbar:', | ||
' title: "Reproducible Workflows"', | ||
' left:', | ||
' - text: "Home"', | ||
' href: index.html', | ||
' - text: "Notebook"', | ||
' href: Notebook.html') | ||
writeLines(site_lines, "src/_site.yml") | ||
|
||
## create index file for website | ||
index_lines <- c('---', | ||
'title: "Index"', | ||
'output:', | ||
' html_document:', | ||
' toc: true', | ||
' toc_float: true', | ||
'---') | ||
index_lines <- paste0("#'", index_lines) | ||
writeLines(index_lines, con = "src/index.R") | ||
spin("src/index.R", knit = FALSE, format = "Rmd") | ||
|
||
|
||
## creating Notebook Rmarkdown file | ||
template_lines <- c('---', | ||
'title: "Notebook"', | ||
'output:', | ||
' html_document:', | ||
' toc: true', | ||
' toc_float: true', | ||
' css: custom.css', | ||
' includes:', | ||
' in_header: html/github_button.html', | ||
'---', | ||
'', | ||
'## Comments', | ||
'I need to add markdown comments here', | ||
'[Picture Credit](https://www.practicereproducibleresearch.org/case-studies/cboettig.html)', | ||
'') | ||
|
||
template_lines <- paste0("#'", template_lines) | ||
code_lines <- c('```{r NotebookSetup, include=FALSE}', | ||
'knitr::opts_chunk$set(echo = TRUE)') | ||
template_lines <- c(template_lines, code_lines) | ||
writeLines(template_lines, con = "src/Notebook.R") | ||
spin("src/Notebook.R", knit = FALSE, format = "Rmd") | ||
|
||
|
||
## output a purl version of the notebook at each documentation level | ||
lapply(0:2, function(i){ | ||
new_file <- paste0("src/Notebook_DocLevel",i,".Rmd") | ||
file.copy("src/Notebook.Rmd", new_file, overwrite = TRUE) | ||
purl(new_file, documentation = i) | ||
r_file <- paste0("Notebook_DocLevel", i, ".R") | ||
file.rename(r_file, paste0("src/", r_file)) | ||
}) | ||
|
||
## copy and rename the documentation 2 level R file to the Final Notebook version | ||
file.copy("src/Notebook_DocLevel2.R", "src/FinalNotebook.R", overwrite = TRUE) | ||
|
||
## spin Final Notebook version R file into and .Rmd file | ||
spin("src/FinalNotebook.R", knit = FALSE, format = "Rmd") | ||
|
||
## purl individual chunks of the final notbook into separate R files | ||
purl_chunks("src/FinalNotebook.Rmd") | ||
|
||
## render all markdown files in the src folder into html files for the website | ||
rmarkdown::render_site(input = "src") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' purl_chunks | ||
#' | ||
#' The function purl_chunks takes an Rmarkdown file and returns each chunk as a | ||
#' seperate R file. It was found on stackoverflow at | ||
#' (https://stackoverflow.com/questions/35855837/with-knitr-preserve-chunk-options-when-purling-chunks-into-separate-files) | ||
#' | ||
#' @seealso knitr | ||
#' @export | ||
#' @param input_file String of the input file name to the Rmarkdown file that will be purlled into seperate chunks | ||
#' @param input_path String of the path where to place the R files of each of the chunks | ||
#' @keywords knitr | ||
#' @keywords purl | ||
#' @importFrom knitr purl | ||
#' @author Kirk Gosik <kdgosik@gmail.com> | ||
|
||
|
||
purl_chunks <- function(input_file, input_path = "src/chunks"){ | ||
|
||
purled <- knitr::purl(input_file) # purl original file; save name to variable | ||
lines <- readLines(purled) # read purled file into a character vector of lines | ||
starts <- grep('^## ----.*-+', lines) # use grep to find header row indices | ||
stops <- c(starts[-1] - 1L, length(lines)) # end row indices | ||
# extract chunk names from headers | ||
names <- sub('^## ----([^-]([^,=]*[^,=-])*)[,-][^=].*', '\\1', lines[starts]) | ||
names <- ifelse(names == lines[starts], '', paste0(names)) # clean if no chunk name | ||
# make nice file names with chunk number and name (if exists) | ||
file_names <- file.path(input_path, | ||
paste0(names, '.R')) | ||
for(chunk in seq_along(starts)){ # loop over header rows | ||
# save the lines in the chunk to a file | ||
writeLines(append("#!/usr/bin/env Rscript", | ||
lines[starts[chunk]:stops[chunk]]), | ||
con = file_names[chunk]) | ||
} | ||
unlink(purled) # delete purled file of entire document | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#' update_site | ||
#' | ||
#' The function create_site is meant to make reporducible workflows easier and more standardized. It | ||
#' many folders commonly used in projects suchs as docs, src and references. It also creates an | ||
#' Rmarkdown file for a notebook as well as uses knitr to spin it into a static website. This html | ||
#' file can be easily shared with collaborators or posted on github pages. It is already placed in | ||
#' the docs folder, which the user can enable a github pages website to be created from that folder | ||
#' under the repository director. | ||
#' | ||
#' @seealso knitr | ||
#' @export | ||
#' @keywords knitr | ||
#' @keywords purl | ||
#' @import utils | ||
#' @import rmarkdown | ||
#' @importFrom knitr purl | ||
#' @importFrom knitr spin | ||
#' @author Kirk Gosik <kdgosik@gmail.com> | ||
|
||
|
||
## This will be a seperate function to update the site | ||
|
||
## needs updated!!! | ||
|
||
update_site <- function() { | ||
|
||
## check if an index file exists for the website, if not create one | ||
if( !{file.exists("src/index.Rmd")} ) { | ||
index_lines <- c('---', | ||
'title: "Index"', | ||
'output:', | ||
' html_document:', | ||
' toc: true', | ||
' toc_float: true', | ||
'---') | ||
index_lines <- paste0("#'", index_lines) | ||
writeLines(index_lines, con = "src/index.R") | ||
spin("src/index.R", knit = FALSE, format = "Rmd") | ||
} | ||
|
||
|
||
## check if a Notebook exists for website, if not creat one | ||
if( !{file.exists("src/Notebook.Rmd")} ) { | ||
template_lines <- c('---', | ||
'title: "Notebook"', | ||
'output:', | ||
' html_document:', | ||
' toc: true', | ||
' toc_float: true', | ||
' css: custom.css', | ||
' includes:', | ||
' in_header: html/github_button.html', | ||
'---', | ||
'', | ||
'## Comments', | ||
'I need to add markdown comments here', | ||
'[Picture Credit](https://www.practicereproducibleresearch.org/case-studies/cboettig.html)', | ||
'') | ||
|
||
template_lines <- paste0("#'", template_lines) | ||
code_lines <- c('```{r NotebookSetup, include=FALSE}', | ||
'knitr::opts_chunk$set(echo = TRUE)') | ||
template_lines <- c(template_lines, code_lines) | ||
writeLines(template_lines, con = "src/Notebook.R") | ||
spin("src/Notebook.R", knit = FALSE, format = "Rmd") | ||
} | ||
|
||
## output a purl version of the notebook at each documentation level | ||
lapply(0:2, function(i){ | ||
new_file <- paste0("src/Notebook_DocLevel",i,".Rmd") | ||
file.copy("src/Notebook.Rmd", new_file, overwrite = TRUE) | ||
purl(new_file, documentation = i) | ||
r_file <- paste0("Notebook_DocLevel", i, ".R") | ||
file.rename(r_file, paste0("src/", r_file)) | ||
}) | ||
|
||
## copy and rename the documentation 2 level R file to the Final Notebook version | ||
file.copy("src/Notebook_DocLevel2.R", "src/FinalNotebook.R", overwrite = TRUE) | ||
|
||
## spin Final Notebook version R file into and .Rmd file | ||
spin("src/FinalNotebook.R", knit = FALSE, format = "Rmd") | ||
|
||
## purl individual chunks of the final notbook into separate R files | ||
purl_chunks("src/FinalNotebook.Rmd") | ||
|
||
## render all markdown files in the src folder into html files for the website | ||
rmarkdown::render_site(input = "src") | ||
|
||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.