Skip to content

Commit

Permalink
add 'rmd_to_knit' arg to use_docker to that the dockerfile is aware o…
Browse files Browse the repository at this point in the history
…f paper.Rmd location, following discussion in #38
  • Loading branch information
Ben Marwick committed Sep 4, 2017
1 parent 2acb0cf commit 645e5f2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# get the base image, the rocker/verse has R, RStudio and pandoc
FROM rocker/verse:3.4.0
FROM rocker/verse:3.4.1

# required
MAINTAINER Your Name <your_email@somewhere.com>
MAINTAINER Ben Marwick <benmarwick@gmail.com>

COPY . /rrtools

Expand All @@ -17,5 +17,6 @@ RUN . /etc/environment \
# build this compendium package
&& R -e "devtools::install('/rrtools', dep=TRUE)" \

# render the manuscript into a docx
# render the manuscript into a docx, you'll need to edit this if you've
# customised the location and name of your main Rmd file
&& R -e "rmarkdown::render('/rrtools/analysis/paper/paper.Rmd')"
44 changes: 31 additions & 13 deletions R/hello.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ use_circleci <- function(pkg = ".", browse = interactive(), docker_hub = TRUE) {
#' @param data forwarded to \code{whisker::whisker.render}
#' @param open_data should git track the files in the data directory?
#' @export
use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rmd', data = list(), open_data = TRUE) {
use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rmd', data = list(), data_in_git = TRUE) {
pkg <- as.package(pkg)
pkg$Rmd <- TRUE
gh <- github_info(pkg$path)
Expand Down Expand Up @@ -228,7 +228,7 @@ use_analysis <- function(pkg = ".", location = "top_level", template = 'paper.Rm
template)
)

if (!open_data) use_git_ignore("*/data/*")
if (!data_in_git) use_git_ignore("*/data/*")

message("Next: \n",
" * Write your article/paper/thesis in Rmd file(s) in analysis/paper/", "\n",
Expand All @@ -251,36 +251,56 @@ invisible(TRUE)
#'
#' @param pkg defaults to the package in the current working directory
#' @param rocker chr, the rocker image to base this container on
#' @param rmd_to_knit, chr, path to the Rmd file to render in the Docker
#' container, relative to the top level of the compendium
#' (i.e. "analysis/paper/paper.Rmd"). There's no need to specify this if your Rmd
#' to render is at "analysis/paper/paper.Rmd", "vignettes/paper/paper.Rmd" or
#' "inst/paper/paper.Rmd". If you have a custom directory structure, and a custom
#' file name for the Rmd file, you can specify that file path and name here so
#' Docker can find the file to render in the container.B
#'
#' @import utils devtools
#' @export
use_dockerfile <- function(pkg = ".", rocker = "verse") {

use_dockerfile <- function(pkg = ".", rocker = "verse", rmd_to_knit = "path_to_rmd") {
pkg <- as.package(pkg)

# get R version for rocker/r-ver
si <- utils::sessionInfo()
r_version <- paste0(si$R.version$major, ".", si$R.version$minor)

# get path to Rmd file to knit
if(rmd_to_knit == "path_to_rmd"){
dir_list <- list.dirs()
paper_dir <- dir_list[grep(pattern = "/paper", dir_list)]
rmd_path <- regmatches(paper_dir, regexpr("analysis|vignettes|inst", paper_dir))
rmd_path <- file.path(rmd_path, "paper/paper.Rmd")
} else {
rmd_path <- rmd_to_knit
}


# assign variables for whisker
gh <- github_info(pkg$path)
gh$r_version <- r_version
gh$rocker <- rocker
gh$rmd_path <- rmd_path

use_template("Dockerfile",
"Dockerfile",
ignore = TRUE,
pkg = pkg,
data = gh,
open = TRUE,
out_path = "")
"Dockerfile",
ignore = TRUE,
pkg = pkg,
data = gh,
open = TRUE,
out_path = "")

message("Next: \n",
" * Edit the dockerfile with your name & email", "\n",
" * Edit the dockerfile to include system dependencies, such as linux libraries that are needed by the R packages you're using", "\n",
" * Edit the last line of the dockerfile to specify which Rmd should be rendered in the Docker container", "\n" )
" * Check the last line of the dockerfile to specify which Rmd should be rendered in the Docker container, edit if necessary", "\n" )

invisible(TRUE)
}

#' Creates skeleton README files
#'
#' @description \itemize{
Expand Down Expand Up @@ -326,7 +346,6 @@ use_readme_rmd <- function(pkg = ".") {
pkg = pkg)
}


message("* Rendering README.Rmd to README.md for GitHub.")
rmarkdown::render("README.Rmd", output_format = NULL)

Expand All @@ -336,7 +355,6 @@ use_readme_rmd <- function(pkg = ".") {
message("* Adding instructions to contributors.")
use_contributing()


invisible(TRUE)
}

Expand Down
5 changes: 3 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ To create a reproducible research compendium using the rrtools approach, follow

#### 5. `rrtools::use_analysis()`

- this has three `location` options: create a top-level `analysis/` directory, create an `inst/` directory (so that all the sub-directories are available after the package is installed), or create a `vingettes/` directory (and automatically update the `DESCRIPTION`). The default is a top-level `analysis/`.
- this function has three `location = ` options: create a top-level `analysis/` directory, create an `inst/` directory (so that all the sub-directories are available after the package is installed), or create a `vingettes/` directory (and automatically update the `DESCRIPTION`). The default is a top-level `analysis/`.
- for each option, the contents of the sub-directories are the same, with the following (using the default `analysis/` for example):

```
Expand Down Expand Up @@ -99,7 +99,8 @@ analysis/
- the `references.bib` file has just one item to demonstrate the format. It is ready to insert more reference details.
- you can replace the supplied `csl` file with a different citation style from <https://github.com/citation-style-language/>
- we recommend using the [citr addin](https://github.com/crsh/citr) and [Zotero](https://www.zotero.org/) to efficiently insert citations while writing in an Rmd file
- remember that the `Imports:` field in the `DESCRIPTION` file must include the names of all packages used in analysis documents (e.g. `paper.Rmd`)
- remember that the `Imports:` field in the `DESCRIPTION` file must include the names of all packages used in analysis documents (e.g. `paper.Rmd`). We have a helper function `rrtools::add_dependencies_to_description()` that will scan the Rmd file, identify libraries used in there, and add them to the `DESCRIPTION` file.
- this function has an `data_in_git =` argument, which is `TRUE` by defail. If set to `FALSE` you will exclude files in the `data/` directory from being tracked by git and prevent them from appearing on GitHub. You should set `data_in_git = FALSE` if your data files are large (>100 mb is the limit for GitHub) or you do not want to make the data files publicly accessible on GitHub.

#### 6. `rrtools::use_dockerfile()`

Expand Down
5 changes: 3 additions & 2 deletions inst/templates/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ RUN . /etc/environment \
# build this compendium package
&& R -e "devtools::install('/{{{repo}}}', dep=TRUE)" \

# render the manuscript into a docx
&& R -e "rmarkdown::render('/{{{repo}}}/analysis/paper/paper.Rmd')"
# render the manuscript into a docx, you'll need to edit this if you've
# customised the location and name of your main Rmd file
&& R -e "rmarkdown::render('/{{{repo}}}/{{{rmd_path}}}')"
2 changes: 1 addition & 1 deletion man/use_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion man/use_dockerfile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 645e5f2

Please sign in to comment.